0db4248a98533535753dfe8d5d9be70a30d99521
[occt.git] / src / Expr / Expr_Division.cxx
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
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
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.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <Expr_Division.ixx>
18 #include <Expr_Product.hxx>
19 #include <Expr_Square.hxx>
20 #include <Expr_UnaryMinus.hxx>
21 #include <Expr_Difference.hxx>
22 #include <Expr_NumericValue.hxx>
23 #include <Expr_Operators.hxx>
24 #include <Expr.hxx>
25
26 Expr_Division::Expr_Division (const Handle(Expr_GeneralExpression)& exp1, const Handle(Expr_GeneralExpression)& exp2)
27 {
28   CreateFirstOperand(exp1);
29   CreateSecondOperand(exp2);
30 }
31
32 Handle(Expr_GeneralExpression) Expr_Division::Copy () const
33 {
34   return Expr::CopyShare(FirstOperand()) / Expr::CopyShare(SecondOperand());
35 }
36   
37 Standard_Boolean Expr_Division::IsIdentical (const Handle(Expr_GeneralExpression)& Other) const
38 {
39   Standard_Boolean ident = Standard_False;
40   if (Other->IsKind(STANDARD_TYPE(Expr_Division))) {
41     Handle(Expr_GeneralExpression) myfirst = FirstOperand();
42     Handle(Expr_GeneralExpression) mysecond = SecondOperand();
43     Handle(Expr_Division) DOther = Handle(Expr_Division)::DownCast(Other);
44     Handle(Expr_GeneralExpression) fother = DOther->FirstOperand();
45     Handle(Expr_GeneralExpression) sother = DOther->SecondOperand();
46     if (myfirst->IsIdentical(fother) &&
47         mysecond->IsIdentical(sother)) {
48       ident = Standard_True;
49     }
50   }
51   return ident;
52 }
53
54 Standard_Boolean Expr_Division::IsLinear () const
55 {
56   Handle(Expr_GeneralExpression) myfirst = FirstOperand();
57   Handle(Expr_GeneralExpression) mysecond = SecondOperand();
58   if (mysecond->IsKind(STANDARD_TYPE(Expr_NamedUnknown)) || mysecond->ContainsUnknowns()) {
59     return Standard_False;
60   }
61   return (myfirst->IsLinear() && mysecond->IsLinear());
62 }
63
64 Handle(Expr_GeneralExpression) Expr_Division::Derivative (const Handle(Expr_NamedUnknown)& X) const
65 {
66   if (!Contains(X)) {
67     return new Expr_NumericValue(0.0);
68   }
69   Handle(Expr_GeneralExpression) myfirst = FirstOperand();
70   Handle(Expr_GeneralExpression) mysecond = SecondOperand();
71   Handle(Expr_GeneralExpression) myfder = myfirst->Derivative(X);
72   Handle(Expr_GeneralExpression) mysder = mysecond->Derivative(X);
73
74   // "u'v"
75   Handle(Expr_Product) firstprod = myfder * Expr::CopyShare(mysecond);
76
77   Handle(Expr_GeneralExpression) firstsimp = firstprod->ShallowSimplified();
78   // "uv'"
79   Handle(Expr_Product) secondprod = Expr::CopyShare(myfirst) * mysder;
80   Handle(Expr_GeneralExpression) secondsimp = secondprod->ShallowSimplified();
81   // "u'v - uv'"
82   Handle(Expr_Difference) mynumer = firstsimp - secondsimp;
83
84   // " v2"
85   Handle(Expr_Square) mydenom = new Expr_Square(Expr::CopyShare(mysecond));
86
87   // result = "u'v-uv' / v2"
88
89   Handle(Expr_GeneralExpression) snumer = mynumer->ShallowSimplified();
90   Handle(Expr_GeneralExpression) sdenom = mydenom->ShallowSimplified();
91   Handle(Expr_Division) result = snumer / sdenom;
92
93   return result->ShallowSimplified();
94 }
95
96 Handle(Expr_GeneralExpression) Expr_Division::ShallowSimplified () const
97 {
98   Handle(Expr_GeneralExpression) myfirst = FirstOperand();
99   Handle(Expr_GeneralExpression) mysecond = SecondOperand();
100
101   if (myfirst->IsKind(STANDARD_TYPE(Expr_NumericValue))) {
102     Handle(Expr_NumericValue) myNVfirst = Handle(Expr_NumericValue)::DownCast(myfirst);
103     if (myNVfirst->GetValue() == 0.0) {
104       // case 0/X2
105       return new Expr_NumericValue(0.0);
106     }
107     if (mysecond->IsKind(STANDARD_TYPE(Expr_NumericValue))) {
108       // case num1/num2
109       Handle(Expr_NumericValue) myNVsecond = Handle(Expr_NumericValue)::DownCast(mysecond);
110       return new Expr_NumericValue(myNVfirst->GetValue()/myNVsecond->GetValue());
111     }
112   }
113   else {
114     if (mysecond->IsKind(STANDARD_TYPE(Expr_NumericValue))) {
115       // case X1/num2
116       Handle(Expr_NumericValue) myNVsecond = Handle(Expr_NumericValue)::DownCast(mysecond);
117       if (myNVsecond->GetValue() == 1.0) {
118         // case X1/1
119         return myfirst;
120       }
121     }
122   }
123   Handle(Expr_Division) me = this;
124   return me;
125 }
126
127 Standard_Real Expr_Division::Evaluate(const Expr_Array1OfNamedUnknown& vars, const TColStd_Array1OfReal& vals) const
128 {
129   Standard_Real res = FirstOperand()->Evaluate(vars,vals);
130   return res / SecondOperand()->Evaluate(vars,vals);
131 }
132
133 TCollection_AsciiString Expr_Division::String() const
134 {
135   Handle(Expr_GeneralExpression) op1 = FirstOperand();
136   Handle(Expr_GeneralExpression) op2 = SecondOperand();
137   TCollection_AsciiString str;
138   if (op1->NbSubExpressions() > 1) {
139     str = "(";
140     str += op1->String();
141     str += ")";
142   }
143   else {
144     str = op1->String();
145   }
146   str += "/";
147   if (op2->NbSubExpressions() > 1) {
148     str += "(";
149     str += op2->String();
150     str += ")";
151   }
152   else {
153     str += op2->String();
154   }
155   return str;
156 }