Skip to content

mathml

Evaluation of formulas and MathML expressions.

A Data of type FUNCTION is a formula of other data, see sbmlsim.data. The formula is parsed into the abstract syntax tree of libsedml, which implements the L3 formula syntax of SBML, and evaluated on the arrays of the variables with sympy.

formula_expression, formula_symbols and evaluate_formula read a formula with libsbml and sbmlmath, which make a symbol of every identifier. expr_from_formula hands the text of a formula to sympify, which reads beta, gamma, lambda, S or I as the functions and constants of sympy and not as identifiers of a model. expression_to_astnode and expression_to_formula are the way back, from a sympy expression to the math of SBML.

formula_to_astnode

formula_to_astnode(formula)

Parse ASTNode from formula.

astnode_to_formula

astnode_to_formula(astnode)

Write ASTNode as formula.

parse_mathml_str

parse_mathml_str(mathml_str)

Parse MathML string.

parse_formula

parse_formula(formula)

Parse formula to ASTNode.

parse_astnode

parse_astnode(astnode)

Parse ASTNode.

An AST node in libSBML is a recursive tree structure; each node has a type, a pointer to a value, and a list of children nodes. Each ASTNode node may have none, one, two, or more children depending on its type. There are node types to represent numbers (with subtypes to distinguish integer, real, and rational numbers), names (e.g., constants or variables), simple mathematical operators, logical or relational operators and functions.

see also: http://sbml.org/Software/libSBML/docs/python-api/libsedml-math.html

:param mathml: :return:

expr_from_formula

expr_from_formula(formula)

Parse sympy expression from given formula string.

evaluate

evaluate(astnode, variables)

Evaluate the astnode with values.

replace_piecewise

replace_piecewise(formula)

Replace libsedml piecewise with sympy piecewise.

formula_expression

formula_expression(formula)

Parse an L3 formula of SBML into a sympy expression.

Every identifier of the formula is a symbol of its name, also the identifiers which are functions or constants of sympy (beta, gamma, lambda, S, I). The time of the model is the symbol time. The constant avogadro stays the csymbol of SBML, which is not an identifier: formula_symbols does not list it and evaluate_formula gives it its value. A logarithm with a base is a quotient of natural logarithms, rem and quotient truncate as they do in SBML.

Parameters:

Name Type Description Default
formula str

the formula, e.g. prey + (alpha - 1.3).

required

Returns:

Type Description
Basic

The expression.

Raises:

Type Description
ValueError

if the formula is empty, is not valid math (units of a number are not) or uses a function which is not a function of the MathML of SBML.

formula_symbols

formula_symbols(formula)

Get the identifiers a formula uses.

Parameters:

Name Type Description Default
formula str

the formula, an L3 formula of SBML.

required

Returns:

Type Description
set[str]

The names of the symbols of the formula, time for the time of the

set[str]

model. Constants like pi or avogadro are no identifiers.

Raises:

Type Description
ValueError

if the formula is not valid math, see formula_expression.

evaluate_formula

evaluate_formula(formula, variables)

Evaluate an L3 formula of SBML on the values of its identifiers.

Parameters:

Name Type Description Default
formula str

the formula.

required
variables Mapping[str, Any]

the value of every identifier of the formula, a number or an array. The values of identifiers the formula does not use are not used, but give the shape of the result.

required

Returns:

Type Description
Any

The value of the formula: a float if all values are numbers, else an

Any

array of the shape the values broadcast to, with nan where the

Any

formula is not defined.

Raises:

Type Description
ValueError

if the formula is not valid math, see formula_expression, if an identifier has no value, if the shapes of the values do not broadcast, or if the value of numbers is complex, overflows or divides by zero.

expression_to_astnode

expression_to_astnode(expression)

Convert a sympy expression into the math of SBML.

The numbers of the expression carry no units, so the math is valid in every level of SBML. The symbol time is the time of the model.

Parameters:

Name Type Description Default
expression Basic

the expression.

required

Returns:

Type Description
ASTNode

The syntax tree of the MathML of the expression.

Raises:

Type Description
ValueError

if the expression uses a function the MathML of SBML does not have, e.g. the error function.

expression_to_formula

expression_to_formula(expression)

Write a sympy expression as an L3 formula of SBML.

Parameters:

Name Type Description Default
expression Basic

the expression.

required

Returns:

Type Description
str

The formula, which formula_expression and libsbml read. The

str

natural logarithm is ln: log is the logarithm to the base 10 in a

str

formula of SBML.

Raises:

Type Description
ValueError

if the expression has no MathML of SBML, see expression_to_astnode.