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