optimisation fonction evaluate
This commit is contained in:
parent
29b218835c
commit
e3d19eedcf
1 changed files with 37 additions and 16 deletions
|
|
@ -358,6 +358,7 @@ class Equation:
|
|||
self.right = right
|
||||
self.value = value
|
||||
self.type = "Equation"
|
||||
self.compiled_function = None
|
||||
|
||||
def copy(self):
|
||||
if self.op == Operation.CONST:
|
||||
|
|
@ -409,23 +410,43 @@ class Equation:
|
|||
if self.op == Operation.COS:
|
||||
return Const(-1) * self.value.partial(var_name) * Equation(Operation.SIN, None, None, self.value.copy())
|
||||
|
||||
def evaluate(self, dico):
|
||||
def compile(self):
|
||||
if self.compiled_function is not None:
|
||||
return self.compiled_function
|
||||
|
||||
if self.op == Operation.CONST:
|
||||
return self.value.value
|
||||
self.compiled_function = lambda dico : self.value.value
|
||||
if self.op == Operation.VAR:
|
||||
return dico[self.value.name]
|
||||
self.compiled_function = lambda dico : dico[self.value.name]
|
||||
if self.op == Operation.ADD:
|
||||
return self.left.evaluate(dico) + self.right.evaluate(dico)
|
||||
left_func = self.left.compile()
|
||||
right_func = self.right.compile()
|
||||
self.compiled_function = lambda dico : left_func(dico) + right_func(dico)
|
||||
if self.op == Operation.SUB:
|
||||
return self.left.evaluate(dico) - self.right.evaluate(dico)
|
||||
left_func = self.left.compile()
|
||||
right_func = self.right.compile()
|
||||
self.compiled_function = lambda dico : left_func(dico) - right_func(dico)
|
||||
if self.op == Operation.MULT:
|
||||
return self.left.evaluate(dico) * self.right.evaluate(dico)
|
||||
left_func = self.left.compile()
|
||||
right_func = self.right.compile()
|
||||
self.compiled_function = lambda dico : left_func(dico) * right_func(dico)
|
||||
if self.op == Operation.DIV:
|
||||
return self.left.evaluate(dico) / self.right.evaluate(dico)
|
||||
left_func = self.left.compile()
|
||||
right_func = self.right.compile()
|
||||
self.compiled_function = lambda dico : left_func(dico) / right_func(dico)
|
||||
if self.op == Operation.SIN:
|
||||
return np.sin(self.value.evaluate(dico))
|
||||
value_func = self.value.compile()
|
||||
self.compiled_function = lambda dico : np.sin(value_func(dico))
|
||||
if self.op == Operation.COS:
|
||||
return np.cos(self.value.evaluate(dico))
|
||||
value_func = self.value.compile()
|
||||
self.compiled_function = lambda dico : np.cos(value_func(dico))
|
||||
|
||||
return self.compiled_function
|
||||
|
||||
def evaluate(self, dico):
|
||||
if self.compiled_function is None:
|
||||
self.compile()
|
||||
return self.compiled_function(dico)
|
||||
|
||||
def solve(self, dico, tmax=10, dt=0.01):
|
||||
self.simplify()
|
||||
|
|
|
|||
Loading…
Reference in a new issue