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
31 if type(value) in (type(1), type(1L), type(1.5), type(1j),type("hh")) :
32 return Constant(value)
33 elif isinstance(value, Formula):
35 elif type(value) == type([]):
36 return Constant(value)
38 # return Constant(value)
39 raise TypeError, ("Can't make formula from", value)
41 #class Formula(object):
45 if val is None:return 0
50 def __complex__(self): return complex(self.eval())
51 def __int__(self): return int(self.eval())
52 def __long__(self): return long(self.eval())
53 def __float__(self): return float(self.eval())
54 def __pos__(self): return self # positive
55 def __neg__(self): return Unop('-', self)
56 def __abs__(self): return Unop('abs', self)
57 def __add__(self, other): return Binop('+', self, other)
58 def __radd__(self, other): return Binop('+', other, self)
59 def __sub__(self, other): return Binop('-', self, other)
60 def __rsub__(self, other): return Binop('-', other, self)
61 def __mul__(self, other): return Binop('*', self, other)
62 def __rmul__(self, other): return Binop('*', other, self)
63 def __div__(self, other): return Binop('/', self, other)
64 def __rdiv__(self, other): return Binop('/', other, self)
65 def __truediv__(self, other): return Binop('/', self, other)
66 def __rtruediv__(self, other): return Binop('/', other, self)
67 def __floordiv__(self, other): return Binop('//', self, other)
68 def __rfloordiv__(self, other): return Binop('//', other, self)
69 def __pow__(self, other): return Binop('**', self, other)
70 def __rpow__(self, other): return Binop('**', other, self)
71 def __getitem__(self,i):
72 if i > len(self) : raise StopIteration
73 return Binop('[]',self,i)
74 def __cmp__( self, other ): return self.eval().__cmp__(other)
75 def __eq__( self, other ): return self.eval() == other
76 def __ne__( self, other ): return self.eval() != other
77 def __lt__( self, other ): return self.eval() < other
78 def __le__( self, other ): return self.eval() <= other
79 def __gt__( self, other ): return self.eval() > other
80 def __ge__( self, other ): return self.eval() >= other
81 def __hash__(self):return id(self)
84 if isinstance(a,(int,long)) and isinstance(b,(int,long)):
94 opmap = { '+': lambda a, b: a + b,
95 '*': lambda a, b: a * b,
96 '-': lambda a, b: a - b,
98 '//': lambda a, b: a // b,
99 '**': lambda a, b: a ** b,
100 '[]': lambda a, b: a[b] ,
102 def __init__(self, op, value1, value2):
104 self.values = mkf(value1), mkf(value2)
108 return "%s[%s]" % (self.values[0], self.values[1])
110 return "(%s %s %s)" % (self.values[0], self.op, self.values[1])
113 return "%s[%s]" % (self.values[0], self.values[1])
115 return "(%s %s %s)" % (self.values[0], self.op, self.values[1])
117 result= self.opmap[self.op](self.values[0].eval(),
118 self.values[1].eval())
119 while isinstance(result,Formula):
122 def __adapt__(self,validator):
123 return validator.adapt(self.eval())
127 opmap = { '-': lambda x: -x,
128 'abs': lambda x: abs(x),
130 def __init__(self, op, arg):
134 return "%s(%s)" % (self._op, self._arg)
136 return "%s(%s)" % (self._op, self._arg)
138 return self.opmap[self._op](self._arg.eval())
139 def __adapt__(self,validator):
140 return validator.adapt(self.eval())
143 def __init__(self, nom, op, arg):
148 self._arg.append(mkf(a))
167 class Constant(Formula):
168 def __init__(self, value): self._value = value
169 def eval(self): return self._value
170 def __str__(self): return str(self._value)
171 def __adapt__(self,validator):
172 return validator.adapt(self._value)
174 class Variable(Formula):
175 def __init__(self,name,value):
178 def eval(self): return self._value
179 def __repr__(self): return "Variable('%s',%s)" % (self._name, self._value)
180 def __str__(self): return self._name
181 def __adapt__(self,validator):
182 return validator.adapt(self._value)
184 if isinstance(f,Formula):
186 elif type(f) in (types.ListType, ):
187 f=[Eval(i) for i in f]
188 elif type(f) in (types.TupleType,):
189 f=tuple([Eval(i) for i in f])
193 def cos(f): return Unop('ncos', f)
194 def sin(f): return Unop('nsin', f)
195 def array(f,*tup,**args):
196 """array de Numeric met en défaut la mécanique des parametres
197 on la supprime dans ce cas. Il faut que la valeur du parametre soit bien définie
199 originalMath=OriginalMath()
200 original_narray=originalMath.original_narray
201 return original_narray(Eval(f),*tup,**args)
202 def sin(f): return Unop('sin', f)
203 def cos(f): return Unop('cos', f)
204 def ceil(f): return Unop('ceil', f)
205 def sqrt(f): return Unop('sqrt', f)
207 def pi2():return Unop('pi')
209 class OriginalMath(object):
211 def __new__(cls, *args, **kwargs):
212 if not cls._instance:
213 cls._instance = super(OriginalMath, cls).__new__(
214 cls, *args, **kwargs)
219 if hasattr(self,'pi') :return
223 def toSurcharge(self):
224 self.numeric_ncos=Numeric.cos
225 self.numeric_nsin=Numeric.sin
226 self.numeric_narray=Numeric.array
233 #surcharge de la fonction cos de Numeric pour les parametres
234 original_ncos=Numeric.cos
235 Unop.opmap['ncos']=lambda x: original_ncos(x)
238 #surcharge de la fonction sin de Numeric pour les parametres
239 original_nsin=Numeric.sin
240 Unop.opmap['nsin']=lambda x: original_nsin(x)
243 #surcharge de la fonction array de Numeric pour les parametres
244 original_narray=Numeric.array
245 self.original_narray=Numeric.array
248 #surcharge de la fonction sin de math pour les parametres
249 original_sin=math.sin
250 Unop.opmap['sin']=lambda x: original_sin(x)
253 #surcharge de la fonction cos de math pour les parametres
254 original_cos=math.cos
255 Unop.opmap['cos']=lambda x: original_cos(x)
258 #surcharge de la fonction sqrt de math pour les parametres
259 original_sqrt=math.sqrt
260 Unop.opmap['sqrt']=lambda x: original_sqrt(x)
263 #surcharge de la fonction ceil de math pour les parametres
264 original_ceil=math.ceil
265 Unop.opmap['ceil']=lambda x: original_ceil(x)
269 Unop.opmap['pi']=lambda x: original_pi
270 pi=Variable('pi',pi2)
273 def toOriginal(self):
281 Numeric.cos=originalMath.numeric_ncos
282 Numeric.sin=originalMath.numeric_nsin
283 Numeric.array=originalMath.numeric_narray
284 math.sin=originalMath.sin
285 math.cos=originalMath.cos
286 math.sqrt=originalMath.sqrt
287 math.ceil=originalMath.ceil
288 math.pi=originalMath.pi
291 originalMath=OriginalMath()