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_FunctionDerivative.hxx>
28 #include <Expr_GeneralExpression.hxx>
29 #include <Expr_GeneralFunction.hxx>
30 #include <Expr_InvalidFunction.hxx>
31 #include <Expr_NamedUnknown.hxx>
32 #include <Expr_NotEvaluable.hxx>
33 #include <Expr_NumericValue.hxx>
34 #include <Expr_Operators.hxx>
35 #include <Expr_Product.hxx>
36 #include <Expr_Sum.hxx>
37 #include <Standard_NumericError.hxx>
38 #include <Standard_Type.hxx>
39 #include <TCollection_AsciiString.hxx>
41 IMPLEMENT_STANDARD_RTTIEXT(Expr_BinaryFunction,Expr_BinaryExpression)
43 Expr_BinaryFunction::Expr_BinaryFunction (const Handle(Expr_GeneralFunction)& func, const Handle(Expr_GeneralExpression)& exp1, const Handle(Expr_GeneralExpression)& exp2)
45 if (func->NbOfVariables() != 2) {
46 throw Expr_InvalidFunction();
49 CreateFirstOperand(exp1);
50 CreateSecondOperand(exp2);
53 Handle(Expr_GeneralExpression) Expr_BinaryFunction::ShallowSimplified () const
55 if (FirstOperand()->IsKind(STANDARD_TYPE(Expr_NumericValue))) {
56 if (SecondOperand()->IsKind(STANDARD_TYPE(Expr_NumericValue))) {
57 TColStd_Array1OfReal tabval(1,2);
58 tabval(1) = Handle(Expr_NumericValue)::DownCast(FirstOperand())->GetValue();
59 tabval(2) = Handle(Expr_NumericValue)::DownCast(SecondOperand())->GetValue();
60 Expr_Array1OfNamedUnknown vars(1,2);
61 vars(1) = myFunction->Variable(1);
62 vars(2) = myFunction->Variable(2);
63 Standard_Real res = myFunction->Evaluate(vars,tabval);
64 return new Expr_NumericValue(res);
67 Handle(Expr_BinaryFunction) me = this;
71 Handle(Expr_GeneralExpression) Expr_BinaryFunction::Copy () const
73 return new Expr_BinaryFunction(myFunction,
74 Expr::CopyShare(FirstOperand()),
75 Expr::CopyShare(SecondOperand()));
78 Standard_Boolean Expr_BinaryFunction::IsIdentical (const Handle(Expr_GeneralExpression)& Other) const
80 if (!Other->IsKind(STANDARD_TYPE(Expr_BinaryFunction))) {
81 return Standard_False;
83 Handle(Expr_BinaryFunction) fother = Handle(Expr_BinaryFunction)::DownCast(Other);
84 Handle(Expr_GeneralExpression) otherexp = fother->FirstOperand();
85 if (otherexp->IsIdentical(FirstOperand())) {
86 otherexp = fother->SecondOperand();
87 if (otherexp->IsIdentical(SecondOperand())) {
88 if (myFunction->IsIdentical(fother->Function())) {
93 return Standard_False;
96 Standard_Boolean Expr_BinaryFunction::IsLinear () const
98 if (!ContainsUnknowns()) {
101 if (!FirstOperand()->IsLinear()) {
102 return Standard_False;
104 if (!SecondOperand()->IsLinear()) {
105 return Standard_False;
107 if (!myFunction->IsLinearOnVariable(1)) {
108 return Standard_False;
110 return myFunction->IsLinearOnVariable(2);
113 Handle(Expr_GeneralExpression) Expr_BinaryFunction::Derivative (const Handle(Expr_NamedUnknown)& X) const
115 Handle(Expr_NamedUnknown) myvar1 = myFunction->Variable(1);
116 Handle(Expr_NamedUnknown) myvar2 = myFunction->Variable(2);
117 Handle(Expr_GeneralExpression) myfop = FirstOperand();
118 Handle(Expr_GeneralExpression) mysop = SecondOperand();
119 Handle(Expr_GeneralExpression) myexpder1 = myfop->Derivative(X);
120 Handle(Expr_GeneralExpression) myexpder2 = mysop->Derivative(X);
122 Handle(Expr_GeneralFunction) myfuncder1 = myFunction->Derivative(myvar1);
123 Handle(Expr_BinaryFunction) firstpart
124 = new Expr_BinaryFunction(myfuncder1,
125 Expr::CopyShare(myfop),
126 Expr::CopyShare(mysop));
128 Handle(Expr_GeneralExpression) fpart = firstpart->ShallowSimplified() * myexpder1;
130 Handle(Expr_GeneralFunction) myfuncder2 = myFunction->Derivative(myvar2);
131 Handle(Expr_BinaryFunction) secondpart
132 = new Expr_BinaryFunction(myfuncder2,
133 Expr::CopyShare(myfop),
134 Expr::CopyShare(mysop));
136 Handle(Expr_GeneralExpression) spart = secondpart->ShallowSimplified() * myexpder2;
138 fpart = fpart->ShallowSimplified();
139 spart = spart->ShallowSimplified();
140 return (fpart + spart)->ShallowSimplified();
143 Handle(Expr_GeneralFunction) Expr_BinaryFunction::Function () const
148 Standard_Real Expr_BinaryFunction::Evaluate(const Expr_Array1OfNamedUnknown& vars, const TColStd_Array1OfReal& vals) const
150 Expr_Array1OfNamedUnknown varsfunc(1,2);
151 varsfunc(1) = myFunction->Variable(1);
152 varsfunc(2) = myFunction->Variable(2);
153 TColStd_Array1OfReal valsfunc(1,2);
154 valsfunc(1) = FirstOperand()->Evaluate(vars,vals);
155 valsfunc(2) = SecondOperand()->Evaluate(vars,vals);
156 return myFunction->Evaluate(varsfunc,valsfunc);
159 TCollection_AsciiString Expr_BinaryFunction::String() const
161 TCollection_AsciiString res = myFunction->GetStringName();
162 res += TCollection_AsciiString('(');
163 res += FirstOperand()->String();
165 res += SecondOperand()->String();