EquationParser

class decida.EquationParser.EquationParser(equation, postfix=False, varlist=None, debug=False, data_function_format=True)

synopsis:

Equation parser class.

EquationParser parses a general algebraic equation, involving numbers, variables, unary and binary operations, and parentheses. It returns a set of simpler, parsed (space-separated) equations with only 1, 2, or 3 token right-hand-side expressions: assignments, unary operations, binary operations.

constructor arguments:

equation (str)

lhs=rhs string to be parsed

postfix (bool, default=False)

if True, generate postfix notation, otherwise infix

varlist (list of str, default=None)

list of variables which are to be treated specially: if variable contains meta-character, it is replaced with a temporary name, then after parsing, re-replaced.

debug (bool, default=False)

if True, print out parsing information

data_function_format (bool, default=True)

if True, print out functions in Data-object-compatible format.

operator precedence (low to high):

&& ||
< <= == != >= >
+ -   (add, sub)
* / %
+ - ! (unary)
^

example:

>>> from decida.EquationParser import EquationParser
>>> ep = EquationParser("z =V(1)* sin(x+3.0)*ID(mp2)",
... varlist=["V(1)", "ID(mp2)"])
>>> eqns = ep.parse()
>>> for eqn in eqns :
>>>     print eqn
zz1003 = x + 3.0
zz1004 = sin zz1003
zz1005 = V(1) * zz1004
z = zz1005 * ID(mp2)

public methods:

ivars()

return list of intermediate variables in the current result.

results :

  • Lists all the intermediate lhsvars created in the set of parsed equations.
parse()

parse the equation and return the result.

results:

  • return set of parsed equations with tokenized right-hand-side expressions of 1, 2 or 3 (space-separated) tokens.