b311480e |
1 | // Created on: 1991-04-17 |
2 | // Created by: Arnaud BOUZY |
3 | // Copyright (c) 1991-1999 Matra Datavision |
973c2be1 |
4 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e |
5 | // |
973c2be1 |
6 | // This file is part of Open CASCADE Technology software library. |
b311480e |
7 | // |
d5f74e42 |
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 |
973c2be1 |
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. |
b311480e |
13 | // |
973c2be1 |
14 | // Alternatively, this file may be used under the terms of Open CASCADE |
15 | // commercial license or contractual agreement. |
7fd59977 |
16 | |
42cf5bc1 |
17 | |
18 | #include <Expr.hxx> |
19 | #include <Expr_Difference.hxx> |
20 | #include <Expr_GeneralExpression.hxx> |
21 | #include <Expr_NamedUnknown.hxx> |
22 | #include <Expr_NotEvaluable.hxx> |
7fd59977 |
23 | #include <Expr_NumericValue.hxx> |
7fd59977 |
24 | #include <Expr_Operators.hxx> |
42cf5bc1 |
25 | #include <Expr_Sum.hxx> |
26 | #include <Expr_UnaryMinus.hxx> |
27 | #include <Standard_NumericError.hxx> |
7fd59977 |
28 | #include <Standard_OutOfRange.hxx> |
42cf5bc1 |
29 | #include <Standard_Type.hxx> |
30 | #include <TCollection_AsciiString.hxx> |
7fd59977 |
31 | |
92efcf78 |
32 | IMPLEMENT_STANDARD_RTTIEXT(Expr_Difference,Expr_BinaryExpression) |
33 | |
7fd59977 |
34 | Expr_Difference::Expr_Difference (const Handle(Expr_GeneralExpression)& exp1, const Handle(Expr_GeneralExpression)& exp2) |
35 | { |
36 | CreateFirstOperand(exp1); |
37 | CreateSecondOperand(exp2); |
38 | } |
39 | |
40 | |
41 | Handle(Expr_GeneralExpression) Expr_Difference::ShallowSimplified() const |
42 | { |
43 | Handle(Expr_GeneralExpression) myfirst = FirstOperand(); |
44 | Handle(Expr_GeneralExpression) mysecond = SecondOperand(); |
45 | |
46 | Standard_Boolean nvfirst = myfirst->IsKind(STANDARD_TYPE(Expr_NumericValue)); |
47 | Standard_Boolean nvsecond = mysecond->IsKind(STANDARD_TYPE(Expr_NumericValue)); |
48 | if (nvfirst && nvsecond) { |
49 | // case num1 - num2 |
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()); |
53 | } |
54 | if (nvfirst && !nvsecond) { |
55 | // case num1 - X2 |
56 | Handle(Expr_NumericValue) myNVfirst = Handle(Expr_NumericValue)::DownCast(myfirst); |
57 | if (myNVfirst->GetValue() == 0.0) { |
58 | // case 0 - X2 |
59 | return - mysecond; |
60 | } |
61 | } |
62 | if (!nvfirst && nvsecond) { |
63 | // case X1 - num2 |
64 | Handle(Expr_NumericValue) myNVsecond = Handle(Expr_NumericValue)::DownCast(mysecond); |
65 | if (myNVsecond->GetValue() == 0.0) { |
66 | // case X1 - 0 |
67 | return myfirst; |
68 | } |
69 | } |
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); |
77 | return ssop2 - ssop1; |
78 | } |
79 | if (unfirst && !unsecond) { |
80 | // case (-ssX1) - X2 = -( ssX1 + X2) |
81 | Handle(Expr_GeneralExpression) ssop1 = myfirst->SubExpression(1); |
82 | return -(ssop1 + mysecond); |
83 | } |
84 | if (!unfirst && unsecond) { |
85 | // case X1 - (-ssX2) = X1 + ssX2 |
86 | Handle(Expr_GeneralExpression) ssop2 = mysecond->SubExpression(1); |
87 | return myfirst + ssop2; |
88 | } |
89 | Handle(Expr_Difference) me = this; |
90 | return me; |
91 | } |
92 | |
93 | Handle(Expr_GeneralExpression) Expr_Difference::Copy () const |
94 | { |
95 | return Expr::CopyShare(FirstOperand()) - Expr::CopyShare(SecondOperand()); |
96 | } |
97 | |
98 | Standard_Boolean Expr_Difference::IsIdentical (const Handle(Expr_GeneralExpression)& Other) const |
99 | { |
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; |
110 | } |
111 | } |
112 | return ident; |
113 | } |
114 | |
115 | Standard_Boolean Expr_Difference::IsLinear () const |
116 | { |
117 | Handle(Expr_GeneralExpression) myfirst = FirstOperand(); |
118 | Handle(Expr_GeneralExpression) mysecond = SecondOperand(); |
119 | return (myfirst->IsLinear() && mysecond->IsLinear()); |
120 | } |
121 | |
122 | Handle(Expr_GeneralExpression) Expr_Difference::Derivative (const Handle(Expr_NamedUnknown)& X) const |
123 | { |
124 | if (!Contains(X)) { |
125 | return new Expr_NumericValue(0.0); |
126 | } |
127 | Handle(Expr_GeneralExpression) myfirst = FirstOperand(); |
128 | Handle(Expr_GeneralExpression) mysecond = SecondOperand(); |
129 | |
130 | myfirst = myfirst->Derivative(X); |
131 | mysecond = mysecond->Derivative(X); |
132 | Handle(Expr_Difference) der = myfirst - mysecond; |
133 | return der->ShallowSimplified(); |
134 | } |
135 | |
136 | Handle(Expr_GeneralExpression) Expr_Difference::NDerivative (const Handle(Expr_NamedUnknown)& X, const Standard_Integer N) const |
137 | { |
138 | if (N <= 0) { |
139 | Standard_OutOfRange::Raise(); |
140 | } |
141 | if (!Contains(X)) { |
142 | return new Expr_NumericValue(0.0); |
143 | } |
144 | Handle(Expr_GeneralExpression) myfirst = FirstOperand(); |
145 | Handle(Expr_GeneralExpression) mysecond = SecondOperand(); |
146 | |
147 | myfirst = myfirst->NDerivative(X,N); |
148 | mysecond = mysecond->NDerivative(X,N); |
149 | Handle(Expr_Difference) der = myfirst - mysecond; |
150 | return der->ShallowSimplified(); |
151 | |
152 | } |
153 | |
154 | |
155 | Standard_Real Expr_Difference::Evaluate(const Expr_Array1OfNamedUnknown& vars, const TColStd_Array1OfReal& vals) const |
156 | { |
157 | Standard_Real res = FirstOperand()->Evaluate(vars,vals); |
158 | return res - SecondOperand()->Evaluate(vars,vals); |
159 | } |
160 | |
161 | TCollection_AsciiString Expr_Difference::String() const |
162 | { |
163 | Handle(Expr_GeneralExpression) op1 = FirstOperand(); |
164 | Handle(Expr_GeneralExpression) op2 = SecondOperand(); |
165 | TCollection_AsciiString str; |
166 | if (op1->NbSubExpressions() > 1) { |
167 | str += "("; |
168 | str += op1->String(); |
169 | str += ")"; |
170 | } |
171 | else { |
172 | str = op1->String(); |
173 | } |
174 | str += "-"; |
175 | if (op2->NbSubExpressions() > 1) { |
176 | str += "("; |
177 | str += op2->String(); |
178 | str += ")"; |
179 | } |
180 | else { |
181 | str += op2->String(); |
182 | } |
183 | return str; |
184 | } |