6684f8eb25fcd5a43922d74526f528f68eea0e9b
[occt.git] / src / Expr / Expr_Square.cxx
1 //static const char* sccsid = "@(#)Expr_Square.cxx      3.2 95/01/10"; // Do not delete this line. Used by sccs.
2 // Copyright:   Matra-Datavision 1991
3 // File:        Expr_Square.cxx
4 // Created:     Fri Apr 19 10:05:26 1991
5 // Author:      Arnaud BOUZY
6 //              <adn>
7
8 #include <Expr_Square.ixx>
9 #include <Expr_NumericValue.hxx>
10 #include <Expr_SquareRoot.hxx>
11 #include <Expr_SequenceOfGeneralExpression.hxx>
12 #include <Expr_Product.hxx>
13 #include <Expr_Exponentiate.hxx>
14 #include <Expr_Operators.hxx>
15 #include <Expr.hxx>
16
17 Expr_Square::Expr_Square (const Handle(Expr_GeneralExpression)& exp)
18 {
19   CreateOperand(exp);
20 }
21
22 Handle(Expr_GeneralExpression) Expr_Square::ShallowSimplified () const
23 {
24   Handle(Expr_GeneralExpression) myexp = Operand();
25   if (myexp->IsKind(STANDARD_TYPE(Expr_NumericValue))) {
26     Handle(Expr_NumericValue) myNVexp = Handle(Expr_NumericValue)::DownCast(myexp);
27     return new Expr_NumericValue(Square(myNVexp->GetValue()));
28   }
29   if (myexp->IsKind(STANDARD_TYPE(Expr_SquareRoot))) {
30     return myexp->SubExpression(1);
31   }
32   if (myexp->IsKind(STANDARD_TYPE(Expr_Square))) {
33     Handle(Expr_GeneralExpression) op = myexp->SubExpression(1);
34     Handle(Expr_NumericValue) val4 = new Expr_NumericValue(4.0);
35     return new Expr_Exponentiate(op,val4);
36   }
37   if (myexp->IsKind(STANDARD_TYPE(Expr_Exponentiate))) {
38     Handle(Expr_GeneralExpression) op = myexp->SubExpression(1);
39     Handle(Expr_GeneralExpression) puis = myexp->SubExpression(2);
40     Handle(Expr_Product) newpuis = 2.0 * puis;
41     Handle(Expr_Exponentiate) res = new Expr_Exponentiate(op,newpuis->ShallowSimplified());
42     return res->ShallowSimplified();
43   }
44   Handle(Expr_Square) me = this;
45   return me;
46 }
47
48 Handle(Expr_GeneralExpression) Expr_Square::Copy () const
49 {
50   return new Expr_Square(Expr::CopyShare(Operand()));
51 }
52
53 Standard_Boolean Expr_Square::IsIdentical (const Handle(Expr_GeneralExpression)& Other) const
54 {
55   if (Other->IsKind(STANDARD_TYPE(Expr_Square))) {
56     return Operand()->IsIdentical(Other->SubExpression(1));
57   }
58   return Standard_False;
59 }
60
61 Standard_Boolean Expr_Square::IsLinear () const
62 {
63   return !ContainsUnknowns();
64 }
65
66 Handle(Expr_GeneralExpression) Expr_Square::Derivative (const Handle(Expr_NamedUnknown)& X) const
67 {
68   if (!Contains(X)) {
69     return  new Expr_NumericValue(0.0);
70   }
71   Handle(Expr_GeneralExpression) myder = Operand();
72   myder = myder->Derivative(X);
73   Handle(Expr_NumericValue) coef = new Expr_NumericValue(2.0);
74   Expr_SequenceOfGeneralExpression ops;
75   ops.Append(coef);
76   ops.Append(myder);
77   Handle(Expr_GeneralExpression) usedop = Expr::CopyShare(Operand());
78   ops.Append(usedop);
79   Handle(Expr_Product) resu = new Expr_Product(ops);
80   return resu->ShallowSimplified();
81 }
82
83 Standard_Real Expr_Square::Evaluate(const Expr_Array1OfNamedUnknown& vars, const TColStd_Array1OfReal& vals) const
84 {
85   Standard_Real val = Operand()->Evaluate(vars,vals);
86   return val*val;
87 }
88
89 TCollection_AsciiString Expr_Square::String() const
90 {
91   TCollection_AsciiString str;
92   Handle(Expr_GeneralExpression) op = Operand();
93   if (op->NbSubExpressions() > 1) {
94     str = "(";
95     str += op->String();
96     str += ")^2";
97   }
98   else {
99     str = op->String();
100     str += "^2";
101   }
102   return str;
103 }