1 # -*- coding: utf-8 -*-
2 from __future__ import division
8 if type(value) in (type(1), type(1L), type(1.5), type(1j),type("hh")) :
10 elif isinstance(value, Formula):
12 elif type(value) == type([]):
13 return Constant(value)
15 # return Constant(value)
16 raise TypeError, ("Can't make formula from", value)
18 #class Formula(object):
22 if val is None:return 0
27 def __complex__(self): return complex(self.eval())
28 def __int__(self): return int(self.eval())
29 def __long__(self): return long(self.eval())
30 def __float__(self): return float(self.eval())
31 def __pos__(self): return self # positive
32 def __neg__(self): return Unop('-', self)
33 def __abs__(self): return Unop('abs', self)
34 def __add__(self, other): return Binop('+', self, other)
35 def __radd__(self, other): return Binop('+', other, self)
36 def __sub__(self, other): return Binop('-', self, other)
37 def __rsub__(self, other): return Binop('-', other, self)
38 def __mul__(self, other): return Binop('*', self, other)
39 def __rmul__(self, other): return Binop('*', other, self)
40 def __div__(self, other): return Binop('/', self, other)
41 def __rdiv__(self, other): return Binop('/', other, self)
42 def __truediv__(self, other): return Binop('/', self, other)
43 def __rtruediv__(self, other): return Binop('/', other, self)
44 def __floordiv__(self, other): return Binop('//', self, other)
45 def __rfloordiv__(self, other): return Binop('//', other, self)
46 def __pow__(self, other): return Binop('**', self, other)
47 def __rpow__(self, other): return Binop('**', other, self)
48 def __getitem__(self,i):return Binop('[]',self,i)
49 def __cmp__( self, other ): return self.eval().__cmp__(other)
50 def __eq__( self, other ): return self.eval() == other
51 def __ne__( self, other ): return self.eval() != other
52 def __lt__( self, other ): return self.eval() < other
53 def __le__( self, other ): return self.eval() <= other
54 def __gt__( self, other ): return self.eval() > other
55 def __ge__( self, other ): return self.eval() >= other
56 def __hash__(self):return id(self)
59 if isinstance(a,(int,long)) and isinstance(b,(int,long)):
69 opmap = { '+': lambda a, b: a + b,
70 '*': lambda a, b: a * b,
71 '-': lambda a, b: a - b,
73 '//': lambda a, b: a // b,
74 '**': lambda a, b: a ** b,
75 '[]': lambda a, b: a[b] ,
77 def __init__(self, op, value1, value2):
79 self.values = mkf(value1), mkf(value2)
82 return "%s[%s]" % (self.values[0], self.values[1])
84 return "(%s %s %s)" % (self.values[0], self.op, self.values[1])
87 return "%s[%s]" % (self.values[0], self.values[1])
89 return "(%s %s %s)" % (self.values[0], self.op, self.values[1])
91 result= self.opmap[self.op](self.values[0].eval(),
92 self.values[1].eval())
93 while isinstance(result,Formula):
96 def __adapt__(self,validator):
97 return validator.adapt(self.eval())
101 opmap = { '-': lambda x: -x,
102 'abs': lambda x: abs(x),
104 def __init__(self, op, arg):
108 return "%s(%s)" % (self._op, self._arg)
110 return "%s(%s)" % (self._op, self._arg)
112 return self.opmap[self._op](self._arg.eval())
113 def __adapt__(self,validator):
114 return validator.adapt(self.eval())
117 def __init__(self, nom, op, arg):
122 self._arg.append(mkf(a))
141 class Constant(Formula):
142 def __init__(self, value): self._value = value
143 def eval(self): return self._value
144 def __str__(self): return str(self._value)
145 def __adapt__(self,validator):
146 return validator.adapt(self._value)
148 class Variable(Formula):
149 def __init__(self,name,value):
152 def eval(self): return self._value
153 def __repr__(self): return "Variable('%s',%s)" % (self._name, self._value)
154 def __str__(self): return self._name
155 def __adapt__(self,validator):
156 return validator.adapt(self._value)
159 if isinstance(f,Formula):
161 elif type(f) in (types.ListType, ):
162 f=[Eval(i) for i in f]
163 elif type(f) in (types.TupleType,):
164 f=tuple([Eval(i) for i in f])
168 #surcharge de la fonction cos de Numeric pour les parametres
169 original_ncos=Numeric.cos
170 def cos(f): return Unop('ncos', f)
171 Unop.opmap['ncos']=lambda x: original_ncos(x)
174 #surcharge de la fonction sin de Numeric pour les parametres
175 original_nsin=Numeric.sin
176 def sin(f): return Unop('nsin', f)
177 Unop.opmap['nsin']=lambda x: original_nsin(x)
180 #surcharge de la fonction array de Numeric pour les parametres
181 original_narray=Numeric.array
182 def array(f,*tup,**args):
183 """array de Numeric met en défaut la mécanique des parametres
184 on la supprime dans ce cas. Il faut que la valeur du parametre soit bien définie
186 return original_narray(Eval(f),*tup,**args)
189 #surcharge de la fonction sin de math pour les parametres
190 original_sin=math.sin
191 def sin(f): return Unop('sin', f)
192 Unop.opmap['sin']=lambda x: original_sin(x)
195 #surcharge de la fonction cos de math pour les parametres
196 original_cos=math.cos
197 Unop.opmap['cos']=lambda x: original_cos(x)
198 def cos(f): return Unop('cos', f)
201 #surcharge de la fonction sqrt de math pour les parametres
202 original_sqrt=math.sqrt
203 def sqrt(f): return Unop('sqrt', f)
204 Unop.opmap['sqrt']=lambda x: original_sqrt(x)
207 #surcharge de la fonction ceil de math pour les parametres
208 original_ceil=math.ceil
209 Unop.opmap['ceil']=lambda x: original_ceil(x)
210 def ceil(f): return Unop('ceil', f)