Salome HOME
Merge branch 'nouvelEficas' of http://git.forge-pleiade.der.edf.fr/git/eficas into...
[tools/eficas.git] / Extensions / param2.py
index ad49373ba61075894a3faf7ece8df06381840fcd..eabdadb7b83da48e2402eff3b4203907f4ca93d5 100644 (file)
@@ -1,9 +1,32 @@
 # -*- coding: utf-8 -*-
+# Copyright (C) 2007-2013   EDF R&D
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+#
+# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+#
 from __future__ import division
 import math
-import Numeric
 import types
 
+try:
+  import Numeric
+except:
+  import numpy
+  Numeric = numpy
+
 def mkf(value):
     if type(value) in (type(1), type(1L), type(1.5), type(1j),type("hh")) :
         return Constant(value)
@@ -30,6 +53,7 @@ class Formula:
     def __float__(self): return float(self.eval())
     def __pos__(self): return self  # positive
     def __neg__(self): return Unop('-', self)
+    def __abs__(self): return Unop('abs', self)
     def __add__(self, other): return Binop('+', self, other)
     def __radd__(self, other): return Binop('+', other, self)
     def __sub__(self, other): return Binop('-', self, other)
@@ -37,23 +61,48 @@ class Formula:
     def __mul__(self, other): return Binop('*', self, other)
     def __rmul__(self, other): return Binop('*', other, self)
     def __div__(self, other): return Binop('/', self, other)
-    def __truediv__(self, other): return Binop('/', self, other)
     def __rdiv__(self, other): return Binop('/', other, self)
+    def __truediv__(self, other): return Binop('/', self, other)
+    def __rtruediv__(self, other): return Binop('/', other, self)
+    def __floordiv__(self, other): return Binop('//', self, other)
+    def __rfloordiv__(self, other): return Binop('//', other, self)
     def __pow__(self, other): return Binop('**', self, other)
     def __rpow__(self, other): return Binop('**', other, self)
-    def __getitem__(self,i):return Binop('[]',self,i)
+    def __getitem__(self,i): 
+        if i > len(self) : raise StopIteration
+        return Binop('[]',self,i)
+    def __cmp__( self, other ): return self.eval().__cmp__(other)
+    def __eq__(  self, other ): return self.eval() == other
+    def __ne__(  self, other ): return self.eval() != other
+    def __lt__(  self, other ): return self.eval() < other
+    def __le__(  self, other ): return self.eval() <= other
+    def __gt__(  self, other ): return self.eval() > other
+    def __ge__(  self, other ): return self.eval() >= other
+    def __hash__(self):return id(self)
+
+def _div(a,b):
+  if isinstance(a,(int,long)) and isinstance(b,(int,long)):
+    if a%b:
+      return a/b
+    else:
+      return a//b
+  else:
+    return a/b
+
 
 class Binop(Formula):
     opmap = { '+': lambda a, b: a + b,
               '*': lambda a, b: a * b,
               '-': lambda a, b: a - b,
-              '/': lambda a, b: a / b,
+              '/': _div,
+              '//': lambda a, b: a // b,
               '**': lambda a, b: a ** b,
               '[]': lambda a, b: a[b] ,
             }
     def __init__(self, op, value1, value2):
         self.op = op
         self.values = mkf(value1), mkf(value2)
+
     def __str__(self):
         if self.op == '[]':
            return "%s[%s]" % (self.values[0], self.values[1])
@@ -76,6 +125,7 @@ class Binop(Formula):
 
 class Unop(Formula):
     opmap = { '-': lambda x: -x,
+              'abs': lambda x: abs(x),
              }
     def __init__(self, op, arg):
         self._op = op
@@ -130,7 +180,6 @@ class Variable(Formula):
     def __str__(self): return self._name
     def __adapt__(self,validator):
         return validator.adapt(self._value)
-
 def Eval(f):
     if isinstance(f,Formula):
         f=f.eval()
@@ -141,47 +190,99 @@ def Eval(f):
     return f
 
 
-#surcharge de la fonction cos de Numeric pour les parametres
-original_ncos=Numeric.cos
 def cos(f): return Unop('ncos', f)
-Unop.opmap['ncos']=lambda x: original_ncos(x)
-Numeric.cos=cos
-
-#surcharge de la fonction sin de Numeric pour les parametres
-original_nsin=Numeric.sin
 def sin(f): return Unop('nsin', f)
