Salome HOME
5498b53fe4eccce2375b1ff7b036da70725a24ef
[tools/medcoupling.git] / src / INTERP_KERNEL / ExprEval / InterpKernelFunction.cxx
1 // Copyright (C) 2007-2019  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (CEA/DEN)
20
21 #include "InterpKernelFunction.hxx"
22 #include "InterpKernelValue.hxx"
23
24 #include <algorithm>
25 #include <cmath>
26 #include <limits>
27
28 using namespace INTERP_KERNEL;
29
30 const char IdentityFunction::REPR[]="Id";
31
32 const char PositiveFunction::REPR[]="+";
33
34 const char NegateFunction::REPR[]="-";
35
36 const char CosFunction::REPR[]="cos";
37
38 const char SinFunction::REPR[]="sin";
39
40 const char TanFunction::REPR[]="tan";
41
42 const char ACosFunction::REPR[]="acos";
43
44 const char ASinFunction::REPR[]="asin";
45
46 const char ATanFunction::REPR[]="atan";
47
48 const char CoshFunction::REPR[]="cosh";
49
50 const char SinhFunction::REPR[]="sinh";
51
52 const char TanhFunction::REPR[]="tanh";
53
54 const char SqrtFunction::REPR[]="sqrt";
55
56 const char AbsFunction::REPR[]="abs";
57
58 const char PlusFunction::REPR[]="+";
59
60 const char MinusFunction::REPR[]="-";
61
62 const char MultFunction::REPR[]="*";
63
64 const char DivFunction::REPR[]="/";
65
66 const char PowFunction::REPR[]="^";
67
68 const char ExpFunction::REPR[]="exp";
69
70 const char LnFunction::REPR[]="ln";
71
72 const char LogFunction::REPR[]="log";
73
74 const char Log10Function::REPR[]="log10";
75
76 const char MaxFunction::REPR[]="max";
77
78 const char MinFunction::REPR[]="min";
79
80 const char GreaterThanFunction::REPR[]=">";
81
82 const char LowerThanFunction::REPR[]="<";
83
84 const char IfFunction::REPR[]="if";
85
86 Function *FunctionsFactory::buildFuncFromString(const char *type, int nbOfParams)
87 {
88   switch(nbOfParams)
89     {
90     case 1:
91       return buildUnaryFuncFromString(type);
92     case 2:
93       return buildBinaryFuncFromString(type);
94     case 3:
95       return buildTernaryFuncFromString(type);
96     default:
97       throw INTERP_KERNEL::Exception("Invalid number of params detected : limited to 2 !");
98     }
99 }
100
101 Function *FunctionsFactory::buildUnaryFuncFromString(const char *type)
102 {
103   std::string tmp(type);
104   if(tmp.empty())
105     return new IdentityFunction;
106   if(tmp==CosFunction::REPR)
107     return new CosFunction;
108   if(tmp==SinFunction::REPR)
109     return new SinFunction;
110   if(tmp==TanFunction::REPR)
111     return new TanFunction;
112   if(tmp==ACosFunction::REPR)
113     return new ACosFunction;
114   if(tmp==ASinFunction::REPR)
115     return new ASinFunction;
116   if(tmp==ATanFunction::REPR)
117     return new ATanFunction;
118   if(tmp==CoshFunction::REPR)
119     return new CoshFunction;
120   if(tmp==SinhFunction::REPR)
121     return new SinhFunction;
122   if(tmp==TanhFunction::REPR)
123     return new TanhFunction;
124   if(tmp==SqrtFunction::REPR)
125     return new SqrtFunction;
126   if(tmp==AbsFunction::REPR)
127     return new AbsFunction;
128   if(tmp==PositiveFunction::REPR)
129     return new PositiveFunction;
130   if(tmp==NegateFunction::REPR)
131     return new NegateFunction;
132   if(tmp==ExpFunction::REPR)
133     return new ExpFunction;
134   if(tmp==LnFunction::REPR)
135     return new LnFunction;
136   if(tmp==LogFunction::REPR)
137     return new LogFunction;
138   if(tmp==Log10Function::REPR)
139     return new Log10Function;
140   //
141   std::string msg("Invalid unary function detected : \"");
142   msg+=type; msg+="\"";
143   throw INTERP_KERNEL::Exception(msg.c_str());
144 }
145
146 Function *FunctionsFactory::buildBinaryFuncFromString(const char *type)
147 {
148   std::string tmp(type);
149   if(tmp==PositiveFunction::REPR)
150     return new PlusFunction;
151   if(tmp==NegateFunction::REPR)
152     return new MinusFunction;
153   if(tmp==MultFunction::REPR)
154     return new MultFunction;
155   if(tmp==DivFunction::REPR)
156     return new DivFunction;
157   if(tmp==PowFunction::REPR)
158     return new PowFunction;
159   if(tmp==MaxFunction::REPR)
160     return new MaxFunction;
161   if(tmp==MinFunction::REPR)
162     return new MinFunction;
163   if(tmp==GreaterThanFunction::REPR)
164     return new GreaterThanFunction;
165   if(tmp==LowerThanFunction::REPR)
166     return new LowerThanFunction;
167   std::string msg("Invalid binary function detected : \"");
168   msg+=type; msg+="\"";
169   throw INTERP_KERNEL::Exception(msg.c_str());
170 }
171
172 Function *FunctionsFactory::buildTernaryFuncFromString(const char *type)
173 {
174   std::string tmp(type);
175   if(tmp==IfFunction::REPR)
176     return new IfFunction();
177   std::string msg("Invalid ternary function detected : \"");
178   msg+=type; msg+="\"";
179   throw INTERP_KERNEL::Exception(msg.c_str());
180 }
181
182 Function *FunctionsFactory::buildBinaryFuncFromString(char type)
183 {
184   char tmp[2]; tmp[0]=type; tmp[1]='\0';
185   return buildBinaryFuncFromString(tmp);
186 }
187
188 Function::~Function()
189 {
190 }
191
192 IdentityFunction::~IdentityFunction()
193 {
194 }
195
196 void IdentityFunction::operate(std::vector<Value *>& stck) const
197 {
198 }
199
200 void IdentityFunction::operateX86(std::vector<std::string>& asmb) const
201 {
202 }
203
204 void IdentityFunction::operateStackOfDouble(std::vector<double>& stck) const
205 {
206 }
207
208 const char *IdentityFunction::getRepr() const
209 {
210   return REPR;
211 }
212
213 bool IdentityFunction::isACall() const
214 {
215   return false;
216 }
217
218 PositiveFunction::~PositiveFunction()
219 {
220 }
221
222 int UnaryFunction::getNbInputParams() const
223 {
224   return 1;
225 }
226
227 void PositiveFunction::operate(std::vector<Value *>& stck) const
228 {
229 }
230
231 void PositiveFunction::operateX86(std::vector<std::string>& asmb) const
232 {
233 }
234
235 void PositiveFunction::operateStackOfDouble(std::vector<double>& stck) const
236 {
237 }
238
239 const char *PositiveFunction::getRepr() const
240 {
241   return REPR;
242 }
243
244 bool PositiveFunction::isACall() const
245 {
246   return false;
247 }
248
249 NegateFunction::~NegateFunction()
250 {
251 }
252
253 void NegateFunction::operate(std::vector<Value *>& stck) const
254 {
255   Value *val=stck.back();
256   val->negate();
257 }
258
259 void NegateFunction::operateX86(std::vector<std::string>& asmb) const
260 {
261   asmb.push_back("fchs");
262 }
263
264 void NegateFunction::operateStackOfDouble(std::vector<double>& stck) const
265 {
266   double v(stck.back());
267   stck.back()=-v;
268 }
269
270 const char *NegateFunction::getRepr() const
271 {
272   return REPR;
273 }
274
275 bool NegateFunction::isACall() const
276 {
277   return false;
278 }
279
280 CosFunction::~CosFunction()
281 {
282 }
283
284 void CosFunction::operate(std::vector<Value *>& stck) const
285 {
286   Value *val=stck.back();
287   val->cos();
288 }
289
290 void CosFunction::operateX86(std::vector<std::string>& asmb) const
291 {
292   asmb.push_back("fcos");
293 }
294
295 void CosFunction::operateStackOfDouble(std::vector<double>& stck) const
296 {
297   double v(stck.back());
298   stck.back()=cos(v);
299 }
300
301 const char *CosFunction::getRepr() const
302 {
303   return REPR;
304 }
305
306 bool CosFunction::isACall() const
307 {
308   return true;
309 }
310
311 SinFunction::~SinFunction()
312 {
313 }
314
315 void SinFunction::operate(std::vector<Value *>& stck) const
316 {
317   Value *val=stck.back();
318   val->sin();
319 }
320
321 void SinFunction::operateX86(std::vector<std::string>& asmb) const
322 {
323   asmb.push_back("fsin");
324 }
325
326 void SinFunction::operateStackOfDouble(std::vector<double>& stck) const
327 {
328   double v(stck.back());
329   stck.back()=sin(v);
330 }
331
332 const char *SinFunction::getRepr() const
333 {
334   return REPR;
335 }
336
337 bool SinFunction::isACall() const
338 {
339   return true;
340 }
341
342 TanFunction::~TanFunction()
343 {
344 }
345
346 void TanFunction::operate(std::vector<Value *>& stck) const
347 {
348   Value *val=stck.back();
349   val->tan();
350 }
351
352 void TanFunction::operateX86(std::vector<std::string>& asmb) const
353 {
354   throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
355 }
356
357 void TanFunction::operateStackOfDouble(std::vector<double>& stck) const
358 {
359   double v(stck.back());
360   stck.back()=tan(v);
361 }
362
363 const char *TanFunction::getRepr() const
364 {
365   return REPR;
366 }
367
368 bool TanFunction::isACall() const
369 {
370   return true;
371 }
372
373 ACosFunction::~ACosFunction()
374 {
375 }
376
377 void ACosFunction::operate(std::vector<Value *>& stck) const
378 {
379   Value *val=stck.back();
380   val->acos();
381 }
382
383 void ACosFunction::operateX86(std::vector<std::string>& asmb) const
384 {
385   throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
386 }
387
388 void ACosFunction::operateStackOfDouble(std::vector<double>& stck) const
389 {
390   double v(stck.back());
391   stck.back()=acos(v);
392 }
393
394 void ACosFunction::operateStackOfDoubleSafe(std::vector<double>& stck) const
395 {
396   double v(stck.back());
397   if(fabs(v)>1.)
398     throw INTERP_KERNEL::Exception("acos on a value which absolute is > 1 !");
399   stck.back()=acos(v);
400 }
401
402 const char *ACosFunction::getRepr() const
403 {
404   return REPR;
405 }
406
407 bool ACosFunction::isACall() const
408 {
409   return true;
410 }
411
412 ASinFunction::~ASinFunction()
413 {
414 }
415
416 void ASinFunction::operate(std::vector<Value *>& stck) const
417 {
418   Value *val=stck.back();
419   val->asin();
420 }
421
422 void ASinFunction::operateX86(std::vector<std::string>& asmb) const
423 {
424   throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
425 }
426
427 void ASinFunction::operateStackOfDouble(std::vector<double>& stck) const
428 {
429   double v(stck.back());
430   stck.back()=asin(v);
431 }
432
433 void ASinFunction::operateStackOfDoubleSafe(std::vector<double>& stck) const
434 {
435   double v(stck.back());
436   if(fabs(v)>1.)
437     throw INTERP_KERNEL::Exception("asin on a value which absolute is > 1 !");
438   stck.back()=asin(v);
439 }
440
441 const char *ASinFunction::getRepr() const
442 {
443   return REPR;
444 }
445
446 bool ASinFunction::isACall() const
447 {
448   return true;
449 }
450
451 ATanFunction::~ATanFunction()
452 {
453 }
454
455 void ATanFunction::operate(std::vector<Value *>& stck) const
456 {
457   Value *val=stck.back();
458   val->atan();
459 }
460
461 void ATanFunction::operateX86(std::vector<std::string>& asmb) const
462 {
463   throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
464 }
465
466 void ATanFunction::operateStackOfDouble(std::vector<double>& stck) const
467 {
468   double v(stck.back());
469   stck.back()=atan(v);
470 }
471
472 const char *ATanFunction::getRepr() const
473 {
474   return REPR;
475 }
476
477 bool ATanFunction::isACall() const
478 {
479   return true;
480 }
481
482 CoshFunction::~CoshFunction()
483 {
484 }
485
486 void CoshFunction::operate(std::vector<Value *>& stck) const
487 {
488   Value *val=stck.back();
489   val->cosh();
490 }
491
492 void CoshFunction::operateX86(std::vector<std::string>& asmb) const
493 {
494   throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
495 }
496
497 void CoshFunction::operateStackOfDouble(std::vector<double>& stck) const
498 {
499   double v(stck.back());
500   stck.back()=cosh(v);
501 }
502
503 const char *CoshFunction::getRepr() const
504 {
505   return REPR;
506 }
507
508 bool CoshFunction::isACall() const
509 {
510   return true;
511 }
512
513 SinhFunction::~SinhFunction()
514 {
515 }
516
517 void SinhFunction::operate(std::vector<Value *>& stck) const
518 {
519   Value *val=stck.back();
520   val->sinh();
521 }
522
523 void SinhFunction::operateX86(std::vector<std::string>& asmb) const
524 {
525   throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
526 }
527
528 void SinhFunction::operateStackOfDouble(std::vector<double>& stck) const
529 {
530   double v(stck.back());
531   stck.back()=sinh(v);
532 }
533
534 const char *SinhFunction::getRepr() const
535 {
536   return REPR;
537 }
538
539 bool SinhFunction::isACall() const
540 {
541   return true;
542 }
543
544 TanhFunction::~TanhFunction()
545 {
546 }
547
548 void TanhFunction::operate(std::vector<Value *>& stck) const
549 {
550   Value *val=stck.back();
551   val->tanh();
552 }
553
554 void TanhFunction::operateX86(std::vector<std::string>& asmb) const
555 {
556   throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
557 }
558
559 void TanhFunction::operateStackOfDouble(std::vector<double>& stck) const
560 {
561   double v(stck.back());
562   stck.back()=tanh(v);
563 }
564
565 const char *TanhFunction::getRepr() const
566 {
567   return REPR;
568 }
569
570 bool TanhFunction::isACall() const
571 {
572   return true;
573 }
574
575 SqrtFunction::~SqrtFunction()
576 {
577 }
578
579 void SqrtFunction::operate(std::vector<Value *>& stck) const
580 {
581   Value *val=stck.back();
582   val->sqrt();
583 }
584
585 void SqrtFunction::operateX86(std::vector<std::string>& asmb) const
586 {
587   asmb.push_back("fsqrt");
588 }
589
590 void SqrtFunction::operateStackOfDouble(std::vector<double>& stck) const
591 {
592   double v(stck.back());
593   stck.back()=sqrt(v);
594 }
595
596 void SqrtFunction::operateStackOfDoubleSafe(std::vector<double>& stck) const
597 {
598   double v(stck.back());
599   if(v<0.)
600     throw INTERP_KERNEL::Exception("sqrt on a value < 0. !");
601   stck.back()=sqrt(v);
602 }
603
604 const char *SqrtFunction::getRepr() const
605 {
606   return REPR;
607 }
608
609 bool SqrtFunction::isACall() const
610 {
611   return true;
612 }
613
614 AbsFunction::~AbsFunction()
615 {
616 }
617
618 void AbsFunction::operate(std::vector<Value *>& stck) const
619 {
620   Value *val=stck.back();
621   val->abs();
622 }
623
624 void AbsFunction::operateX86(std::vector<std::string>& asmb) const
625 {
626   asmb.push_back("fabs");
627 }
628
629 void AbsFunction::operateStackOfDouble(std::vector<double>& stck) const
630 {
631   double v(stck.back());
632   stck.back()=fabs(v);
633 }
634
635 const char *AbsFunction::getRepr() const
636 {
637   return REPR;
638 }
639
640 bool AbsFunction::isACall() const
641 {
642   return false;
643 }
644
645 void ExpFunction::operate(std::vector<Value *>& stck) const
646 {
647   Value *val=stck.back();
648   val->exp();
649 }
650
651 void ExpFunction::operateX86(std::vector<std::string>& asmb) const
652 {
653   throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
654 }
655
656 void ExpFunction::operateStackOfDouble(std::vector<double>& stck) const
657 {
658   double v(stck.back());
659   stck.back()=std::exp(v);
660 }
661
662 const char *ExpFunction::getRepr() const
663 {
664   return REPR;
665 }
666
667 bool ExpFunction::isACall() const
668 {
669   return true;
670 }
671
672 LnFunction::~LnFunction()
673 {
674 }
675
676 void LnFunction::operate(std::vector<Value *>& stck) const
677 {
678   Value *val=stck.back();
679   val->ln();
680 }
681
682 void LnFunction::operateX86(std::vector<std::string>& asmb) const
683 {
684   throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
685 }
686
687 void LnFunction::operateStackOfDouble(std::vector<double>& stck) const
688 {
689   double v(stck.back());
690   stck.back()=std::log(v);
691 }
692
693 void LnFunction::operateStackOfDoubleSafe(std::vector<double>& stck) const
694 {
695   double v(stck.back());
696   if(v<0.)
697     throw INTERP_KERNEL::Exception("ln on a value < 0. !");
698   stck.back()=std::log(v);
699 }
700
701 const char *LnFunction::getRepr() const
702 {
703   return REPR;
704 }
705
706 bool LnFunction::isACall() const
707 {
708   return true;
709 }
710
711 LogFunction::~LogFunction()
712 {
713 }
714
715 void LogFunction::operate(std::vector<Value *>& stck) const
716 {
717   Value *val=stck.back();
718   val->ln();
719 }
720
721 void LogFunction::operateX86(std::vector<std::string>& asmb) const
722 {
723   throw INTERP_KERNEL::Exception("Assembly for log Not implemented yet !");
724 }
725
726 void LogFunction::operateStackOfDouble(std::vector<double>& stck) const
727 {
728   double v(stck.back());
729   stck.back()=std::log(v);
730 }
731
732 void LogFunction::operateStackOfDoubleSafe(std::vector<double>& stck) const
733 {
734   double v(stck.back());
735   if(v<0.)
736     throw INTERP_KERNEL::Exception("log on a value < 0. !");
737   stck.back()=std::log(v);
738 }
739
740 const char *LogFunction::getRepr() const
741 {
742   return REPR;
743 }
744
745 bool LogFunction::isACall() const
746 {
747   return true;
748 }
749
750 Log10Function::~Log10Function()
751 {
752 }
753
754 void Log10Function::operate(std::vector<Value *>& stck) const
755 {
756   Value *val=stck.back();
757   val->log10();
758 }
759
760 void Log10Function::operateX86(std::vector<std::string>& asmb) const
761 {
762   throw INTERP_KERNEL::Exception("Assembly for log Not implemented yet !");
763 }
764
765 void Log10Function::operateStackOfDouble(std::vector<double>& stck) const
766 {
767   double v(stck.back());
768   stck.back()=std::log10(v);
769 }
770
771 void Log10Function::operateStackOfDoubleSafe(std::vector<double>& stck) const
772 {
773   double v(stck.back());
774   if(v<0.)
775     throw INTERP_KERNEL::Exception("log10 on a value < 0. !");
776   stck.back()=std::log10(v);
777 }
778
779 const char *Log10Function::getRepr() const
780 {
781   return REPR;
782 }
783
784 bool Log10Function::isACall() const
785 {
786   return true;
787 }
788
789 int BinaryFunction::getNbInputParams() const
790 {
791   return 2;
792 }
793
794 PlusFunction::~PlusFunction()
795 {
796 }
797
798 void PlusFunction::operate(std::vector<Value *>& stck) const
799 {
800   Value *val1=stck.back();
801   stck.pop_back();
802   Value *& val2=stck.back();
803   Value *val3;
804   try
805     {
806       val3=val1->plus(val2);
807     }
808   catch(INTERP_KERNEL::Exception& e)
809     {
810       delete val1;
811       throw e;
812     }
813   delete val1;
814   delete val2;
815   val2=val3;
816 }
817
818 void PlusFunction::operateX86(std::vector<std::string>& asmb) const
819 {
820   asmb.push_back("faddp st1");
821 }
822
823 void PlusFunction::operateStackOfDouble(std::vector<double>& stck) const
824 {
825   double a(stck.back());
826   stck.pop_back();
827   stck.back()=a+stck.back();
828 }
829
830 const char *PlusFunction::getRepr() const
831 {
832   return REPR;
833 }
834
835 bool PlusFunction::isACall() const
836 {
837   return false;
838 }
839
840 MinusFunction::~MinusFunction()
841 {
842 }
843
844 void MinusFunction::operate(std::vector<Value *>& stck) const
845 {
846   Value *val1=stck.back();
847   stck.pop_back();
848   Value *& val2=stck.back();
849   Value *val3;
850   try
851     {
852       val3=val1->minus(val2);
853     }
854   catch(INTERP_KERNEL::Exception& e)
855     {
856       delete val1;
857       throw e;
858     }
859   delete val1;
860   delete val2;
861   val2=val3;
862 }
863
864 void MinusFunction::operateX86(std::vector<std::string>& asmb) const
865 {
866   asmb.push_back("fsubp st1");
867 }
868
869 void MinusFunction::operateStackOfDouble(std::vector<double>& stck) const
870 {
871   double a(stck.back());
872   stck.pop_back();
873   stck.back()=a-stck.back();
874 }
875
876 const char *MinusFunction::getRepr() const
877 {
878   return REPR;
879 }
880
881 bool MinusFunction::isACall() const
882 {
883   return false;
884 }
885
886 MultFunction::~MultFunction()
887 {
888 }
889
890 void MultFunction::operate(std::vector<Value *>& stck) const
891 {
892   Value *val1=stck.back();
893   stck.pop_back();
894   Value *& val2=stck.back();
895   Value *val3=val1->mult(val2);
896   delete val1;
897   delete val2;
898   val2=val3;
899 }
900
901 void MultFunction::operateX86(std::vector<std::string>& asmb) const
902 {
903   asmb.push_back("fmulp st1");
904 }
905
906 void MultFunction::operateStackOfDouble(std::vector<double>& stck) const
907 {
908   double a(stck.back());
909   stck.pop_back();
910   stck.back()=a*stck.back();
911 }
912
913 const char *MultFunction::getRepr() const
914 {
915   return REPR;
916 }
917
918 bool MultFunction::isACall() const
919 {
920   return false;
921 }
922
923 DivFunction::~DivFunction()
924 {
925 }
926
927 void DivFunction::operate(std::vector<Value *>& stck) const
928 {
929   Value *val1=stck.back();
930   stck.pop_back();
931   Value *& val2=stck.back();
932   Value *val3;
933   try
934     {
935       val3=val1->div(val2);
936     }
937   catch(INTERP_KERNEL::Exception& e)
938     {
939       delete val1;
940       throw e;
941     }
942   delete val1;
943   delete val2;
944   val2=val3;
945 }
946
947 void DivFunction::operateX86(std::vector<std::string>& asmb) const
948 {
949   asmb.push_back("fdivp st1");
950 }
951
952 void DivFunction::operateStackOfDouble(std::vector<double>& stck) const
953 {
954   double a(stck.back());
955   stck.pop_back();
956   stck.back()=a/stck.back();
957 }
958
959 void DivFunction::operateStackOfDoubleSafe(std::vector<double>& stck) const
960 {
961   double a(stck.back());
962   stck.pop_back();
963   if(stck.back()==0.)
964     throw INTERP_KERNEL::Exception("division by 0. !");
965   stck.back()=a/stck.back();
966 }
967
968 const char *DivFunction::getRepr() const
969 {
970   return REPR;
971 }
972
973 bool DivFunction::isACall() const
974 {
975   return false;
976 }
977
978 PowFunction::~PowFunction()
979 {
980 }
981
982 void PowFunction::operate(std::vector<Value *>& stck) const
983 {
984   Value *val1=stck.back();
985   stck.pop_back();
986   Value *& val2=stck.back();
987   Value *val3;
988   try
989     {
990       val3=val1->pow(val2);
991     }
992   catch(INTERP_KERNEL::Exception& e)
993     {
994       delete val1;
995       throw e;
996     }
997   delete val1;
998   delete val2;
999   val2=val3;
1000 }
1001
1002 void PowFunction::operateX86(std::vector<std::string>& asmb) const
1003 {
1004   throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
1005 }
1006
1007 void PowFunction::operateStackOfDouble(std::vector<double>& stck) const
1008 {
1009   double a(stck.back());
1010   stck.pop_back();
1011   stck.back()=std::pow(a,stck.back());
1012 }
1013
1014 void PowFunction::operateStackOfDoubleSafe(std::vector<double>& stck) const
1015 {
1016   double a(stck.back());
1017   stck.pop_back();
1018   double b(stck.back());
1019   if(a<0.)
1020     throw INTERP_KERNEL::Exception("pow with val < 0. !");
1021   stck.back()=std::pow(a,b);
1022 }
1023
1024 const char *PowFunction::getRepr() const
1025 {
1026   return REPR;
1027 }
1028
1029 bool PowFunction::isACall() const
1030 {
1031   return true;
1032 }
1033
1034 ExpFunction::~ExpFunction()
1035 {
1036 }
1037
1038 MaxFunction::~MaxFunction()
1039 {
1040 }
1041
1042 void MaxFunction::operate(std::vector<Value *>& stck) const
1043 {
1044   Value *val1=stck.back();
1045   stck.pop_back();
1046   Value *& val2=stck.back();
1047   Value *val3;
1048   try
1049     {
1050       val3=val1->max(val2);
1051     }
1052   catch(INTERP_KERNEL::Exception& e)
1053     {
1054       delete val1;
1055       throw e;
1056     }
1057   delete val1;
1058   delete val2;
1059   val2=val3;
1060 }
1061
1062 void MaxFunction::operateX86(std::vector<std::string>& asmb) const
1063 {
1064   throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
1065 }
1066
1067 void MaxFunction::operateStackOfDouble(std::vector<double>& stck) const
1068 {
1069   double a(stck.back());
1070   stck.pop_back();
1071   stck.back()=std::max(stck.back(),a);
1072 }
1073
1074 const char *MaxFunction::getRepr() const
1075 {
1076   return REPR;
1077 }
1078
1079 bool MaxFunction::isACall() const
1080 {
1081   return false;
1082 }
1083
1084 MinFunction::~MinFunction()
1085 {
1086 }
1087
1088 void MinFunction::operate(std::vector<Value *>& stck) const
1089 {
1090   Value *val1=stck.back();
1091   stck.pop_back();
1092   Value *& val2=stck.back();
1093   Value *val3;
1094   try
1095     {
1096       val3=val1->min(val2);
1097     }
1098   catch(INTERP_KERNEL::Exception& e)
1099     {
1100       delete val1;
1101       throw e;
1102     }
1103   delete val1;
1104   delete val2;
1105   val2=val3;
1106 }
1107
1108 void MinFunction::operateX86(std::vector<std::string>& asmb) const
1109 {
1110   throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
1111 }
1112
1113 void MinFunction::operateStackOfDouble(std::vector<double>& stck) const
1114 {
1115   double a(stck.back());
1116   stck.pop_back();
1117   stck.back()=std::min(stck.back(),a);
1118 }
1119
1120 const char *MinFunction::getRepr() const
1121 {
1122   return REPR;
1123 }
1124
1125 bool MinFunction::isACall() const
1126 {
1127   return false;
1128 }
1129
1130 GreaterThanFunction::~GreaterThanFunction()
1131 {
1132 }
1133
1134 void GreaterThanFunction::operate(std::vector<Value *>& stck) const
1135 {
1136   Value *val1=stck.back();
1137   stck.pop_back();
1138   Value *& val2=stck.back();
1139   Value *val3;
1140   try
1141     {
1142       val3=val1->greaterThan(val2);
1143     }
1144   catch(INTERP_KERNEL::Exception& e)
1145     {
1146       delete val1;
1147       throw e;
1148     }
1149   delete val1;
1150   delete val2;
1151   val2=val3;
1152 }
1153
1154 void GreaterThanFunction::operateX86(std::vector<std::string>& asmb) const
1155 {
1156   throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
1157 }
1158
1159 void GreaterThanFunction::operateStackOfDouble(std::vector<double>& stck) const
1160 {
1161   double a(stck.back());
1162   stck.pop_back();
1163   double b(stck.back());
1164   stck.back()=a>b?std::numeric_limits<double>::max():-std::numeric_limits<double>::max();
1165 }
1166
1167 const char *GreaterThanFunction::getRepr() const
1168 {
1169   return REPR;
1170 }
1171
1172 bool GreaterThanFunction::isACall() const
1173 {
1174   return false;
1175 }
1176
1177 LowerThanFunction::~LowerThanFunction()
1178 {
1179 }
1180
1181 void LowerThanFunction::operate(std::vector<Value *>& stck) const
1182 {
1183   Value *val1=stck.back();
1184   stck.pop_back();
1185   Value *& val2=stck.back();
1186   Value *val3;
1187   try
1188     {
1189       val3=val1->lowerThan(val2);
1190     }
1191   catch(INTERP_KERNEL::Exception& e)
1192     {
1193       delete val1;
1194       throw e;
1195     }
1196   delete val1;
1197   delete val2;
1198   val2=val3;
1199 }
1200
1201 void LowerThanFunction::operateX86(std::vector<std::string>& asmb) const
1202 {
1203   throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
1204 }
1205
1206 void LowerThanFunction::operateStackOfDouble(std::vector<double>& stck) const
1207 {
1208   double a(stck.back());
1209   stck.pop_back();
1210   double b(stck.back());
1211   stck.back()=a<b?std::numeric_limits<double>::max():-std::numeric_limits<double>::max();
1212 }
1213
1214 const char *LowerThanFunction::getRepr() const
1215 {
1216   return REPR;
1217 }
1218
1219 bool LowerThanFunction::isACall() const
1220 {
1221   return false;
1222 }
1223
1224 int TernaryFunction::getNbInputParams() const
1225 {
1226   return 3;
1227 }
1228
1229 IfFunction::~IfFunction()
1230 {
1231 }
1232
1233 void IfFunction::operate(std::vector<Value *>& stck) const
1234 {
1235   Value *val1=stck.back();
1236   stck.pop_back();
1237   Value *val2=stck.back();
1238   stck.pop_back();
1239   Value *&val3=stck.back();
1240   Value *val4;
1241   try
1242     {
1243       val4=val1->ifFunc(val2,val3);
1244     }
1245   catch(INTERP_KERNEL::Exception& e)
1246     {
1247       delete val1;
1248       delete val2;
1249       throw e;
1250     }
1251   delete val1;
1252   delete val2;
1253   delete val3;
1254   val3=val4;
1255 }
1256
1257 void IfFunction::operateX86(std::vector<std::string>& asmb) const
1258 {
1259   throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
1260 }
1261
1262 void IfFunction::operateStackOfDouble(std::vector<double>& stck) const
1263 {
1264   double cond(stck.back());
1265   stck.pop_back();
1266   double the(stck.back());
1267   stck.pop_back();
1268   if(cond==std::numeric_limits<double>::max())
1269     stck.back()=the;
1270 }
1271
1272 void IfFunction::operateStackOfDoubleSafe(std::vector<double>& stck) const
1273 {
1274   double cond(stck.back());
1275   stck.pop_back();
1276   double the(stck.back());
1277   stck.pop_back();
1278   if(cond!=std::numeric_limits<double>::max() && cond!=-std::numeric_limits<double>::max())
1279     throw INTERP_KERNEL::Exception("ifFunc : first parameter of ternary func is NOT a consequence of a boolean op !");
1280   if(cond==std::numeric_limits<double>::max())
1281     stck.back()=the;
1282 }
1283
1284 const char *IfFunction::getRepr() const
1285 {
1286   return REPR;
1287 }
1288
1289 bool IfFunction::isACall() const
1290 {
1291   return false;
1292 }
1293