1 // Created on: 1991-04-17
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.
19 #include <Expr_Difference.hxx>
20 #include <Expr_GeneralExpression.hxx>
21 #include <Expr_NamedUnknown.hxx>
22 #include <Expr_NotEvaluable.hxx>
23 #include <Expr_NumericValue.hxx>
24 #include <Expr_Operators.hxx>
25 #include <Expr_Sum.hxx>
26 #include <Expr_UnaryMinus.hxx>
27 #include <Standard_NumericError.hxx>
28 #include <Standard_OutOfRange.hxx>
29 #include <Standard_Type.hxx>
30 #include <TCollection_AsciiString.hxx>
32 IMPLEMENT_STANDARD_RTTIEXT(Expr_Difference,Expr_BinaryExpression)
34 Expr_Difference::Expr_Difference (const Handle(Expr_GeneralExpression)& exp1, const Handle(Expr_GeneralExpression)& exp2)
36 CreateFirstOperand(exp1);
37 CreateSecondOperand(exp2);
41 Handle(Expr_GeneralExpression) Expr_Difference::ShallowSimplified() const
43 Handle(Expr_GeneralExpression) myfirst = FirstOperand();
44 Handle(Expr_GeneralExpression) mysecond = SecondOperand();
46 Standard_Boolean nvfirst = myfirst->IsKind(STANDARD_TYPE(Expr_NumericValue));
47 Standard_Boolean nvsecond = mysecond->IsKind(STANDARD_TYPE(Expr_NumericValue));
48 if (nvfirst && nvsecond) {
50 Handle(Expr_NumericValue) myNVfirst = Handle(Expr_NumericValue)::DownCast(myfirst);
51 Handle(Expr_NumericValue) myNVsecond = Handle(Expr_NumericValue)::DownCast(mysecond);
52 return new Expr_NumericValue(myNVfirst->GetValue()-myNVsecond->GetValue());
54 if (nvfirst && !nvsecond) {
56 Handle(Expr_NumericValue) myNVfirst = Handle(Expr_NumericValue)::DownCast(myfirst);
57 if (myNVfirst->GetValue() == 0.0) {
62 if (!nvfirst && nvsecond) {
64 Handle(Expr_NumericValue) myNVsecond = Handle(Expr_NumericValue)::DownCast(mysecond);
65 if (myNVsecond->GetValue() == 0.0) {
70 // Treat UnaryMinus case
71 Standard_Boolean unfirst = myfirst->IsKind(STANDARD_TYPE(Expr_UnaryMinus));
72 Standard_Boolean unsecond = mysecond->IsKind(STANDARD_TYPE(Expr_UnaryMinus));
73 if (unfirst && unsecond) {
74 // case (-ssX1) - (-ssX2) = ssX2 - ssX1
75 Handle(Expr_GeneralExpression) ssop1 = myfirst->SubExpression(1);
76 Handle(Expr_GeneralExpression) ssop2 = mysecond->SubExpression(1);
79 if (unfirst && !unsecond) {
80 // case (-ssX1) - X2 = -( ssX1 + X2)
81 Handle(Expr_GeneralExpression) ssop1 = myfirst->SubExpression(1);
82 return -(ssop1 + mysecond);
84 if (!unfirst && unsecond) {
85 // case X1 - (-ssX2) = X1 + ssX2
86 Handle(Expr_GeneralExpression) ssop2 = mysecond->SubExpression(1);
87 return myfirst + ssop2;
89 Handle(Expr_Difference) me = this;
93 Handle(Expr_GeneralExpression) Expr_Difference::Copy () const
95 return Expr::CopyShare(FirstOperand()) - Expr::CopyShare(SecondOperand());
98 Standard_Boolean Expr_Difference::IsIdentical (const Handle(Expr_GeneralExpression)& Other) const
100 Standard_Boolean ident = Standard_False;
101 if (Other->IsKind(STANDARD_TYPE(Expr_Difference))) {
102 Handle(Expr_GeneralExpression) myfirst = FirstOperand();
103 Handle(Expr_GeneralExpression) mysecond = SecondOperand();
104 Handle(Expr_Difference) DOther = Handle(Expr_Difference)::DownCast(Other);
105 Handle(Expr_GeneralExpression) fother = DOther->FirstOperand();
106 Handle(Expr_GeneralExpression) sother = DOther->SecondOperand();
107 if ((myfirst->IsIdentical(fother)) &&
108 (mysecond->IsIdentical(sother))) {
109 ident = Standard_True;
115 Standard_Boolean Expr_Difference::IsLinear () const
117 Handle(Expr_GeneralExpression) myfirst = FirstOperand();
118 Handle(Expr_GeneralExpression) mysecond = SecondOperand();
119 return (myfirst->IsLinear() && mysecond->IsLinear());
122 Handle(Expr_GeneralExpression) Expr_Difference::Derivative (const Handle(Expr_NamedUnknown)& X) const
125 return new Expr_NumericValue(0.0);
127 Handle(Expr_GeneralExpression) myfirst = FirstOperand();
128 Handle(Expr_GeneralExpression) mysecond = SecondOperand();
130 myfirst = myfirst->Derivative(X);
131 mysecond = mysecond->Derivative(X);
132 Handle(Expr_Difference) der = myfirst - mysecond;
133 return der->ShallowSimplified();
136 Handle(Expr_GeneralExpression) Expr_Difference::NDerivative (const Handle(Expr_NamedUnknown)& X, const Standard_Integer N) const
139 throw Standard_OutOfRange();
142 return new Expr_NumericValue(0.0);
144 Handle(Expr_GeneralExpression) myfirst = FirstOperand();
145 Handle(Expr_GeneralExpression) mysecond = SecondOperand();
147 myfirst = myfirst->NDerivative(X,N);
148 mysecond = mysecond->NDerivative(X,N);
149 Handle(Expr_Difference) der = myfirst - mysecond;
150 return der->ShallowSimplified();
155 Standard_Real Expr_Difference::Evaluate(const Expr_Array1OfNamedUnknown& vars, const TColStd_Array1OfReal& vals) const
157 Standard_Real res = FirstOperand()->Evaluate(vars,vals);
158 return res - SecondOperand()->Evaluate(vars,vals);
161 TCollection_AsciiString Expr_Difference::String() const
163 Handle(Expr_GeneralExpression) op1 = FirstOperand();
164 Handle(Expr_GeneralExpression) op2 = SecondOperand();
165 TCollection_AsciiString str;
166 if (op1->NbSubExpressions() > 1) {
168 str += op1->String();
175 if (op2->NbSubExpressions() > 1) {
177 str += op2->String();
181 str += op2->String();