0026936: Drawbacks of inlining in new type system in OCCT 7.0 -- automatic
[occt.git] / src / Expr / Expr_UnaryMinus.cxx
1 // Created on: 1991-04-16
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
18 #include <Expr.hxx>
19 #include <Expr_GeneralExpression.hxx>
20 #include <Expr_NamedUnknown.hxx>
21 #include <Expr_NotEvaluable.hxx>
22 #include <Expr_NumericValue.hxx>
23 #include <Expr_Operators.hxx>
24 #include <Expr_UnaryMinus.hxx>
25 #include <Standard_NumericError.hxx>
26 #include <Standard_OutOfRange.hxx>
27 #include <Standard_Type.hxx>
28 #include <TCollection_AsciiString.hxx>
29
30 IMPLEMENT_STANDARD_RTTIEXT(Expr_UnaryMinus,Expr_UnaryExpression)
31
32 Expr_UnaryMinus::Expr_UnaryMinus(const Handle(Expr_GeneralExpression)& exp)
33 {
34   CreateOperand(exp);
35 }
36
37 Handle(Expr_GeneralExpression) Expr_UnaryMinus::ShallowSimplified () const
38 {
39   Handle(Expr_GeneralExpression) myexp = Operand();
40   if (myexp->IsKind(STANDARD_TYPE(Expr_NumericValue))) {
41     Handle(Expr_NumericValue) myNVexp = Handle(Expr_NumericValue)::DownCast(myexp);
42     return new Expr_NumericValue(-myNVexp->GetValue());
43   }
44   if (myexp->IsKind(STANDARD_TYPE(Expr_UnaryMinus))) {
45     return myexp->SubExpression(1);
46   }
47   Handle(Expr_UnaryMinus) me = this;
48   return me;
49 }
50
51 Handle(Expr_GeneralExpression) Expr_UnaryMinus::Copy () const
52 {
53   return -(Expr::CopyShare(Operand()));
54 }
55
56 Standard_Boolean Expr_UnaryMinus::IsIdentical (const Handle(Expr_GeneralExpression)& Other) const
57 {
58   if (Other->IsKind(STANDARD_TYPE(Expr_UnaryMinus))) {
59     Handle(Expr_GeneralExpression) myexp = Operand();
60     return myexp->IsIdentical(Other->SubExpression(1));
61   }
62   return Standard_False;
63 }
64
65 Standard_Boolean Expr_UnaryMinus::IsLinear () const
66 {
67   Handle(Expr_GeneralExpression) myexp = Operand();
68   return myexp->IsLinear();
69 }
70
71 Handle(Expr_GeneralExpression) Expr_UnaryMinus::Derivative (const Handle(Expr_NamedUnknown)& X) const
72 {
73   Handle(Expr_GeneralExpression) myder = Operand();
74   myder = myder->Derivative(X);
75   Handle(Expr_UnaryMinus) resu = - myder;
76   return resu->ShallowSimplified();
77 }
78
79 Handle(Expr_GeneralExpression) Expr_UnaryMinus::NDerivative (const Handle(Expr_NamedUnknown)& X, const Standard_Integer N) const
80 {
81   if (N <= 0) {
82     Standard_OutOfRange::Raise();
83   }
84   Handle(Expr_GeneralExpression) myder = Operand();
85   myder = myder->NDerivative(X,N);
86   Handle(Expr_UnaryMinus) resu = - myder;
87   return resu->ShallowSimplified();
88 }
89
90 Standard_Real Expr_UnaryMinus::Evaluate(const Expr_Array1OfNamedUnknown& vars, const TColStd_Array1OfReal& vals) const
91 {
92   return - Operand()->Evaluate(vars,vals);
93 }
94
95 TCollection_AsciiString Expr_UnaryMinus::String() const
96 {
97   TCollection_AsciiString str;
98   Handle(Expr_GeneralExpression) op = Operand();
99   if (op->NbSubExpressions() > 1) {
100     str = "-(";
101     str += op->String();
102     str += ")";
103   }
104   else {
105     str = "-";
106     str += op->String();
107   }
108   return str;
109 }