0025418: Debug output to be limited to OCC development environment
[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 #include <Expr_UnaryFunction.ixx>
23 #include <Expr_InvalidFunction.hxx>
24 #include <Expr_Array1OfNamedUnknown.hxx>
25 #include <Expr_Array1OfGeneralExpression.hxx>
26 #include <Expr_FunctionDerivative.hxx>
27 #include <Expr_Product.hxx>
28 #include <Expr_Operators.hxx>
29 #include <Expr.hxx>
30
31 Expr_UnaryFunction::Expr_UnaryFunction (const Handle(Expr_GeneralFunction)& func, const Handle(Expr_GeneralExpression)& exp)
32 {
33   if (func->NbOfVariables() != 1) {
34     Expr_InvalidFunction::Raise();
35   }
36   myFunction = func;
37   CreateOperand(exp);
38 }
39
40 Handle(Expr_GeneralFunction) Expr_UnaryFunction::Function () const
41 {
42   return myFunction;
43 }
44
45 Handle(Expr_GeneralExpression) Expr_UnaryFunction::ShallowSimplified () const
46 {
47   Handle(Expr_GeneralExpression) op = Operand();
48   if (op->IsKind(STANDARD_TYPE(Expr_NumericValue))) {
49     Handle(Expr_NumericValue) nval = Handle(Expr_NumericValue)::DownCast(op);
50     TColStd_Array1OfReal tabval(1,1);
51     tabval(1) = nval->GetValue();
52     Expr_Array1OfNamedUnknown vars(1,1);
53     vars(1) = myFunction->Variable(1);
54     Standard_Real res = myFunction->Evaluate(vars,tabval);
55     return new Expr_NumericValue(res);
56   }
57   Handle(Expr_UnaryFunction) me = this;
58   return me;
59 }
60
61 Handle(Expr_GeneralExpression) Expr_UnaryFunction::Copy () const
62 {
63   return new Expr_UnaryFunction(myFunction,Expr::CopyShare(Operand()));
64 }
65
66 Standard_Boolean Expr_UnaryFunction::IsIdentical (const Handle(Expr_GeneralExpression)& Other) const
67 {
68   if (!Other->IsKind(STANDARD_TYPE(Expr_UnaryFunction))) {
69     return Standard_False;
70   }
71   Handle(Expr_UnaryFunction) fother = Handle(Expr_UnaryFunction)::DownCast(Other);
72   Handle(Expr_GeneralExpression) otherexp = fother->Operand();
73   if (otherexp->IsIdentical(Operand())) {
74     if (myFunction->IsIdentical(fother->Function())) {
75       return Standard_True;
76     }
77   }
78   return Standard_False;
79 }
80
81 Standard_Boolean Expr_UnaryFunction::IsLinear () const
82 {
83   if (!ContainsUnknowns()) {
84     return Standard_True;
85   }
86   if (!Operand()->IsLinear()) {
87     return Standard_False;
88   }
89   return myFunction->IsLinearOnVariable(1);
90 }
91
92 Handle(Expr_GeneralExpression) Expr_UnaryFunction::Derivative (const Handle(Expr_NamedUnknown)& X) const
93 {
94   Handle(Expr_NamedUnknown) myvar = myFunction->Variable(1);
95   Handle(Expr_GeneralExpression) myop = Operand();
96   Handle(Expr_GeneralExpression) myexpder = myop->Derivative(X);
97   Handle(Expr_GeneralFunction) myfuncder = myFunction->Derivative(myvar);
98   Handle(Expr_UnaryFunction) firstpart = new Expr_UnaryFunction(myfuncder,Expr::CopyShare(myop));
99   Handle(Expr_Product) resu = firstpart->ShallowSimplified() * myexpder;
100   return resu->ShallowSimplified();
101 }
102
103 Standard_Real Expr_UnaryFunction::Evaluate(const Expr_Array1OfNamedUnknown& vars, const TColStd_Array1OfReal& vals) const
104 {
105   Expr_Array1OfNamedUnknown varsfunc(1,1);
106   varsfunc(1) = myFunction->Variable(1);
107   TColStd_Array1OfReal valsfunc(1,1);
108   valsfunc(1) = Operand()->Evaluate(vars,vals);
109   return myFunction->Evaluate(varsfunc,valsfunc);
110 }
111
112 TCollection_AsciiString Expr_UnaryFunction::String() const
113 {
114   TCollection_AsciiString res = myFunction->GetStringName();
115   res += "(";
116   res += Operand()->String();
117   res += ")";;
118   return res;
119 }