Salome HOME
Merge from V6_main_20120808 08Aug12
[tools/medcoupling.git] / src / INTERP_KERNEL / ExprEval / InterpKernelFunction.cxx
1 // Copyright (C) 2007-2012  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.
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
20 #include "InterpKernelFunction.hxx"
21 #include "InterpKernelValue.hxx"
22
23 #include <cmath>
24
25 using namespace INTERP_KERNEL;
26
27 const char IdentityFunction::REPR[]="Id";
28
29 const char PositiveFunction::REPR[]="+";
30
31 const char NegateFunction::REPR[]="-";
32
33 const char CosFunction::REPR[]="cos";
34
35 const char SinFunction::REPR[]="sin";
36
37 const char TanFunction::REPR[]="tan";
38
39 const char SqrtFunction::REPR[]="sqrt";
40
41 const char AbsFunction::REPR[]="abs";
42
43 const char PlusFunction::REPR[]="+";
44
45 const char MinusFunction::REPR[]="-";
46
47 const char MultFunction::REPR[]="*";
48
49 const char DivFunction::REPR[]="/";
50
51 const char PowFunction::REPR[]="^";
52
53 const char ExpFunction::REPR[]="exp";
54
55 const char LnFunction::REPR[]="ln";
56
57 const char LogFunction::REPR[]="log";
58
59 const char Log10Function::REPR[]="log10";
60
61 const char MaxFunction::REPR[]="max";
62
63 const char MinFunction::REPR[]="min";
64
65 const char GreaterThanFunction::REPR[]=">";
66
67 const char LowerThanFunction::REPR[]="<";
68
69 const char IfFunction::REPR[]="if";
70
71 Function *FunctionsFactory::buildFuncFromString(const char *type, int nbOfParams) throw(INTERP_KERNEL::Exception)
72 {
73   switch(nbOfParams)
74     {
75     case 1:
76       return buildUnaryFuncFromString(type);
77     case 2:
78       return buildBinaryFuncFromString(type);
79     case 3:
80       return buildTernaryFuncFromString(type);
81     default:
82       throw INTERP_KERNEL::Exception("Invalid number of params detected : limited to 2 !");
83     }
84 }
85
86 Function *FunctionsFactory::buildUnaryFuncFromString(const char *type) throw(INTERP_KERNEL::Exception)
87 {
88   std::string tmp(type);
89   if(tmp.empty())
90     return new IdentityFunction;
91   if(tmp==CosFunction::REPR)
92     return new CosFunction;
93   if(tmp==SinFunction::REPR)
94     return new SinFunction;
95   if(tmp==TanFunction::REPR)
96     return new TanFunction;
97   if(tmp==SqrtFunction::REPR)
98     return new SqrtFunction;
99   if(tmp==AbsFunction::REPR)
100     return new AbsFunction;
101   if(tmp==PositiveFunction::REPR)
102     return new PositiveFunction;
103   if(tmp==NegateFunction::REPR)
104     return new NegateFunction;
105   if(tmp==ExpFunction::REPR)
106     return new ExpFunction;
107   if(tmp==LnFunction::REPR)
108     return new LnFunction;
109   if(tmp==LogFunction::REPR)
110     return new LogFunction;
111   if(tmp==Log10Function::REPR)
112     return new Log10Function;
113   //
114   std::string msg("Invalid unary function detected : \"");
115   msg+=type; msg+="\"";
116   throw INTERP_KERNEL::Exception(msg.c_str());
117 }
118
119 Function *FunctionsFactory::buildBinaryFuncFromString(const char *type) throw(INTERP_KERNEL::Exception)
120 {
121   std::string tmp(type);
122   if(tmp==PositiveFunction::REPR)
123     return new PlusFunction;
124   if(tmp==NegateFunction::REPR)
125     return new MinusFunction;
126   if(tmp==MultFunction::REPR)
127     return new MultFunction;
128   if(tmp==DivFunction::REPR)
129     return new DivFunction;
130   if(tmp==PowFunction::REPR)
131     return new PowFunction;
132   if(tmp==MaxFunction::REPR)
133     return new MaxFunction;
134   if(tmp==MinFunction::REPR)
135     return new MinFunction;
136   if(tmp==GreaterThanFunction::REPR)
137     return new GreaterThanFunction;
138   if(tmp==LowerThanFunction::REPR)
139     return new LowerThanFunction;
140   std::string msg("Invalid binary function detected : \"");
141   msg+=type; msg+="\"";
142   throw INTERP_KERNEL::Exception(msg.c_str());
143 }
144
145 Function *FunctionsFactory::buildTernaryFuncFromString(const char *type) throw(INTERP_KERNEL::Exception)
146 {
147   std::string tmp(type);
148   if(tmp==IfFunction::REPR)
149     return new IfFunction();
150   std::string msg("Invalid ternary function detected : \"");
151   msg+=type; msg+="\"";
152   throw INTERP_KERNEL::Exception(msg.c_str());
153 }
154
155 Function *FunctionsFactory::buildBinaryFuncFromString(char type) throw(INTERP_KERNEL::Exception)
156 {
157   char tmp[2]; tmp[0]=type; tmp[1]='\0';
158   return buildBinaryFuncFromString(tmp);
159 }
160
161 Function::~Function()
162 {
163 }
164
165 IdentityFunction::~IdentityFunction()
166 {
167 }
168
169 void IdentityFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
170 {
171 }
172
173 void IdentityFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
174 {
175 }
176
177 const char *IdentityFunction::getRepr() const
178 {
179   return REPR;
180 }
181
182 bool IdentityFunction::isACall() const
183 {
184   return false;
185 }
186
187 PositiveFunction::~PositiveFunction()
188 {
189 }
190
191 int UnaryFunction::getNbInputParams() const
192 {
193   return 1;
194 }
195
196 void PositiveFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
197 {
198 }
199
200 void PositiveFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
201 {
202 }
203
204 const char *PositiveFunction::getRepr() const
205 {
206   return REPR;
207 }
208
209 bool PositiveFunction::isACall() const
210 {
211   return false;
212 }
213
214 NegateFunction::~NegateFunction()
215 {
216 }
217
218 void NegateFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
219 {
220   Value *val=stack.back();
221   val->negate();
222 }
223
224 void NegateFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
225 {
226   asmb.push_back("fchs");
227 }
228
229 const char *NegateFunction::getRepr() const
230 {
231   return REPR;
232 }
233
234 bool NegateFunction::isACall() const
235 {
236   return false;
237 }
238
239 CosFunction::~CosFunction()
240 {
241 }
242
243 void CosFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
244 {
245   Value *val=stack.back();
246   val->cos();
247 }
248
249 void CosFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
250 {
251   asmb.push_back("fcos");
252 }
253
254 const char *CosFunction::getRepr() const
255 {
256   return REPR;
257 }
258
259 bool CosFunction::isACall() const
260 {
261   return true;
262 }
263
264 SinFunction::~SinFunction()
265 {
266 }
267
268 void SinFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
269 {
270   Value *val=stack.back();
271   val->sin();
272 }
273
274 void SinFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
275 {
276   asmb.push_back("fsin");
277 }
278
279 const char *SinFunction::getRepr() const
280 {
281   return REPR;
282 }
283
284 bool SinFunction::isACall() const
285 {
286   return true;
287 }
288
289 TanFunction::~TanFunction()
290 {
291 }
292
293 void TanFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
294 {
295   Value *val=stack.back();
296   val->tan();
297 }
298
299 void TanFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
300 {
301   throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
302 }
303
304 const char *TanFunction::getRepr() const
305 {
306   return REPR;
307 }
308
309 bool TanFunction::isACall() const
310 {
311   return true;
312 }
313
314 SqrtFunction::~SqrtFunction()
315 {
316 }
317
318 void SqrtFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
319 {
320   Value *val=stack.back();
321   val->sqrt();
322 }
323
324 void SqrtFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
325 {
326   asmb.push_back("fsqrt");
327 }
328
329 const char *SqrtFunction::getRepr() const
330 {
331   return REPR;
332 }
333
334 bool SqrtFunction::isACall() const
335 {
336   return true;
337 }
338
339 AbsFunction::~AbsFunction()
340 {
341 }
342
343 void AbsFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
344 {
345   Value *val=stack.back();
346   val->abs();
347 }
348
349 void AbsFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
350 {
351   asmb.push_back("fabs");
352 }
353
354 const char *AbsFunction::getRepr() const
355 {
356   return REPR;
357 }
358
359 bool AbsFunction::isACall() const
360 {
361   return false;
362 }
363
364 void ExpFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
365 {
366   Value *val=stack.back();
367   val->exp();
368 }
369
370 void ExpFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
371 {
372   throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
373 }
374
375 const char *ExpFunction::getRepr() const
376 {
377   return REPR;
378 }
379
380 bool ExpFunction::isACall() const
381 {
382   return true;
383 }
384
385 LnFunction::~LnFunction()
386 {
387 }
388
389 void LnFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
390 {
391   Value *val=stack.back();
392   val->ln();
393 }
394
395 void LnFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
396 {
397   throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
398 }
399
400 const char *LnFunction::getRepr() const
401 {
402   return REPR;
403 }
404
405 bool LnFunction::isACall() const
406 {
407   return true;
408 }
409
410 LogFunction::~LogFunction()
411 {
412 }
413
414 void LogFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
415 {
416   Value *val=stack.back();
417   val->ln();
418 }
419
420 void LogFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
421 {
422   throw INTERP_KERNEL::Exception("Assembly for log Not implemented yet !");
423 }
424
425 const char *LogFunction::getRepr() const
426 {
427   return REPR;
428 }
429
430 bool LogFunction::isACall() const
431 {
432   return true;
433 }
434
435 Log10Function::~Log10Function()
436 {
437 }
438
439 void Log10Function::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
440 {
441   Value *val=stack.back();
442   val->log10();
443 }
444
445 void Log10Function::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
446 {
447   throw INTERP_KERNEL::Exception("Assembly for log Not implemented yet !");
448 }
449
450 const char *Log10Function::getRepr() const
451 {
452   return REPR;
453 }
454
455 bool Log10Function::isACall() const
456 {
457   return true;
458 }
459
460 int BinaryFunction::getNbInputParams() const
461 {
462   return 2;
463 }
464
465 PlusFunction::~PlusFunction()
466 {
467 }
468
469 void PlusFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
470 {
471   Value *val1=stack.back();
472   stack.pop_back();
473   Value *& val2=stack.back();
474   Value *val3;
475   try
476     {
477       val3=val1->plus(val2);
478     }
479   catch(INTERP_KERNEL::Exception& e)
480     {
481       delete val1;
482       throw e;
483     }
484   delete val1;
485   delete val2;
486   val2=val3;
487 }
488
489 void PlusFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
490 {
491   asmb.push_back("faddp st1");
492 }
493
494 const char *PlusFunction::getRepr() const
495 {
496   return REPR;
497 }
498
499 bool PlusFunction::isACall() const
500 {
501   return false;
502 }
503
504 MinusFunction::~MinusFunction()
505 {
506 }
507
508 void MinusFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
509 {
510   Value *val1=stack.back();
511   stack.pop_back();
512   Value *& val2=stack.back();
513   Value *val3;
514   try
515     {
516       val3=val1->minus(val2);
517     }
518   catch(INTERP_KERNEL::Exception& e)
519     {
520       delete val1;
521       throw e;
522     }
523   delete val1;
524   delete val2;
525   val2=val3;
526 }
527
528 void MinusFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
529 {
530   asmb.push_back("fsubp st1");
531 }
532
533 const char *MinusFunction::getRepr() const
534 {
535   return REPR;
536 }
537
538 bool MinusFunction::isACall() const
539 {
540   return false;
541 }
542
543 MultFunction::~MultFunction()
544 {
545 }
546
547 void MultFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
548 {
549   Value *val1=stack.back();
550   stack.pop_back();
551   Value *& val2=stack.back();
552   Value *val3=val1->mult(val2);
553   delete val1;
554   delete val2;
555   val2=val3;
556 }
557
558 void MultFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
559 {
560   asmb.push_back("fmulp st1");
561 }
562
563 const char *MultFunction::getRepr() const
564 {
565   return REPR;
566 }
567
568 bool MultFunction::isACall() const
569 {
570   return false;
571 }
572
573 DivFunction::~DivFunction()
574 {
575 }
576
577 void DivFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
578 {
579   Value *val1=stack.back();
580   stack.pop_back();
581   Value *& val2=stack.back();
582   Value *val3;
583   try
584     {
585       val3=val1->div(val2);
586     }
587   catch(INTERP_KERNEL::Exception& e)
588     {
589       delete val1;
590       throw e;
591     }
592   delete val1;
593   delete val2;
594   val2=val3;
595 }
596
597 void DivFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
598 {
599   asmb.push_back("fdivp st1");
600 }
601
602 const char *DivFunction::getRepr() const
603 {
604   return REPR;
605 }
606
607 bool DivFunction::isACall() const
608 {
609   return false;
610 }
611
612 PowFunction::~PowFunction()
613 {
614 }
615
616 void PowFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
617 {
618   Value *val1=stack.back();
619   stack.pop_back();
620   Value *& val2=stack.back();
621   Value *val3;
622   try
623     {
624       val3=val1->pow(val2);
625     }
626   catch(INTERP_KERNEL::Exception& e)
627     {
628       delete val1;
629       throw e;
630     }
631   delete val1;
632   delete val2;
633   val2=val3;
634 }
635
636 void PowFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
637 {
638   throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
639 }
640
641 const char *PowFunction::getRepr() const
642 {
643   return REPR;
644 }
645
646 bool PowFunction::isACall() const
647 {
648   return true;
649 }
650
651 ExpFunction::~ExpFunction()
652 {
653 }
654
655 MaxFunction::~MaxFunction()
656 {
657 }
658
659 void MaxFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
660 {
661   Value *val1=stack.back();
662   stack.pop_back();
663   Value *& val2=stack.back();
664   Value *val3;
665   try
666     {
667       val3=val1->max(val2);
668     }
669   catch(INTERP_KERNEL::Exception& e)
670     {
671       delete val1;
672       throw e;
673     }
674   delete val1;
675   delete val2;
676   val2=val3;
677 }
678
679 void MaxFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
680 {
681   throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
682 }
683
684 const char *MaxFunction::getRepr() const
685 {
686   return REPR;
687 }
688
689 bool MaxFunction::isACall() const
690 {
691   return false;
692 }
693
694 MinFunction::~MinFunction()
695 {
696 }
697
698 void MinFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
699 {
700   Value *val1=stack.back();
701   stack.pop_back();
702   Value *& val2=stack.back();
703   Value *val3;
704   try
705     {
706       val3=val1->min(val2);
707     }
708   catch(INTERP_KERNEL::Exception& e)
709     {
710       delete val1;
711       throw e;
712     }
713   delete val1;
714   delete val2;
715   val2=val3;
716 }
717
718 void MinFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
719 {
720   throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
721 }
722
723 const char *MinFunction::getRepr() const
724 {
725   return REPR;
726 }
727
728 bool MinFunction::isACall() const
729 {
730   return false;
731 }
732
733 GreaterThanFunction::~GreaterThanFunction()
734 {
735 }
736
737 void GreaterThanFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
738 {
739   Value *val1=stack.back();
740   stack.pop_back();
741   Value *& val2=stack.back();
742   Value *val3;
743   try
744     {
745       val3=val1->greaterThan(val2);
746     }
747   catch(INTERP_KERNEL::Exception& e)
748     {
749       delete val1;
750       throw e;
751     }
752   delete val1;
753   delete val2;
754   val2=val3;
755 }
756
757 void GreaterThanFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
758 {
759   throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
760 }
761
762 const char *GreaterThanFunction::getRepr() const
763 {
764   return REPR;
765 }
766
767 bool GreaterThanFunction::isACall() const
768 {
769   return false;
770 }
771
772 LowerThanFunction::~LowerThanFunction()
773 {
774 }
775
776 void LowerThanFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
777 {
778   Value *val1=stack.back();
779   stack.pop_back();
780   Value *& val2=stack.back();
781   Value *val3;
782   try
783     {
784       val3=val1->lowerThan(val2);
785     }
786   catch(INTERP_KERNEL::Exception& e)
787     {
788       delete val1;
789       throw e;
790     }
791   delete val1;
792   delete val2;
793   val2=val3;
794 }
795
796 void LowerThanFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
797 {
798   throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
799 }
800
801 const char *LowerThanFunction::getRepr() const
802 {
803   return REPR;
804 }
805
806 bool LowerThanFunction::isACall() const
807 {
808   return false;
809 }
810
811 int TernaryFunction::getNbInputParams() const
812 {
813   return 3;
814 }
815
816 IfFunction::~IfFunction()
817 {
818 }
819
820 void IfFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
821 {
822   Value *val1=stack.back();
823   stack.pop_back();
824   Value *val2=stack.back();
825   stack.pop_back();
826   Value *&val3=stack.back();
827   Value *val4;
828   try
829     {
830       val4=val1->ifFunc(val2,val3);
831     }
832   catch(INTERP_KERNEL::Exception& e)
833     {
834       delete val1;
835       delete val2;
836       throw e;
837     }
838   delete val1;
839   delete val2;
840   delete val3;
841   val3=val4;
842 }
843
844 void IfFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
845 {
846   throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
847 }
848
849 const char *IfFunction::getRepr() const
850 {
851   return REPR;
852 }
853
854 bool IfFunction::isACall() const
855 {
856   return false;
857 }
858