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