def test_equation_rewrite(self): """ Test the equation rewriting function """ variables = dict() variables['x'] = Variable('x') variables['y'] = Variable('y') self.assertEquals('x - y', _rewrite(variables, {}, 'x - y')) self.assertEquals('xx - y', _rewrite(variables, {}, 'xx - y')) self.assertEquals('xx - yx', _rewrite(variables, {}, 'xx - yx')) self.assertEquals('xx(0) - yx', _rewrite(variables, {}, 'xx(0) - yx')) self.assertEquals('_series_acc(x,-1)', _rewrite(variables, {}, 'x(-1)')) self.assertEquals('_series_acc(x,-t)', _rewrite(variables, {}, 'x(-t)')) parameters = dict() parameters['a'] = Parameter('a') parameters['b'] = Parameter('b') self.assertEquals('_series_acc(a,-1)', _rewrite({}, parameters, 'a(-1)'))
def evaluate(self, equation): """ Evaluates an arbitrary function using the current values of the variables. Parmeters: eqn: Returns: The value of the expression. """ equation = _rewrite(self.variables, self.parameters, equation) expr = parse_expr(equation, self._local_context, transformations=(factorial_notation, auto_number)) expr = sympify(expr).subs(self._get_context()) for func in _RT_FUNCS: expr = expr.replace(func[2], func[1]) if sympify(expr).is_number: break return float(expr)