0026936: Drawbacks of inlining in new type system in OCCT 7.0 -- automatic
[occt.git] / src / Expr / Expr_UnaryFunction.cxx
1 // Created on: 1991-05-28
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 #ifndef OCCT_DEBUG
18 #define No_Standard_RangeError
19 #define No_Standard_OutOfRange
20 #endif
21
22
23 #include <Expr.hxx>
24 #include <Expr_Array1OfGeneralExpression.hxx>
25 #include <Expr_Array1OfNamedUnknown.hxx>
26 #include <Expr_FunctionDerivative.hxx>
27 #include <Expr_GeneralExpression.hxx>
28 #include <Expr_GeneralFunction.hxx>
29 #include <Expr_InvalidFunction.hxx>
30 #include <Expr_NamedUnknown.hxx>
31 #include <Expr_NotEvaluable.hxx>
32 #include <Expr_NumericValue.hxx>
33 #include <Expr_Operators.hxx>
34 #include <Expr_Product.hxx>
35 #include <Expr_UnaryFunction.hxx>
36 #include <Standard_NumericError.hxx>
37 #include <Standard_Type.hxx>
38 #include <TCollection_AsciiString.hxx>
39
40 IMPLEMENT_STANDARD_RTTIEXT(Expr_UnaryFunction,Expr_UnaryExpression)
41
42 Expr_UnaryFunction::Expr_UnaryFunction (const Handle(Expr_GeneralFunction)& func, const Handle(Expr_GeneralExpression)& exp)
43 {
44   if (func->NbOfVariables() != 1) {
45     Expr_InvalidFunction::Raise();
46   }
47   myFunction = func;
48   CreateOperand(exp);
49 }
50
51 Handle(Expr_GeneralFunction) Expr_UnaryFunction::Function () const
52 {
53   return myFunction;
54 }
55
56 Handle(Expr_GeneralExpression) Expr_UnaryFunction::ShallowSimplified () const
57 {
58   Handle(Expr_GeneralExpression) op = Operand();
59   if (op->IsKind(STANDARD_TYPE(Expr_NumericValue))) {
60     Handle(Expr_NumericValue) nval = Handle(Expr_NumericValue)::DownCast(op);
61     TColStd_Array1OfReal tabval(1,1);
62     tabval(1) = nval->GetValue();
63     Expr_Array1OfNamedUnknown vars(1,1);
64     vars(1) = myFunction->Variable(1);
65     Standard_Real res = myFunction->Evaluate(vars,tabval);
66     return new Expr_NumericValue(res);
67   }
68   Handle(Expr_UnaryFunction) me = this;
69   return me;
70 }
71
72 Handle(Expr_GeneralExpression) Expr_UnaryFunction::Copy () const
73 {
74   return new Expr_UnaryFunction(myFunction,Expr::CopyShare(Operand()));
75 }
76
77 Standard_Boolean Expr_UnaryFunction::IsIdentical (const Handle(Expr_GeneralExpression)& Other) const
78 {
79   if (!Other->IsKind(STANDARD_TYPE(Expr_UnaryFunction))) {
80     return Standard_False;
81   }
82   Handle(Expr_UnaryFunction) fother = Handle(Expr_UnaryFunction)::DownCast(Other);
83   Handle(Expr_GeneralExpression) otherexp = fother->Operand();
84   if (otherexp->IsIdentical(Operand())) {
85     if (myFunction->IsIdentical(fother->Function())) {
86       return Standard_True;
87     }
88   }
89   return Standard_False;
90 }
91
92 Standard_Boolean Expr_UnaryFunction::IsLinear () const
93 {
94   if (!ContainsUnknowns()) {
95     return Standard_True;
96   }
97   if (!Operand()->IsLinear()) {
98     return Standard_False;
99   }
100   return myFunction->IsLinearOnVariable(1);
101 }
102
103 Handle(Expr_GeneralExpression) Expr_UnaryFunction::Derivative (const Handle(Expr_NamedUnknown)& X) const
104 {
105   Handle(Expr_NamedUnknown) myvar = myFunction->Variable(1);
106   Handle(Expr_GeneralExpression) myop = Operand();
107   Handle(Expr_GeneralExpression) myexpder = myop->Derivative(X);
108   Handle(Expr_GeneralFunction) myfuncder = myFunction->Derivative(myvar);
109   Handle(Expr_UnaryFunction) firstpart = new Expr_UnaryFunction(myfuncder,Expr::CopyShare(myop));
110   Handle(Expr_Product) resu = firstpart->ShallowSimplified() * myexpder;
111   return resu->ShallowSimplified();
112 }
113
114 Standard_Real Expr_UnaryFunction::Evaluate(const Expr_Array1OfNamedUnknown& vars, const TColStd_Array1OfReal& vals) const
115 {
116   Expr_Array1OfNamedUnknown varsfunc(1,1);
117   varsfunc(1) = myFunction->Variable(1);
118   TColStd_Array1OfReal valsfunc(1,1);
119   valsfunc(1) = Operand()->Evaluate(vars,vals);
120   return myFunction->Evaluate(varsfunc,valsfunc);
121 }
122
123 TCollection_AsciiString Expr_UnaryFunction::String() const
124 {
125   TCollection_AsciiString res = myFunction->GetStringName();
126   res += "(";
127   res += Operand()->String();
128   res += ")";;
129   return res;
130 }