1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2013 EDF R&D
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 from __future__ import division
21 from __future__ import absolute_import
23 from builtins import str
24 from builtins import object
41 if type(value) in (type(1), type(1), type(1.5), type(1j),type("hh")) :
42 return Constant(value)
43 elif isinstance(value, Formula):
45 elif type(value) == type([]):
46 return Constant(value)
48 # return Constant(value)
49 raise TypeError("Can't make formula from", value)
51 #class Formula(object):
52 class Formula(object):
55 if val is None:return 0
60 def __complex__(self): return complex(self.eval())
61 def __int__(self): return int(self.eval())
62 def __long__(self): return int(self.eval())
63 def __float__(self): return float(self.eval())
64 def __pos__(self): return self # positive
65 def __neg__(self): return Unop('-', self)
66 def __abs__(self): return Unop('abs', self)
67 def __add__(self, other): return Binop('+', self, other)
68 def __radd__(self, other): return Binop('+', other, self)
69 def __sub__(self, other): return Binop('-', self, other)
70 def __rsub__(self, other): return Binop('-', other, self)
71 def __mul__(self, other): return Binop('*', self, other)
72 def __rmul__(self, other): return Binop('*', other, self)
73 def __div__(self, other): return Binop('/', self, other)
74 def __rdiv__(self, other): return Binop('/', other, self)
75 def __truediv__(self, other): return Binop('/', self, other)
76 def __rtruediv__(self, other): return Binop('/', other, self)
77 def __floordiv__(self, other): return Binop('//', self, other)
78 def __rfloordiv__(self, other): return Binop('//', other, self)
79 def __pow__(self, other): return Binop('**', self, other)
80 def __rpow__(self, other): return Binop('**', other, self)
81 def __getitem__(self,i):
82 if i > len(self) : raise StopIteration
83 return Binop('[]',self,i)
84 def __cmp__( self, other ): return self.eval().__cmp__(other)
85 def __eq__( self, other ): return self.eval() == other
86 def __ne__( self, other ): return self.eval() != other
87 def __lt__( self, other ): return self.eval() < other
88 def __le__( self, other ): return self.eval() <= other
89 def __gt__( self, other ): return self.eval() > other
90 def __ge__( self, other ): return self.eval() >= other
91 def __hash__(self):return id(self)
94 if isinstance(a,six.integer_types) and isinstance(b,six.integer_types):
103 class Binop(Formula):
104 opmap = { '+': lambda a, b: a + b,
105 '*': lambda a, b: a * b,
106 '-': lambda a, b: a - b,
108 '//': lambda a, b: a // b,
109 '**': lambda a, b: a ** b,
110 '[]': lambda a, b: a[b] ,
112 def __init__(self, op, value1, value2):
114 self.values = mkf(value1), mkf(value2)
118 return "%s[%s]" % (self.values[0], self.values[1])
120 return "(%s %s %s)" % (self.values[0], self.op, self.values[1])
123 return "%s[%s]" % (self.values[0], self.values[1])
125 return "(%s %s %s)" % (self.values[0], self.op, self.values[1])
127 result= self.opmap[self.op](self.values[0].eval(),
128 self.values[1].eval())
129 while isinstance(result,Formula):
132 def __adapt__(self,validator):
133 return validator.adapt(self.eval())
137 opmap = { '-': lambda x: -x,
138 'abs': lambda x: abs(x),
140 def __init__(self, op, arg):
144 return "%s(%s)" % (self._op, self._arg)
146 return "%s(%s)" % (self._op, self._arg)
148 return self.opmap[self._op](self._arg.eval())
149 def __adapt__(self,validator):
150 return validator.adapt(self.eval())
153 def __init__(self, nom, op, arg):
158 self._arg.append(mkf(a))
177 class Constant(Formula):
178 def __init__(self, value): self._value = value
179 def eval(self): return self._value
180 def __str__(self): return str(self._value)
181 def __adapt__(self,validator):
182 return validator.adapt(self._value)
184 class Variable(Formula):
185 def __init__(self,name,value):
188 def eval(self): return self._value
189 def __repr__(self): return "Variable('%s',%s)" % (self._name, self._value)
190 def __str__(self): return self._name
191 def __adapt__(self,validator):
192 return validator.adapt(self._value)
194 if isinstance(f,Formula):
196 elif type(f) in (list, ):
197 f=[Eval(i) for i in f]
198 elif type(f) in (tuple,):
199 f=tuple([Eval(i) for i in f])
203 def cos(f): return Unop('ncos', f)
204 def sin(f): return Unop('nsin', f)
205 def array(f,*tup,**args):
206 """array de Numeric met en defaut la mecanique des parametres
207 on la supprime dans ce cas. Il faut que la valeur du parametre soit bien definie
209 originalMath=OriginalMath()
210 original_narray=originalMath.original_narray
211 return original_narray(Eval(f),*tup,**args)
212 def sin(f): return Unop('sin', f)
213 def cos(f): return Unop('cos', f)
214 def ceil(f): return Unop('ceil', f)
215 def sqrt(f): return Unop('sqrt', f)
217 def pi2():return Unop('pi')
219 class OriginalMath(object):
221 def __new__(cls, *args, **kwargs):
222 if not cls._instance:
223 cls._instance = super(OriginalMath, cls).__new__(
224 cls, *args, **kwargs)
229 if hasattr(self,'pi') :return
235 def toSurcharge(self):
236 self.numeric_ncos=Numeric.cos
237 self.numeric_nsin=Numeric.sin
238 self.numeric_narray=Numeric.array
245 #surcharge de la fonction cos de Numeric pour les parametres
246 original_ncos=Numeric.cos
247 Unop.opmap['ncos']=lambda x: original_ncos(x)
250 #surcharge de la fonction sin de Numeric pour les parametres
251 original_nsin=Numeric.sin
252 Unop.opmap['nsin']=lambda x: original_nsin(x)
255 #surcharge de la fonction array de Numeric pour les parametres
256 original_narray=Numeric.array
257 self.original_narray=Numeric.array
260 #surcharge de la fonction sin de math pour les parametres
261 original_sin=math.sin
262 Unop.opmap['sin']=lambda x: original_sin(x)
265 #surcharge de la fonction cos de math pour les parametres
266 original_cos=math.cos
267 Unop.opmap['cos']=lambda x: original_cos(x)
270 #surcharge de la fonction sqrt de math pour les parametres
271 original_sqrt=math.sqrt
272 Unop.opmap['sqrt']=lambda x: original_sqrt(x)
275 #surcharge de la fonction ceil de math pour les parametres
276 original_ceil=math.ceil
277 Unop.opmap['ceil']=lambda x: original_ceil(x)
281 Unop.opmap['pi']=lambda x: original_pi
282 pi=Variable('pi',pi2)
285 def toOriginal(self):
294 Numeric.cos=originalMath.numeric_ncos
295 Numeric.sin=originalMath.numeric_nsin
296 Numeric.array=originalMath.numeric_narray
298 math.sin=originalMath.sin
299 math.cos=originalMath.cos
300 math.sqrt=originalMath.sqrt
301 math.ceil=originalMath.ceil
302 math.pi=originalMath.pi
305 originalMath=OriginalMath()