0031682: Visualization - Prs3d_ShadingAspect::SetTransparency() has no effect with...
[occt.git] / src / Expr / Expr_PolyFunction.cxx
CommitLineData
b311480e 1// Created on: 1991-07-02
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
0797d9d3 17#ifndef OCCT_DEBUG
7fd59977 18#define No_Standard_RangeError
19#define No_Standard_OutOfRange
20#endif
21
42cf5bc1 22
23#include <Expr.hxx>
7fd59977 24#include <Expr_Array1OfNamedUnknown.hxx>
25#include <Expr_FunctionDerivative.hxx>
42cf5bc1 26#include <Expr_GeneralExpression.hxx>
27#include <Expr_GeneralFunction.hxx>
28#include <Expr_NamedUnknown.hxx>
29#include <Expr_NotEvaluable.hxx>
30#include <Expr_NumericValue.hxx>
31#include <Expr_Operators.hxx>
32#include <Expr_PolyFunction.hxx>
7fd59977 33#include <Expr_Product.hxx>
34#include <Expr_Sum.hxx>
7fd59977 35#include <Standard_DimensionError.hxx>
42cf5bc1 36#include <Standard_NumericError.hxx>
37#include <Standard_OutOfRange.hxx>
38#include <Standard_Type.hxx>
39#include <TCollection_AsciiString.hxx>
7fd59977 40
92efcf78 41IMPLEMENT_STANDARD_RTTIEXT(Expr_PolyFunction,Expr_PolyExpression)
42
7fd59977 43Expr_PolyFunction::Expr_PolyFunction (const Handle(Expr_GeneralFunction)& func, const Expr_Array1OfGeneralExpression& exps)
44{
45 for (Standard_Integer i=exps.Lower();i <= exps.Upper(); i++) {
46 AddOperand(exps(i));
47 }
48 myFunction = func;
49}
50
51Handle(Expr_GeneralFunction) Expr_PolyFunction::Function () const
52{
53 return myFunction;
54}
55
56Handle(Expr_GeneralExpression) Expr_PolyFunction::ShallowSimplified () const
57{
58 Standard_Boolean allval = Standard_True;
59 Standard_Integer max = NbSubExpressions();
60 Standard_Integer i;
61 for (i = 1; (i <= max) && allval ; i++) {
62 allval = SubExpression(i)->IsKind(STANDARD_TYPE(Expr_NumericValue));
63 }
64 if (allval) {
65 TColStd_Array1OfReal tabval(1,max);
66 Expr_Array1OfNamedUnknown tabvar(1,max);
67 for (i=1; i<=max;i++) {
68 tabval(i) = Handle(Expr_NumericValue)::DownCast(SubExpression(i))->GetValue();
69 tabvar(i) = myFunction->Variable(i);
70 }
71 Standard_Real res = myFunction->Evaluate(tabvar,tabval);
72 return new Expr_NumericValue(res);
73 }
74 Handle(Expr_PolyFunction) me =this;
75 return me;
76}
77
78Handle(Expr_GeneralExpression) Expr_PolyFunction::Copy () const
79{
80 Standard_Integer max = NbSubExpressions();
81 Expr_Array1OfGeneralExpression vars(1,max);
82 for (Standard_Integer i = 1; i <= max; i++) {
83 vars(i) = Expr::CopyShare(SubExpression(i));
84 }
85 return new Expr_PolyFunction(myFunction,vars);
86}
87
88Standard_Boolean Expr_PolyFunction::IsIdentical (const Handle(Expr_GeneralExpression)& Other) const
89{
90 if (!Other->IsKind(STANDARD_TYPE(Expr_PolyFunction))) {
91 return Standard_False;
92 }
93 if (Other->NbSubExpressions() != NbSubExpressions()) {
94 return Standard_False;
95 }
96 Handle(Expr_PolyFunction) pother = Handle(Expr_PolyFunction)::DownCast(Other);
97 Handle(Expr_GeneralFunction) fother = pother->Function();
98 if (!fother->IsIdentical(Function())) {
99 return Standard_False;
100 }
101 Standard_Integer max = NbSubExpressions();
102 Handle(Expr_GeneralExpression) opother;
103 for (Standard_Integer i = 1; i<=max;i++) {
104 opother = pother->SubExpression(i);
105 if (!opother->IsIdentical(SubExpression(i))) {
106 return Standard_False;
107 }
108 }
109 return Standard_True;
110}
111
112Standard_Boolean Expr_PolyFunction::IsLinear () const
113{
114 if (!ContainsUnknowns()) {
115 return Standard_True;
116 }
117 for (Standard_Integer i=1; i<= NbOperands(); i++) {
118 if (!Operand(i)->IsLinear()) {
119 return Standard_False;
120 }
121 if (!myFunction->IsLinearOnVariable(i)) {
122 return Standard_False;
123 }
124 }
125 return Standard_True;
126}
127
128Handle(Expr_GeneralExpression) Expr_PolyFunction::Derivative (const Handle(Expr_NamedUnknown)& X) const
129{
130 Handle(Expr_GeneralExpression) myop;
131 Handle(Expr_NamedUnknown) thevar;
132 Handle(Expr_GeneralFunction) partderfunc;
133 Handle(Expr_PolyFunction) partder;
134 Handle(Expr_Product) partprod;
135 Standard_Integer max = NbSubExpressions();
136 Expr_Array1OfGeneralExpression theops(1,max);
137 for (Standard_Integer k=1; k<= max; k++) {
138 theops(k) = Operand(k);
139 }
140 Expr_SequenceOfGeneralExpression thesum;
141 for (Standard_Integer i = 1; i <= max; i++) {
142 thevar = myFunction->Variable(i);
143 myop = SubExpression(i);
144 partderfunc = myFunction->Derivative(thevar);
145 partder = new Expr_PolyFunction(partderfunc,theops);
146 partprod = partder->ShallowSimplified() * myop->Derivative(X);
147 thesum.Append(partprod->ShallowSimplified());
148 }
149 Handle(Expr_Sum) res = new Expr_Sum(thesum);
150 return res->ShallowSimplified();
151}
152
153
154Standard_Real Expr_PolyFunction::Evaluate(const Expr_Array1OfNamedUnknown& vars, const TColStd_Array1OfReal& vals) const
155{
156 Standard_Integer max = NbSubExpressions();
157 Expr_Array1OfNamedUnknown varsfunc(1,max);
158 TColStd_Array1OfReal valsfunc(1,max);
159 for (Standard_Integer i = 1; i <= max ; i++) {
160 varsfunc(i) = myFunction->Variable(i);
161 valsfunc(i) = SubExpression(i)->Evaluate(vars,vals);
162 }
163 return myFunction->Evaluate(varsfunc,valsfunc);
164}
165
166TCollection_AsciiString Expr_PolyFunction::String() const
167{
168 TCollection_AsciiString res = myFunction->GetStringName();
169 res += "(";
170 Standard_Integer max = NbOperands();
171 for (Standard_Integer i=1; i<= max; i++) {
172 res += Operand(i)->String();
173 if (i != max) {
174 res += ",";
175 }
176 }
177 res += ")";
178 return res;
179}