1 // Created on: 1991-05-30
2 // Created by: Arnaud BOUZY
3 // Copyright (c) 1991-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
18 #define No_Standard_RangeError
19 #define No_Standard_OutOfRange
24 #include <Expr_Array1OfGeneralExpression.hxx>
25 #include <Expr_Array1OfNamedUnknown.hxx>
26 #include <Expr_BinaryFunction.hxx>
27 #include <Expr_GeneralExpression.hxx>
28 #include <Expr_GeneralFunction.hxx>
29 #include <Expr_InvalidFunction.hxx>
30 #include <Expr_NamedUnknown.hxx>
31 #include <Expr_Operators.hxx>
32 #include <Expr_Sum.hxx>
33 #include <Standard_Type.hxx>
34 #include <TCollection_AsciiString.hxx>
36 IMPLEMENT_STANDARD_RTTIEXT(Expr_BinaryFunction,Expr_BinaryExpression)
38 Expr_BinaryFunction::Expr_BinaryFunction (const Handle(Expr_GeneralFunction)& func, const Handle(Expr_GeneralExpression)& exp1, const Handle(Expr_GeneralExpression)& exp2)
40 if (func->NbOfVariables() != 2) {
41 throw Expr_InvalidFunction();
44 CreateFirstOperand(exp1);
45 CreateSecondOperand(exp2);
48 Handle(Expr_GeneralExpression) Expr_BinaryFunction::ShallowSimplified () const
50 if (FirstOperand()->IsKind(STANDARD_TYPE(Expr_NumericValue))) {
51 if (SecondOperand()->IsKind(STANDARD_TYPE(Expr_NumericValue))) {
52 TColStd_Array1OfReal tabval(1,2);
53 tabval(1) = Handle(Expr_NumericValue)::DownCast(FirstOperand())->GetValue();
54 tabval(2) = Handle(Expr_NumericValue)::DownCast(SecondOperand())->GetValue();
55 Expr_Array1OfNamedUnknown vars(1,2);
56 vars(1) = myFunction->Variable(1);
57 vars(2) = myFunction->Variable(2);
58 Standard_Real res = myFunction->Evaluate(vars,tabval);
59 return new Expr_NumericValue(res);
62 Handle(Expr_BinaryFunction) me = this;
66 Handle(Expr_GeneralExpression) Expr_BinaryFunction::Copy () const
68 return new Expr_BinaryFunction(myFunction,
69 Expr::CopyShare(FirstOperand()),
70 Expr::CopyShare(SecondOperand()));
73 Standard_Boolean Expr_BinaryFunction::IsIdentical (const Handle(Expr_GeneralExpression)& Other) const
75 if (!Other->IsKind(STANDARD_TYPE(Expr_BinaryFunction))) {
76 return Standard_False;
78 Handle(Expr_BinaryFunction) fother = Handle(Expr_BinaryFunction)::DownCast(Other);
79 Handle(Expr_GeneralExpression) otherexp = fother->FirstOperand();
80 if (otherexp->IsIdentical(FirstOperand())) {
81 otherexp = fother->SecondOperand();
82 if (otherexp->IsIdentical(SecondOperand())) {
83 if (myFunction->IsIdentical(fother->Function())) {
88 return Standard_False;
91 Standard_Boolean Expr_BinaryFunction::IsLinear () const
93 if (!ContainsUnknowns()) {
96 if (!FirstOperand()->IsLinear()) {
97 return Standard_False;
99 if (!SecondOperand()->IsLinear()) {
100 return Standard_False;
102 if (!myFunction->IsLinearOnVariable(1)) {
103 return Standard_False;
105 return myFunction->IsLinearOnVariable(2);
108 Handle(Expr_GeneralExpression) Expr_BinaryFunction::Derivative (const Handle(Expr_NamedUnknown)& X) const
110 Handle(Expr_NamedUnknown) myvar1 = myFunction->Variable(1);
111 Handle(Expr_NamedUnknown) myvar2 = myFunction->Variable(2);
112 Handle(Expr_GeneralExpression) myfop = FirstOperand();
113 Handle(Expr_GeneralExpression) mysop = SecondOperand();
114 Handle(Expr_GeneralExpression) myexpder1 = myfop->Derivative(X);
115 Handle(Expr_GeneralExpression) myexpder2 = mysop->Derivative(X);
117 Handle(Expr_GeneralFunction) myfuncder1 = myFunction->Derivative(myvar1);
118 Handle(Expr_BinaryFunction) firstpart
119 = new Expr_BinaryFunction(myfuncder1,
120 Expr::CopyShare(myfop),
121 Expr::CopyShare(mysop));
123 Handle(Expr_GeneralExpression) fpart = firstpart->ShallowSimplified() * myexpder1;
125 Handle(Expr_GeneralFunction) myfuncder2 = myFunction->Derivative(myvar2);
126 Handle(Expr_BinaryFunction) secondpart
127 = new Expr_BinaryFunction(myfuncder2,
128 Expr::CopyShare(myfop),
129 Expr::CopyShare(mysop));
131 Handle(Expr_GeneralExpression) spart = secondpart->ShallowSimplified() * myexpder2;
133 fpart = fpart->ShallowSimplified();
134 spart = spart->ShallowSimplified();
135 return (fpart + spart)->ShallowSimplified();
138 Handle(Expr_GeneralFunction) Expr_BinaryFunction::Function () const
143 Standard_Real Expr_BinaryFunction::Evaluate(const Expr_Array1OfNamedUnknown& vars, const TColStd_Array1OfReal& vals) const
145 Expr_Array1OfNamedUnknown varsfunc(1,2);
146 varsfunc(1) = myFunction->Variable(1);
147 varsfunc(2) = myFunction->Variable(2);
148 TColStd_Array1OfReal valsfunc(1,2);
149 valsfunc(1) = FirstOperand()->Evaluate(vars,vals);
150 valsfunc(2) = SecondOperand()->Evaluate(vars,vals);
151 return myFunction->Evaluate(varsfunc,valsfunc);
154 TCollection_AsciiString Expr_BinaryFunction::String() const
156 TCollection_AsciiString res = myFunction->GetStringName();
157 res += TCollection_AsciiString('(');
158 res += FirstOperand()->String();
160 res += SecondOperand()->String();