Skip to content

sciml.formula

The math of SBML as sympy expressions for the network compiler and hybridization.

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

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.