openmmtools.utils.math_eval¶
- openmmtools.utils.math_eval(expression, variables=None, functions=None)[source]¶
Evaluate a mathematical expression with variables.
All the functions in the standard module math are available together with - step(x) : Heaviside step function (1.0 for x=0) - step_hm(x) : Heaviside step function with half-maximum convention. - sign(x) : sign function (0.0 for x=0.0)
Available operators are
+
,-
,*
,/
,**
,-x
(negative),&
,and
,|
, andor
The operators ``and`` and ``or`` operate BITWISE and behave the same as ``&`` and ``|`` respectively as this function is not designed to handle logical operations. If you provide sets, they must be as variables.
- Parameters:
- expressionstr
The mathematical expression as a string.
- variablesdict of str: float, optional
The variables in the expression, if any (default is None).
- functionsdict of str: callable function, optional
Additional functions to teach the math eval statement how to handle. Built-in functions are ‘step’, ‘step_hm’, and ‘sign’
- Returns:
- result
The result of the evaluated expression.
Examples
>>> expr = '-((x + ceil(y)) / z * 4 + step(-0.2))**2' >>> vars = {'x': 1, 'y': 1.9, 'z': 3} >>> math_eval(expr, vars) -16.0