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 __truediv__(self, other): return Binop('/', self, other)
42 def __rdiv__(self, other): return Binop('/', other, self)
43 def __pow__(self, other): return Binop('**', self, other)
44 def __rpow__(self, other): return Binop('**', other, self)
45 def __getitem__(self,i):return Binop('[]',self,i)
48 opmap = { '+': 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,
53 '[]': lambda a, b: a[b] ,
55 def __init__(self, op, value1, value2):
57 self.values = mkf(value1), mkf(value2)
60 return "%s[%s]" % (self.values[0], self.values[1])
62 return "(%s %s %s)" % (self.values[0], self.op, self.values[1])
65 return "%s[%s]" % (self.values[0], self.values[1])
67 return "(%s %s %s)" % (self.values[0], self.op, self.values[1])
69 result= self.opmap[self.op](self.values[0].eval(),
70 self.values[1].eval())
71 while isinstance(result,Formula):
74 def __adapt__(self,validator):
75 return validator.adapt(self.eval())
79 opmap = { '-': lambda x: -x,
80 'abs': lambda x: abs(x),
82 def __init__(self, op, arg):
86 return "%s(%s)" % (self._op, self._arg)
88 return "%s(%s)" % (self._op, self._arg)
90 return self.opmap[self._op](self._arg.eval())
91 def __adapt__(self,validator):
92 return validator.adapt(self.eval())
95 def __init__(self, nom, op, arg):
100 self._arg.append(mkf(a))
119 class Constant(Formula):
120 def __init__(self, value): self._value = value
121 def eval(self): return self._value
122 def __str__(self): return str(self._value)
123 def __adapt__(self,validator):
124 return validator.adapt(self._value)
126 class Variable(Formula):
127 def __init__(self,name,value):
130 def eval(self): return self._value
131 def __repr__(self): return "Variable('%s',%s)" % (self._name, self._value)
132 def __str__(self): return self._name
133 def __adapt__(self,validator):
134 return validator.adapt(self._value)
137 if isinstance(f,Formula):
139 elif type(f) in (types.ListType, ):
140 f=[Eval(i) for i in f]
141 elif type(f) in (types.TupleType,):
142 f=tuple([Eval(i) for i in f])
146 #surcharge de la fonction cos de Numeric pour les parametres
147 original_ncos=Numeric.cos
148 def cos(f): return Unop('ncos', f)
149 Unop.opmap['ncos']=lambda x: original_ncos(x)
152 #surcharge de la fonction sin de Numeric pour les parametres
153 original_nsin=Numeric.sin
154 def sin(f): return Unop('nsin', f)
155 Unop.opmap['nsin']=lambda x: original_nsin(x)
158 #surcharge de la fonction array de Numeric pour les parametres
159 original_narray=Numeric.array
160 def array(f,*tup,**args):
161 """array de Numeric met en défaut la mécanique des parametres
162 on la supprime dans ce cas. Il faut que la valeur du parametre soit bien définie
164 return original_narray(Eval(f),*tup,**args)
167 #surcharge de la fonction sin de math pour les parametres
168 original_sin=math.sin
169 def sin(f): return Unop('sin', f)
170 Unop.opmap['sin']=lambda x: original_sin(x)
173 #surcharge de la fonction cos de math pour les parametres
174 original_cos=math.cos
175 Unop.opmap['cos']=lambda x: original_cos(x)
176 def cos(f): return Unop('cos', f)
179 #surcharge de la fonction sqrt de math pour les parametres
180 original_sqrt=math.sqrt
181 def sqrt(f): return Unop('sqrt', f)
182 Unop.opmap['sqrt']=lambda x: original_sqrt(x)
185 #surcharge de la fonction ceil de math pour les parametres
186 original_ceil=math.ceil
187 Unop.opmap['ceil']=lambda x: original_ceil(x)
188 def ceil(f): return Unop('ceil', f)