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.
parse_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:
formula_expression
¶
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. |
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
¶
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, |
set[str]
|
model. Constants like |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the formula is not valid math, see
|
evaluate_formula
¶
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 |
Any
|
formula is not defined. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the formula is not valid math, see
|
expression_to_astnode
¶
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
¶
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 |
str
|
natural logarithm is |
str
|
formula of SBML. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the expression has no MathML of SBML, see
|