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)
185 if isinstance(f,Formula):
187 elif type(f) in (types.ListType, ):
188 f=[Eval(i) for i in f]
189 elif type(f) in (types.TupleType,):
190 f=tuple([Eval(i) for i in f])
194 #surcharge de la fonction cos de Numeric pour les parametres
195 original_ncos=Numeric.cos
196 def cos(f): return Unop('ncos', f)
197 Unop.opmap['ncos']=lambda x: original_ncos(x)
200 #surcharge de la fonction sin de Numeric pour les parametres
201 original_nsin=Numeric.sin
202 def sin(f): return Unop('nsin', f)
203 Unop.opmap['nsin']=lambda x: original_nsin(x)
206 #surcharge de la fonction array de Numeric pour les parametres
207 original_narray=Numeric.array
208 def array(f,*tup,**args):
209 """array de Numeric met en défaut la mécanique des parametres
210 on la supprime dans ce cas. Il faut que la valeur du parametre soit bien définie
212 return original_narray(Eval(f),*tup,**args)
215 #surcharge de la fonction sin de math pour les parametres
216 original_sin=math.sin
217 def sin(f): return Unop('sin', f)
218 Unop.opmap['sin']=lambda x: original_sin(x)
221 #surcharge de la fonction cos de math pour les parametres
222 original_cos=math.cos
223 Unop.opmap['cos']=lambda x: original_cos(x)
224 def cos(f): return Unop('cos', f)
227 #surcharge de la fonction sqrt de math pour les parametres
228 original_sqrt=math.sqrt
229 def sqrt(f): return Unop('sqrt', f)
230 Unop.opmap['sqrt']=lambda x: original_sqrt(x)
233 #surcharge de la fonction ceil de math pour les parametres
234 original_ceil=math.ceil
235 Unop.opmap['ceil']=lambda x: original_ceil(x)
236 def ceil(f): return Unop('ceil', f)