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 __add__(self, other): return Binop('+', self, other)
34 def __radd__(self, other): return Binop('+', other, self)
35 def __sub__(self, other): return Binop('-', self, other)
36 def __rsub__(self, other): return Binop('-', other, self)
37 def __mul__(self, other): return Binop('*', self, other)
38 def __rmul__(self, other): return Binop('*', other, self)
39 def __div__(self, other): return Binop('/', self, other)
40 def __truediv__(self, other): return Binop('/', self, other)
41 def __rdiv__(self, other): return Binop('/', other, self)
42 def __pow__(self, other): return Binop('**', self, other)
43 def __rpow__(self, other): return Binop('**', other, self)
44 def __getitem__(self,i):return Binop('[]',self,i)
47 opmap = { '+': lambda a, b: a + b,
48 '*': lambda a, b: a * b,
49 '-': lambda a, b: a - b,
50 '/': lambda a, b: a / b,
51 '**': lambda a, b: a ** b,
52 '[]': lambda a, b: a[b] ,
54 def __init__(self, op, value1, value2):
56 self.values = mkf(value1), mkf(value2)
59 return "%s[%s]" % (self.values[0], self.values[1])
61 return "(%s %s %s)" % (self.values[0], self.op, self.values[1])
64 return "%s[%s]" % (self.values[0], self.values[1])
66 return "(%s %s %s)" % (self.values[0], self.op, self.values[1])
68 result= self.opmap[self.op](self.values[0].eval(),
69 self.values[1].eval())
70 while isinstance(result,Formula):
73 def __adapt__(self,validator):
74 return validator.adapt(self.eval())
78 opmap = { '-': lambda x: -x,
80 def __init__(self, op, arg):
84 return "%s(%s)" % (self._op, self._arg)
86 return "%s(%s)" % (self._op, self._arg)
88 return self.opmap[self._op](self._arg.eval())
89 def __adapt__(self,validator):
90 return validator.adapt(self.eval())
93 def __init__(self, nom, op, arg):
98 self._arg.append(mkf(a))
117 class Constant(Formula):
118 def __init__(self, value): self._value = value
119 def eval(self): return self._value
120 def __str__(self): return str(self._value)
121 def __adapt__(self,validator):
122 return validator.adapt(self._value)
124 class Variable(Formula):
125 def __init__(self,name,value):
128 def eval(self): return self._value
129 def __repr__(self): return "Variable('%s',%s)" % (self._name, self._value)
130 def __str__(self): return self._name
131 def __adapt__(self,validator):
132 return validator.adapt(self._value)
135 if isinstance(f,Formula):
137 elif type(f) in (types.ListType, ):
138 f=[Eval(i) for i in f]
139 elif type(f) in (types.TupleType,):
140 f=tuple([Eval(i) for i in f])
144 #surcharge de la fonction cos de Numeric pour les parametres
145 original_ncos=Numeric.cos
146 def cos(f): return Unop('ncos', f)
147 Unop.opmap['ncos']=lambda x: original_ncos(x)
150 #surcharge de la fonction sin de Numeric pour les parametres
151 original_nsin=Numeric.sin
152 def sin(f): return Unop('nsin', f)
153 Unop.opmap['nsin']=lambda x: original_nsin(x)
156 #surcharge de la fonction array de Numeric pour les parametres
157 original_narray=Numeric.array
158 def array(f,*tup,**args):
159 """array de Numeric met en défaut la mécanique des parametres
160 on la supprime dans ce cas. Il faut que la valeur du parametre soit bien définie
162 return original_narray(Eval(f),*tup,**args)
165 #surcharge de la fonction sin de math pour les parametres
166 original_sin=math.sin
167 def sin(f): return Unop('sin', f)
168 Unop.opmap['sin']=lambda x: original_sin(x)
171 #surcharge de la fonction cos de math pour les parametres
172 original_cos=math.cos
173 Unop.opmap['cos']=lambda x: original_cos(x)
174 def cos(f): return Unop('cos', f)
177 #surcharge de la fonction sqrt de math pour les parametres
178 original_sqrt=math.sqrt
179 def sqrt(f): return Unop('sqrt', f)
180 Unop.opmap['sqrt']=lambda x: original_sqrt(x)
183 #surcharge de la fonction ceil de math pour les parametres
184 original_ceil=math.ceil
185 Unop.opmap['ceil']=lambda x: original_ceil(x)
186 def ceil(f): return Unop('ceil', f)