b311480e |
1 | // Created on: 1991-06-27 |
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 | |
42cf5bc1 |
17 | |
18 | #include <Expr_FunctionDerivative.hxx> |
19 | #include <Expr_GeneralExpression.hxx> |
20 | #include <Expr_GeneralFunction.hxx> |
7fd59977 |
21 | #include <Expr_NamedFunction.hxx> |
42cf5bc1 |
22 | #include <Expr_NamedUnknown.hxx> |
23 | #include <Expr_NotEvaluable.hxx> |
24 | #include <Expr_NumericValue.hxx> |
25 | #include <Standard_DimensionMismatch.hxx> |
7fd59977 |
26 | #include <Standard_NotImplemented.hxx> |
42cf5bc1 |
27 | #include <Standard_NumericError.hxx> |
28 | #include <Standard_OutOfRange.hxx> |
29 | #include <Standard_Type.hxx> |
30 | #include <TCollection_AsciiString.hxx> |
7fd59977 |
31 | |
92efcf78 |
32 | IMPLEMENT_STANDARD_RTTIEXT(Expr_FunctionDerivative,Expr_GeneralFunction) |
33 | |
7fd59977 |
34 | Expr_FunctionDerivative::Expr_FunctionDerivative (const Handle(Expr_GeneralFunction)& func, const Handle(Expr_NamedUnknown)& withX, const Standard_Integer deg) |
35 | { |
36 | myFunction = func; |
37 | myDerivate = withX; |
38 | if (deg <= 0) { |
39 | Standard_OutOfRange::Raise(); |
40 | } |
41 | myDegree = deg; |
42 | UpdateExpression(); |
43 | } |
44 | |
45 | Standard_Integer Expr_FunctionDerivative::NbOfVariables () const |
46 | { |
47 | return myFunction->NbOfVariables(); |
48 | } |
49 | |
50 | Handle(Expr_NamedUnknown) Expr_FunctionDerivative::Variable (const Standard_Integer index) const |
51 | { |
52 | return myFunction->Variable(index); |
53 | } |
54 | |
55 | Standard_Real Expr_FunctionDerivative::Evaluate (const Expr_Array1OfNamedUnknown& vars, const TColStd_Array1OfReal& values) const |
56 | { |
57 | if (vars.Length() != values.Length()) { |
58 | Standard_OutOfRange::Raise(); |
59 | } |
60 | return myExp->Evaluate(vars,values); |
61 | } |
62 | |
63 | Handle(Expr_GeneralFunction) Expr_FunctionDerivative::Copy () const |
64 | { |
65 | return new Expr_FunctionDerivative(myFunction->Copy(),myDerivate,myDegree); |
66 | } |
67 | |
68 | Handle(Expr_GeneralFunction) Expr_FunctionDerivative::Derivative(const Handle(Expr_NamedUnknown)& var) const |
69 | { |
70 | return Derivative(var,1); |
71 | } |
72 | |
73 | Handle(Expr_GeneralFunction) Expr_FunctionDerivative::Derivative(const Handle(Expr_NamedUnknown)& var, const Standard_Integer deg) const |
74 | { |
75 | if (var == myDerivate) { |
76 | return new Expr_FunctionDerivative(myFunction,var,myDegree+deg); |
77 | } |
78 | Handle(Expr_FunctionDerivative) me = this; |
79 | return new Expr_FunctionDerivative(me,var,deg); |
80 | } |
81 | |
82 | Standard_Boolean Expr_FunctionDerivative::IsIdentical (const Handle(Expr_GeneralFunction)& func) const |
83 | { |
84 | if (!func->IsKind(STANDARD_TYPE(Expr_FunctionDerivative))) { |
85 | return Standard_False; |
86 | } |
87 | Handle(Expr_FunctionDerivative) dfunc = Handle(Expr_FunctionDerivative)::DownCast(func); |
88 | if (myDegree != dfunc->Degree()) { |
89 | return Standard_False; |
90 | } |
91 | if (!myDerivate->IsIdentical(dfunc->DerivVariable())) { |
92 | return Standard_False; |
93 | } |
94 | if (!myFunction->IsIdentical(dfunc->Function())) { |
95 | return Standard_False; |
96 | } |
97 | return Standard_True; |
98 | } |
99 | |
100 | Standard_Boolean Expr_FunctionDerivative::IsLinearOnVariable(const Standard_Integer) const |
101 | { |
102 | // should be improved |
103 | return myExp->IsLinear(); |
104 | } |
105 | |
106 | Handle(Expr_GeneralFunction) Expr_FunctionDerivative::Function () const |
107 | { |
108 | return myFunction; |
109 | } |
110 | |
111 | Standard_Integer Expr_FunctionDerivative::Degree () const |
112 | { |
113 | return myDegree; |
114 | } |
115 | |
116 | Handle(Expr_NamedUnknown) Expr_FunctionDerivative::DerivVariable () const |
117 | { |
118 | return myDerivate; |
119 | } |
120 | |
121 | |
122 | TCollection_AsciiString Expr_FunctionDerivative::GetStringName() const |
123 | { |
124 | TCollection_AsciiString res; |
125 | if (NbOfVariables() ==1) { |
126 | res = myFunction->GetStringName(); |
127 | char c = 39; |
128 | TCollection_AsciiString diff(myDegree,c); |
129 | res += diff; |
130 | return res; |
131 | } |
132 | TCollection_AsciiString diff("@"); |
133 | if (myDegree > 1) { |
134 | TCollection_AsciiString deg(myDegree); |
135 | diff += deg; |
136 | } |
137 | res = diff; |
138 | res += myFunction->GetStringName(); |
139 | res += "/"; |
140 | Standard_Integer index=0; |
141 | for (Standard_Integer i=1; (i<= NbOfVariables()) && (index ==0) ; i++) { |
142 | if (Variable(i) == myDerivate) { |
143 | index =i; |
144 | } |
145 | } |
146 | res += diff; |
147 | res += "X"; |
148 | TCollection_AsciiString rank(index); |
149 | res += rank; |
150 | return res; |
151 | } |
152 | |
153 | |
154 | Handle(Expr_GeneralExpression) Expr_FunctionDerivative::Expression() const |
155 | { |
156 | return myExp; |
157 | } |
158 | |
159 | void Expr_FunctionDerivative::UpdateExpression() |
160 | { |
161 | if (myFunction->IsKind(STANDARD_TYPE(Expr_FunctionDerivative))) { |
162 | Handle(Expr_FunctionDerivative) defunc = Handle(Expr_FunctionDerivative)::DownCast(myFunction); |
163 | defunc->UpdateExpression(); |
164 | myExp = defunc->Expression()->NDerivative(myDerivate,myDegree); |
165 | } |
166 | else { |
167 | Handle(Expr_NamedFunction) nafunc = Handle(Expr_NamedFunction)::DownCast(myFunction); |
168 | myExp = nafunc->Expression()->NDerivative(myDerivate,myDegree); |
169 | } |
170 | } |