1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (CEA/DEN)
21 #include "InterpKernelFunction.hxx"
22 #include "InterpKernelValue.hxx"
27 using namespace INTERP_KERNEL;
29 const char IdentityFunction::REPR[]="Id";
31 const char PositiveFunction::REPR[]="+";
33 const char NegateFunction::REPR[]="-";
35 const char CosFunction::REPR[]="cos";
37 const char SinFunction::REPR[]="sin";
39 const char TanFunction::REPR[]="tan";
41 const char ACosFunction::REPR[]="acos";
43 const char ASinFunction::REPR[]="asin";
45 const char ATanFunction::REPR[]="atan";
47 const char CoshFunction::REPR[]="cosh";
49 const char SinhFunction::REPR[]="sinh";
51 const char TanhFunction::REPR[]="tanh";
53 const char SqrtFunction::REPR[]="sqrt";
55 const char AbsFunction::REPR[]="abs";
57 const char PlusFunction::REPR[]="+";
59 const char MinusFunction::REPR[]="-";
61 const char MultFunction::REPR[]="*";
63 const char DivFunction::REPR[]="/";
65 const char PowFunction::REPR[]="^";
67 const char ExpFunction::REPR[]="exp";
69 const char LnFunction::REPR[]="ln";
71 const char LogFunction::REPR[]="log";
73 const char Log10Function::REPR[]="log10";
75 const char MaxFunction::REPR[]="max";
77 const char MinFunction::REPR[]="min";
79 const char GreaterThanFunction::REPR[]=">";
81 const char LowerThanFunction::REPR[]="<";
83 const char IfFunction::REPR[]="if";
85 Function *FunctionsFactory::buildFuncFromString(const char *type, int nbOfParams)
90 return buildUnaryFuncFromString(type);
92 return buildBinaryFuncFromString(type);
94 return buildTernaryFuncFromString(type);
96 throw INTERP_KERNEL::Exception("Invalid number of params detected : limited to 2 !");
100 Function *FunctionsFactory::buildUnaryFuncFromString(const char *type)
102 std::string tmp(type);
104 return new IdentityFunction;
105 if(tmp==CosFunction::REPR)
106 return new CosFunction;
107 if(tmp==SinFunction::REPR)
108 return new SinFunction;
109 if(tmp==TanFunction::REPR)
110 return new TanFunction;
111 if(tmp==ACosFunction::REPR)
112 return new ACosFunction;
113 if(tmp==ASinFunction::REPR)
114 return new ASinFunction;
115 if(tmp==ATanFunction::REPR)
116 return new ATanFunction;
117 if(tmp==CoshFunction::REPR)
118 return new CoshFunction;
119 if(tmp==SinhFunction::REPR)
120 return new SinhFunction;
121 if(tmp==TanhFunction::REPR)
122 return new TanhFunction;
123 if(tmp==SqrtFunction::REPR)
124 return new SqrtFunction;
125 if(tmp==AbsFunction::REPR)
126 return new AbsFunction;
127 if(tmp==PositiveFunction::REPR)
128 return new PositiveFunction;
129 if(tmp==NegateFunction::REPR)
130 return new NegateFunction;
131 if(tmp==ExpFunction::REPR)
132 return new ExpFunction;
133 if(tmp==LnFunction::REPR)
134 return new LnFunction;
135 if(tmp==LogFunction::REPR)
136 return new LogFunction;
137 if(tmp==Log10Function::REPR)
138 return new Log10Function;
140 std::string msg("Invalid unary function detected : \"");
141 msg+=type; msg+="\"";
142 throw INTERP_KERNEL::Exception(msg.c_str());
145 Function *FunctionsFactory::buildBinaryFuncFromString(const char *type)
147 std::string tmp(type);
148 if(tmp==PositiveFunction::REPR)
149 return new PlusFunction;
150 if(tmp==NegateFunction::REPR)
151 return new MinusFunction;
152 if(tmp==MultFunction::REPR)
153 return new MultFunction;
154 if(tmp==DivFunction::REPR)
155 return new DivFunction;
156 if(tmp==PowFunction::REPR)
157 return new PowFunction;
158 if(tmp==MaxFunction::REPR)
159 return new MaxFunction;
160 if(tmp==MinFunction::REPR)
161 return new MinFunction;
162 if(tmp==GreaterThanFunction::REPR)
163 return new GreaterThanFunction;
164 if(tmp==LowerThanFunction::REPR)
165 return new LowerThanFunction;
166 std::string msg("Invalid binary function detected : \"");
167 msg+=type; msg+="\"";
168 throw INTERP_KERNEL::Exception(msg.c_str());
171 Function *FunctionsFactory::buildTernaryFuncFromString(const char *type)
173 std::string tmp(type);
174 if(tmp==IfFunction::REPR)
175 return new IfFunction();
176 std::string msg("Invalid ternary function detected : \"");
177 msg+=type; msg+="\"";
178 throw INTERP_KERNEL::Exception(msg.c_str());
181 Function *FunctionsFactory::buildBinaryFuncFromString(char type)
183 char tmp[2]; tmp[0]=type; tmp[1]='\0';
184 return buildBinaryFuncFromString(tmp);
187 Function::~Function()
191 IdentityFunction::~IdentityFunction()
195 void IdentityFunction::operate(std::vector<Value *>& stck) const
199 void IdentityFunction::operateX86(std::vector<std::string>& asmb) const
203 void IdentityFunction::operateStackOfDouble(std::vector<double>& stck) const
207 const char *IdentityFunction::getRepr() const
212 bool IdentityFunction::isACall() const
217 PositiveFunction::~PositiveFunction()
221 int UnaryFunction::getNbInputParams() const
226 void PositiveFunction::operate(std::vector<Value *>& stck) const
230 void PositiveFunction::operateX86(std::vector<std::string>& asmb) const
234 void PositiveFunction::operateStackOfDouble(std::vector<double>& stck) const
238 const char *PositiveFunction::getRepr() const
243 bool PositiveFunction::isACall() const
248 NegateFunction::~NegateFunction()
252 void NegateFunction::operate(std::vector<Value *>& stck) const
254 Value *val=stck.back();
258 void NegateFunction::operateX86(std::vector<std::string>& asmb) const
260 asmb.push_back("fchs");
263 void NegateFunction::operateStackOfDouble(std::vector<double>& stck) const
265 double v(stck.back());
269 const char *NegateFunction::getRepr() const
274 bool NegateFunction::isACall() const
279 CosFunction::~CosFunction()
283 void CosFunction::operate(std::vector<Value *>& stck) const
285 Value *val=stck.back();
289 void CosFunction::operateX86(std::vector<std::string>& asmb) const
291 asmb.push_back("fcos");
294 void CosFunction::operateStackOfDouble(std::vector<double>& stck) const
296 double v(stck.back());
300 const char *CosFunction::getRepr() const
305 bool CosFunction::isACall() const
310 SinFunction::~SinFunction()
314 void SinFunction::operate(std::vector<Value *>& stck) const
316 Value *val=stck.back();
320 void SinFunction::operateX86(std::vector<std::string>& asmb) const
322 asmb.push_back("fsin");
325 void SinFunction::operateStackOfDouble(std::vector<double>& stck) const
327 double v(stck.back());
331 const char *SinFunction::getRepr() const
336 bool SinFunction::isACall() const
341 TanFunction::~TanFunction()
345 void TanFunction::operate(std::vector<Value *>& stck) const
347 Value *val=stck.back();
351 void TanFunction::operateX86(std::vector<std::string>& asmb) const
353 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
356 void TanFunction::operateStackOfDouble(std::vector<double>& stck) const
358 double v(stck.back());
362 const char *TanFunction::getRepr() const
367 bool TanFunction::isACall() const
372 ACosFunction::~ACosFunction()
376 void ACosFunction::operate(std::vector<Value *>& stck) const
378 Value *val=stck.back();
382 void ACosFunction::operateX86(std::vector<std::string>& asmb) const
384 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
387 void ACosFunction::operateStackOfDouble(std::vector<double>& stck) const
389 double v(stck.back());
393 void ACosFunction::operateStackOfDoubleSafe(std::vector<double>& stck) const
395 double v(stck.back());
397 throw INTERP_KERNEL::Exception("acos on a value which absolute is > 1 !");
401 const char *ACosFunction::getRepr() const
406 bool ACosFunction::isACall() const
411 ASinFunction::~ASinFunction()
415 void ASinFunction::operate(std::vector<Value *>& stck) const
417 Value *val=stck.back();
421 void ASinFunction::operateX86(std::vector<std::string>& asmb) const
423 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
426 void ASinFunction::operateStackOfDouble(std::vector<double>& stck) const
428 double v(stck.back());
432 void ASinFunction::operateStackOfDoubleSafe(std::vector<double>& stck) const
434 double v(stck.back());
436 throw INTERP_KERNEL::Exception("asin on a value which absolute is > 1 !");
440 const char *ASinFunction::getRepr() const
445 bool ASinFunction::isACall() const
450 ATanFunction::~ATanFunction()
454 void ATanFunction::operate(std::vector<Value *>& stck) const
456 Value *val=stck.back();
460 void ATanFunction::operateX86(std::vector<std::string>& asmb) const
462 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
465 void ATanFunction::operateStackOfDouble(std::vector<double>& stck) const
467 double v(stck.back());
471 const char *ATanFunction::getRepr() const
476 bool ATanFunction::isACall() const
481 CoshFunction::~CoshFunction()
485 void CoshFunction::operate(std::vector<Value *>& stck) const
487 Value *val=stck.back();
491 void CoshFunction::operateX86(std::vector<std::string>& asmb) const
493 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
496 void CoshFunction::operateStackOfDouble(std::vector<double>& stck) const
498 double v(stck.back());
502 const char *CoshFunction::getRepr() const
507 bool CoshFunction::isACall() const
512 SinhFunction::~SinhFunction()
516 void SinhFunction::operate(std::vector<Value *>& stck) const
518 Value *val=stck.back();
522 void SinhFunction::operateX86(std::vector<std::string>& asmb) const
524 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
527 void SinhFunction::operateStackOfDouble(std::vector<double>& stck) const
529 double v(stck.back());
533 const char *SinhFunction::getRepr() const
538 bool SinhFunction::isACall() const
543 TanhFunction::~TanhFunction()
547 void TanhFunction::operate(std::vector<Value *>& stck) const
549 Value *val=stck.back();
553 void TanhFunction::operateX86(std::vector<std::string>& asmb) const
555 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
558 void TanhFunction::operateStackOfDouble(std::vector<double>& stck) const
560 double v(stck.back());
564 const char *TanhFunction::getRepr() const
569 bool TanhFunction::isACall() const
574 SqrtFunction::~SqrtFunction()
578 void SqrtFunction::operate(std::vector<Value *>& stck) const
580 Value *val=stck.back();
584 void SqrtFunction::operateX86(std::vector<std::string>& asmb) const
586 asmb.push_back("fsqrt");
589 void SqrtFunction::operateStackOfDouble(std::vector<double>& stck) const
591 double v(stck.back());
595 void SqrtFunction::operateStackOfDoubleSafe(std::vector<double>& stck) const
597 double v(stck.back());
599 throw INTERP_KERNEL::Exception("sqrt on a value < 0. !");
603 const char *SqrtFunction::getRepr() const
608 bool SqrtFunction::isACall() const
613 AbsFunction::~AbsFunction()
617 void AbsFunction::operate(std::vector<Value *>& stck) const
619 Value *val=stck.back();
623 void AbsFunction::operateX86(std::vector<std::string>& asmb) const
625 asmb.push_back("fabs");
628 void AbsFunction::operateStackOfDouble(std::vector<double>& stck) const
630 double v(stck.back());
634 const char *AbsFunction::getRepr() const
639 bool AbsFunction::isACall() const
644 void ExpFunction::operate(std::vector<Value *>& stck) const
646 Value *val=stck.back();
650 void ExpFunction::operateX86(std::vector<std::string>& asmb) const
652 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
655 void ExpFunction::operateStackOfDouble(std::vector<double>& stck) const
657 double v(stck.back());
658 stck.back()=std::exp(v);
661 const char *ExpFunction::getRepr() const
666 bool ExpFunction::isACall() const
671 LnFunction::~LnFunction()
675 void LnFunction::operate(std::vector<Value *>& stck) const
677 Value *val=stck.back();
681 void LnFunction::operateX86(std::vector<std::string>& asmb) const
683 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
686 void LnFunction::operateStackOfDouble(std::vector<double>& stck) const
688 double v(stck.back());
689 stck.back()=std::log(v);
692 void LnFunction::operateStackOfDoubleSafe(std::vector<double>& stck) const
694 double v(stck.back());
696 throw INTERP_KERNEL::Exception("ln on a value < 0. !");
697 stck.back()=std::log(v);
700 const char *LnFunction::getRepr() const
705 bool LnFunction::isACall() const
710 LogFunction::~LogFunction()
714 void LogFunction::operate(std::vector<Value *>& stck) const
716 Value *val=stck.back();
720 void LogFunction::operateX86(std::vector<std::string>& asmb) const
722 throw INTERP_KERNEL::Exception("Assembly for log Not implemented yet !");
725 void LogFunction::operateStackOfDouble(std::vector<double>& stck) const
727 double v(stck.back());
728 stck.back()=std::log(v);
731 void LogFunction::operateStackOfDoubleSafe(std::vector<double>& stck) const
733 double v(stck.back());
735 throw INTERP_KERNEL::Exception("log on a value < 0. !");
736 stck.back()=std::log(v);
739 const char *LogFunction::getRepr() const
744 bool LogFunction::isACall() const
749 Log10Function::~Log10Function()
753 void Log10Function::operate(std::vector<Value *>& stck) const
755 Value *val=stck.back();
759 void Log10Function::operateX86(std::vector<std::string>& asmb) const
761 throw INTERP_KERNEL::Exception("Assembly for log Not implemented yet !");
764 void Log10Function::operateStackOfDouble(std::vector<double>& stck) const
766 double v(stck.back());
767 stck.back()=std::log10(v);
770 void Log10Function::operateStackOfDoubleSafe(std::vector<double>& stck) const
772 double v(stck.back());
774 throw INTERP_KERNEL::Exception("log10 on a value < 0. !");
775 stck.back()=std::log10(v);
778 const char *Log10Function::getRepr() const
783 bool Log10Function::isACall() const
788 int BinaryFunction::getNbInputParams() const
793 PlusFunction::~PlusFunction()
797 void PlusFunction::operate(std::vector<Value *>& stck) const
799 Value *val1=stck.back();
801 Value *& val2=stck.back();
805 val3=val1->plus(val2);
807 catch(INTERP_KERNEL::Exception& e)
817 void PlusFunction::operateX86(std::vector<std::string>& asmb) const
819 asmb.push_back("faddp st1");
822 void PlusFunction::operateStackOfDouble(std::vector<double>& stck) const
824 double a(stck.back());
826 stck.back()=a+stck.back();
829 const char *PlusFunction::getRepr() const
834 bool PlusFunction::isACall() const
839 MinusFunction::~MinusFunction()
843 void MinusFunction::operate(std::vector<Value *>& stck) const
845 Value *val1=stck.back();
847 Value *& val2=stck.back();
851 val3=val1->minus(val2);
853 catch(INTERP_KERNEL::Exception& e)
863 void MinusFunction::operateX86(std::vector<std::string>& asmb) const
865 asmb.push_back("fsubp st1");
868 void MinusFunction::operateStackOfDouble(std::vector<double>& stck) const
870 double a(stck.back());
872 stck.back()=a-stck.back();
875 const char *MinusFunction::getRepr() const
880 bool MinusFunction::isACall() const
885 MultFunction::~MultFunction()
889 void MultFunction::operate(std::vector<Value *>& stck) const
891 Value *val1=stck.back();
893 Value *& val2=stck.back();
894 Value *val3=val1->mult(val2);
900 void MultFunction::operateX86(std::vector<std::string>& asmb) const
902 asmb.push_back("fmulp st1");
905 void MultFunction::operateStackOfDouble(std::vector<double>& stck) const
907 double a(stck.back());
909 stck.back()=a*stck.back();
912 const char *MultFunction::getRepr() const
917 bool MultFunction::isACall() const
922 DivFunction::~DivFunction()
926 void DivFunction::operate(std::vector<Value *>& stck) const
928 Value *val1=stck.back();
930 Value *& val2=stck.back();
934 val3=val1->div(val2);
936 catch(INTERP_KERNEL::Exception& e)
946 void DivFunction::operateX86(std::vector<std::string>& asmb) const
948 asmb.push_back("fdivp st1");
951 void DivFunction::operateStackOfDouble(std::vector<double>& stck) const
953 double a(stck.back());
955 stck.back()=a/stck.back();
958 void DivFunction::operateStackOfDoubleSafe(std::vector<double>& stck) const
960 double a(stck.back());
963 throw INTERP_KERNEL::Exception("division by 0. !");
964 stck.back()=a/stck.back();
967 const char *DivFunction::getRepr() const
972 bool DivFunction::isACall() const
977 PowFunction::~PowFunction()
981 void PowFunction::operate(std::vector<Value *>& stck) const
983 Value *val1=stck.back();
985 Value *& val2=stck.back();
989 val3=val1->pow(val2);
991 catch(INTERP_KERNEL::Exception& e)
1001 void PowFunction::operateX86(std::vector<std::string>& asmb) const
1003 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
1006 void PowFunction::operateStackOfDouble(std::vector<double>& stck) const
1008 double a(stck.back());
1010 stck.back()=std::pow(a,stck.back());
1013 void PowFunction::operateStackOfDoubleSafe(std::vector<double>& stck) const
1015 double a(stck.back());
1017 double b(stck.back());
1019 throw INTERP_KERNEL::Exception("pow with val < 0. !");
1020 stck.back()=std::pow(a,b);
1023 const char *PowFunction::getRepr() const
1028 bool PowFunction::isACall() const
1033 ExpFunction::~ExpFunction()
1037 MaxFunction::~MaxFunction()
1041 void MaxFunction::operate(std::vector<Value *>& stck) const
1043 Value *val1=stck.back();
1045 Value *& val2=stck.back();
1049 val3=val1->max(val2);
1051 catch(INTERP_KERNEL::Exception& e)
1061 void MaxFunction::operateX86(std::vector<std::string>& asmb) const
1063 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
1066 void MaxFunction::operateStackOfDouble(std::vector<double>& stck) const
1068 double a(stck.back());
1070 stck.back()=std::max(stck.back(),a);
1073 const char *MaxFunction::getRepr() const
1078 bool MaxFunction::isACall() const
1083 MinFunction::~MinFunction()
1087 void MinFunction::operate(std::vector<Value *>& stck) const
1089 Value *val1=stck.back();
1091 Value *& val2=stck.back();
1095 val3=val1->min(val2);
1097 catch(INTERP_KERNEL::Exception& e)
1107 void MinFunction::operateX86(std::vector<std::string>& asmb) const
1109 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
1112 void MinFunction::operateStackOfDouble(std::vector<double>& stck) const
1114 double a(stck.back());
1116 stck.back()=std::min(stck.back(),a);
1119 const char *MinFunction::getRepr() const
1124 bool MinFunction::isACall() const
1129 GreaterThanFunction::~GreaterThanFunction()
1133 void GreaterThanFunction::operate(std::vector<Value *>& stck) const
1135 Value *val1=stck.back();
1137 Value *& val2=stck.back();
1141 val3=val1->greaterThan(val2);
1143 catch(INTERP_KERNEL::Exception& e)
1153 void GreaterThanFunction::operateX86(std::vector<std::string>& asmb) const
1155 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
1158 void GreaterThanFunction::operateStackOfDouble(std::vector<double>& stck) const
1160 double a(stck.back());
1162 double b(stck.back());
1163 stck.back()=a>b?std::numeric_limits<double>::max():-std::numeric_limits<double>::max();
1166 const char *GreaterThanFunction::getRepr() const
1171 bool GreaterThanFunction::isACall() const
1176 LowerThanFunction::~LowerThanFunction()
1180 void LowerThanFunction::operate(std::vector<Value *>& stck) const
1182 Value *val1=stck.back();
1184 Value *& val2=stck.back();
1188 val3=val1->lowerThan(val2);
1190 catch(INTERP_KERNEL::Exception& e)
1200 void LowerThanFunction::operateX86(std::vector<std::string>& asmb) const
1202 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
1205 void LowerThanFunction::operateStackOfDouble(std::vector<double>& stck) const
1207 double a(stck.back());
1209 double b(stck.back());
1210 stck.back()=a<b?std::numeric_limits<double>::max():-std::numeric_limits<double>::max();
1213 const char *LowerThanFunction::getRepr() const
1218 bool LowerThanFunction::isACall() const
1223 int TernaryFunction::getNbInputParams() const
1228 IfFunction::~IfFunction()
1232 void IfFunction::operate(std::vector<Value *>& stck) const
1234 Value *val1=stck.back();
1236 Value *val2=stck.back();
1238 Value *&val3=stck.back();
1242 val4=val1->ifFunc(val2,val3);
1244 catch(INTERP_KERNEL::Exception& e)
1256 void IfFunction::operateX86(std::vector<std::string>& asmb) const
1258 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
1261 void IfFunction::operateStackOfDouble(std::vector<double>& stck) const
1263 double cond(stck.back());
1265 double the(stck.back());
1267 if(cond==std::numeric_limits<double>::max())
1271 void IfFunction::operateStackOfDoubleSafe(std::vector<double>& stck) const
1273 double cond(stck.back());
1275 double the(stck.back());
1277 if(cond!=std::numeric_limits<double>::max() && cond!=-std::numeric_limits<double>::max())
1278 throw INTERP_KERNEL::Exception("ifFunc : first parameter of ternary func is NOT a consequence of a boolean op !");
1279 if(cond==std::numeric_limits<double>::max())
1283 const char *IfFunction::getRepr() const
1288 bool IfFunction::isACall() const