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.
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>
26 Expr_Division::Expr_Division (const Handle(Expr_GeneralExpression)& exp1, const Handle(Expr_GeneralExpression)& exp2)
28 CreateFirstOperand(exp1);
29 CreateSecondOperand(exp2);
32 Handle(Expr_GeneralExpression) Expr_Division::Copy () const
34 return Expr::CopyShare(FirstOperand()) / Expr::CopyShare(SecondOperand());
37 Standard_Boolean Expr_Division::IsIdentical (const Handle(Expr_GeneralExpression)& Other) const
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;
54 Standard_Boolean Expr_Division::IsLinear () const
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;
61 return (myfirst->IsLinear() && mysecond->IsLinear());
64 Handle(Expr_GeneralExpression) Expr_Division::Derivative (const Handle(Expr_NamedUnknown)& X) const
67 return new Expr_NumericValue(0.0);
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);
75 Handle(Expr_Product) firstprod = myfder * Expr::CopyShare(mysecond);
77 Handle(Expr_GeneralExpression) firstsimp = firstprod->ShallowSimplified();
79 Handle(Expr_Product) secondprod = Expr::CopyShare(myfirst) * mysder;
80 Handle(Expr_GeneralExpression) secondsimp = secondprod->ShallowSimplified();
82 Handle(Expr_Difference) mynumer = firstsimp - secondsimp;
85 Handle(Expr_Square) mydenom = new Expr_Square(Expr::CopyShare(mysecond));
87 // result = "u'v-uv' / v2"
89 Handle(Expr_GeneralExpression) snumer = mynumer->ShallowSimplified();
90 Handle(Expr_GeneralExpression) sdenom = mydenom->ShallowSimplified();
91 Handle(Expr_Division) result = snumer / sdenom;
93 return result->ShallowSimplified();
96 Handle(Expr_GeneralExpression) Expr_Division::ShallowSimplified () const
98 Handle(Expr_GeneralExpression) myfirst = FirstOperand();
99 Handle(Expr_GeneralExpression) mysecond = SecondOperand();
101 if (myfirst->IsKind(STANDARD_TYPE(Expr_NumericValue))) {
102 Handle(Expr_NumericValue) myNVfirst = Handle(Expr_NumericValue)::DownCast(myfirst);
103 if (myNVfirst->GetValue() == 0.0) {
105 return new Expr_NumericValue(0.0);
107 if (mysecond->IsKind(STANDARD_TYPE(Expr_NumericValue))) {
109 Handle(Expr_NumericValue) myNVsecond = Handle(Expr_NumericValue)::DownCast(mysecond);
110 return new Expr_NumericValue(myNVfirst->GetValue()/myNVsecond->GetValue());
114 if (mysecond->IsKind(STANDARD_TYPE(Expr_NumericValue))) {
116 Handle(Expr_NumericValue) myNVsecond = Handle(Expr_NumericValue)::DownCast(mysecond);
117 if (myNVsecond->GetValue() == 1.0) {
123 Handle(Expr_Division) me = this;
127 Standard_Real Expr_Division::Evaluate(const Expr_Array1OfNamedUnknown& vars, const TColStd_Array1OfReal& vals) const
129 Standard_Real res = FirstOperand()->Evaluate(vars,vals);
130 return res / SecondOperand()->Evaluate(vars,vals);
133 TCollection_AsciiString Expr_Division::String() const
135 Handle(Expr_GeneralExpression) op1 = FirstOperand();
136 Handle(Expr_GeneralExpression) op2 = SecondOperand();
137 TCollection_AsciiString str;
138 if (op1->NbSubExpressions() > 1) {
140 str += op1->String();
147 if (op2->NbSubExpressions() > 1) {
149 str += op2->String();
153 str += op2->String();