-Unop.opmap['nsin']=lambda x: original_nsin(x)
-Numeric.sin=sin
-
-#surcharge de la fonction array de Numeric pour les parametres
-original_narray=Numeric.array
 def array(f,*tup,**args): 
     """array de Numeric met en défaut la mécanique des parametres
        on la supprime dans ce cas. Il faut que la valeur du parametre soit bien définie
     """
     return original_narray(Eval(f),*tup,**args)
-Numeric.array=array
-
-#surcharge de la fonction sin de math pour les parametres
-original_sin=math.sin
 def sin(f): return Unop('sin', f)
-Unop.opmap['sin']=lambda x: original_sin(x)
-math.sin=sin
-
-#surcharge de la fonction cos de math pour les parametres
-original_cos=math.cos
-Unop.opmap['cos']=lambda x: original_cos(x)
 def cos(f): return Unop('cos', f)
-math.cos=cos
-
-#surcharge de la fonction sqrt de math pour les parametres
-original_sqrt=math.sqrt
+def ceil(f): return Unop('ceil', f)
 def sqrt(f): return Unop('sqrt', f)
-Unop.opmap['sqrt']=lambda x: original_sqrt(x)
-math.sqrt=sqrt
 
-#surcharge de la fonction ceil de math pour les parametres
-original_ceil=math.ceil
-Unop.opmap['ceil']=lambda x: original_ceil(x)
-def ceil(f): return Unop('ceil', f)
-math.ceil=ceil
+def pi2():return Unop('pi')
+
+class  OriginalMath(object):
+    _instance = None
+    def __new__(cls, *args, **kwargs):
+        if not cls._instance:
+            cls._instance = super(OriginalMath, cls).__new__(
+                                cls, *args, **kwargs)
+
+        return cls._instance
+
+    def __init__(self):
+        if hasattr(self,'pi') :return
+        import math
+        self.toSurcharge()
+
+    def toSurcharge(self):
+        self.numeric_ncos=Numeric.cos
+        self.numeric_nsin=Numeric.sin
+        self.numeric_narray=Numeric.array
+        self.sin=math.sin
+        self.cos=math.cos
+        self.sqrt=math.sqrt
+        self.ceil=math.ceil
+        self.pi=math.pi
+
+        #surcharge de la fonction cos de Numeric pour les parametres
+        original_ncos=Numeric.cos
+        Unop.opmap['ncos']=lambda x: original_ncos(x)
+        Numeric.cos=cos
+
+        #surcharge de la fonction sin de Numeric pour les parametres
+        original_nsin=Numeric.sin
+        Unop.opmap['nsin']=lambda x: original_nsin(x)
+        Numeric.sin=sin
+
+        #surcharge de la fonction array de Numeric pour les parametres
+        original_narray=Numeric.array
+        Numeric.array=array
+
+        #surcharge de la fonction sin de math pour les parametres
+        original_sin=math.sin
+        Unop.opmap['sin']=lambda x: original_sin(x)
+        math.sin=sin
+
+        #surcharge de la fonction cos de math pour les parametres
+        original_cos=math.cos
+        Unop.opmap['cos']=lambda x: original_cos(x)
+        math.cos=cos
+
+        #surcharge de la fonction sqrt de math pour les parametres
+        original_sqrt=math.sqrt
+        Unop.opmap['sqrt']=lambda x: original_sqrt(x)
+        math.sqrt=sqrt
+
+        #surcharge de la fonction ceil de math pour les parametres
+        original_ceil=math.ceil
+        Unop.opmap['ceil']=lambda x: original_ceil(x)
+        math.ceil=ceil
+
+        original_pi=math.pi
+        Unop.opmap['pi']=lambda x: original_pi
+        pi=Variable('pi',pi2)
+        math.pi=pi
+
+    def toOriginal(self):
+        import math
+        try:
+          import Numeric
+        except:
+          import numpy
+          Numeric = numpy
+
+        Numeric.cos=originalMath.numeric_ncos
+        Numeric.sin=originalMath.numeric_nsin
+        Numeric.array=originalMath.numeric_narray
+        math.sin=originalMath.sin
+        math.cos=originalMath.cos
+        math.sqrt=originalMath.sqrt
+        math.ceil=originalMath.ceil
+        math.pi=originalMath.pi
+
+
+originalMath=OriginalMath()