b311480e |
1 | // Created on: 1991-04-19 |
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> |
7fd59977 |
19 | #include <Expr_Exponentiate.hxx> |
42cf5bc1 |
20 | #include <Expr_GeneralExpression.hxx> |
21 | #include <Expr_NamedUnknown.hxx> |
22 | #include <Expr_NotEvaluable.hxx> |
23 | #include <Expr_NumericValue.hxx> |
7fd59977 |
24 | #include <Expr_Operators.hxx> |
42cf5bc1 |
25 | #include <Expr_Product.hxx> |
26 | #include <Expr_SequenceOfGeneralExpression.hxx> |
27 | #include <Expr_Square.hxx> |
28 | #include <Expr_SquareRoot.hxx> |
29 | #include <Standard_NumericError.hxx> |
30 | #include <Standard_Type.hxx> |
31 | #include <TCollection_AsciiString.hxx> |
7fd59977 |
32 | |
92efcf78 |
33 | IMPLEMENT_STANDARD_RTTIEXT(Expr_Square,Expr_UnaryExpression) |
34 | |
7fd59977 |
35 | Expr_Square::Expr_Square (const Handle(Expr_GeneralExpression)& exp) |
36 | { |
37 | CreateOperand(exp); |
38 | } |
39 | |
40 | Handle(Expr_GeneralExpression) Expr_Square::ShallowSimplified () const |
41 | { |
42 | Handle(Expr_GeneralExpression) myexp = Operand(); |
43 | if (myexp->IsKind(STANDARD_TYPE(Expr_NumericValue))) { |
44 | Handle(Expr_NumericValue) myNVexp = Handle(Expr_NumericValue)::DownCast(myexp); |
45 | return new Expr_NumericValue(Square(myNVexp->GetValue())); |
46 | } |
47 | if (myexp->IsKind(STANDARD_TYPE(Expr_SquareRoot))) { |
48 | return myexp->SubExpression(1); |
49 | } |
50 | if (myexp->IsKind(STANDARD_TYPE(Expr_Square))) { |
51 | Handle(Expr_GeneralExpression) op = myexp->SubExpression(1); |
52 | Handle(Expr_NumericValue) val4 = new Expr_NumericValue(4.0); |
53 | return new Expr_Exponentiate(op,val4); |
54 | } |
55 | if (myexp->IsKind(STANDARD_TYPE(Expr_Exponentiate))) { |
56 | Handle(Expr_GeneralExpression) op = myexp->SubExpression(1); |
57 | Handle(Expr_GeneralExpression) puis = myexp->SubExpression(2); |
58 | Handle(Expr_Product) newpuis = 2.0 * puis; |
59 | Handle(Expr_Exponentiate) res = new Expr_Exponentiate(op,newpuis->ShallowSimplified()); |
60 | return res->ShallowSimplified(); |
61 | } |
62 | Handle(Expr_Square) me = this; |
63 | return me; |
64 | } |
65 | |
66 | Handle(Expr_GeneralExpression) Expr_Square::Copy () const |
67 | { |
68 | return new Expr_Square(Expr::CopyShare(Operand())); |
69 | } |
70 | |
71 | Standard_Boolean Expr_Square::IsIdentical (const Handle(Expr_GeneralExpression)& Other) const |
72 | { |
73 | if (Other->IsKind(STANDARD_TYPE(Expr_Square))) { |
74 | return Operand()->IsIdentical(Other->SubExpression(1)); |
75 | } |
76 | return Standard_False; |
77 | } |
78 | |
79 | Standard_Boolean Expr_Square::IsLinear () const |
80 | { |
81 | return !ContainsUnknowns(); |
82 | } |
83 | |
84 | Handle(Expr_GeneralExpression) Expr_Square::Derivative (const Handle(Expr_NamedUnknown)& X) const |
85 | { |
86 | if (!Contains(X)) { |
87 | return new Expr_NumericValue(0.0); |
88 | } |
89 | Handle(Expr_GeneralExpression) myder = Operand(); |
90 | myder = myder->Derivative(X); |
91 | Handle(Expr_NumericValue) coef = new Expr_NumericValue(2.0); |
92 | Expr_SequenceOfGeneralExpression ops; |
93 | ops.Append(coef); |
94 | ops.Append(myder); |
95 | Handle(Expr_GeneralExpression) usedop = Expr::CopyShare(Operand()); |
96 | ops.Append(usedop); |
97 | Handle(Expr_Product) resu = new Expr_Product(ops); |
98 | return resu->ShallowSimplified(); |
99 | } |
100 | |
101 | Standard_Real Expr_Square::Evaluate(const Expr_Array1OfNamedUnknown& vars, const TColStd_Array1OfReal& vals) const |
102 | { |
103 | Standard_Real val = Operand()->Evaluate(vars,vals); |
104 | return val*val; |
105 | } |
106 | |
107 | TCollection_AsciiString Expr_Square::String() const |
108 | { |
109 | TCollection_AsciiString str; |
110 | Handle(Expr_GeneralExpression) op = Operand(); |
111 | if (op->NbSubExpressions() > 1) { |
112 | str = "("; |
113 | str += op->String(); |
114 | str += ")^2"; |
115 | } |
116 | else { |
117 | str = op->String(); |
118 | str += "^2"; |
119 | } |
120 | return str; |
121 | } |