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