MathML

Methods are provided to parse and generate expressions in Content MathML. Presentation MathML can also be generated (but not parsed).

Parsing

myokit.formats.mathml.parse_mathml_etree(element, name_factory, number_factory, free_variables={})

Parses a MathML expression and returns a myokit.Expression.

Arguments:

element
An xml.etree.ElementTree.Element (or similar) to start parsing from. Must be an <apply> element.
name_factory
A callable with arguments (name_as_string, element) that returns myokit.Name objects.
number_factory
A callable with arguments (number_as_float, element) that returns myokit.Number objects. Note that element can be None for numbers that have no corresponding <cn> element.
free_variables
All Name objects for free variables in derivative expressions will be added to this set.
myokit.formats.mathml.parse_mathml_string(s)

Parses a MathML string that should contain a single expression.

class myokit.formats.mathml.MathMLError(message, element=None)

Raised if an error occurs during MathML import.

The argument element can be used to pass in an element that caused the error.

class myokit.formats.mathml.MathMLParser(variable_factory, number_factory, free_variables={})

Parses MathML expressions into myokit.Expression objects.

Arguments:

name_factory
A callable with arguments (name_as_string, element) that returns myokit.Name objects.
number_factory
A callable with arguments (number_as_float, element) that returns myokit.Number objects. Note that element can be None for numbers that have no corresponding <cn> element.
free_variables
All Name objects for free variables in derivative expressions will be added to this set.

This is not a validating parser: if the MathML is invalid the method’s behaviour is undefined.

The following MathML elements are recognised:

Literals and references

<ci>
Is converted to a myokit.Name by passing the contents of the ci tag to the name_factory.
<diff> (with <bvar> and <degree>)
Becomes a myokit.Derivative. Only first-order derivatives are supported. To check if the derivatives are all time-derivatives, the derivative post-processing function can be used.
<cn>
Becomes a myokit.Number. To process units which may be present in the tag’s attributes (esp. in CellML) the number post-processing function can be used.
<csymbol>
Is converted to a myokit.Name by passing the contents of its definitionURL to the name_factory. Note that csymbols representing operators or functions are not supported.

Algebra

<plus>
Becomes a myokit.PrefixPlus, a :class`myokit.Plus` or a tree of myokit.Plus elements.
<minus>
Becomes a myokit.PrefixMinus, a :class`myokit.Minus` or a tree of myokit.Minus elements.
<times>
Becomes a myokit.Multiply or a tree of myokit.Multiply elements.
<divide>
Becomes a myokit.Divide or a tree of myokit.Divide elements.
<apply>
Used to indicate the tree structure of the equation. These get translated but don’t have a Myokit counterpart.

Functions

<power>
Becomes a myokit.Power.
<root> (with <degree>)
Becomes a myokit.Sqrt.
<exp>
Becomes a myokit.Exp.
<ln>
Becomes a myokit.Log.
<log> (with <logbase>)
Becomes a myokit.Log10 or a myokit.Log.
<abs>
Becomes a myokit.Abs.
<floor>
Becomes a myokit.Floor.
<ceiling>
Becomes a myokit.Ceil.
<quotient>
Becomes a myokit.Quotient.
<rem>
Becomes a myokit.Remainder.

Trigonometry

<sin>, <cos> and <tan>
Become myokit.Sin, myokit.Cos and myokit.Tan.
<arcsin>, <arccos> and <arctan>
Become myokit.ASin, myokit.ACos and myokit.ATan.
<csc>, <sec> and <cot>
Become 1/sin, 1/cos and 1/tan.
<arccsc>, <arcsec> and <arccot>
Become asin(1/x), acos(1/x) and atan(1/x).

Hyperbolic trigonometry

<sinh>
Becomes 0.5 * (exp(x) - exp(-x)).
<cosh>
Becomes 0.5 * (exp(x) + exp(-x)).
<tanh>
Becomes (exp(2 * x) - 1) / (exp(2 * x) + 1).
<arcsinh>
Becomes log(x + sqrt(1 + x*x)).
<arccosh>
Becomes log(x + sqrt(x + 1) * sqrt(x - 1)).
<arctanh>
Becomes 0.5 * (log(1 + x) - log(1 - x)).
<csch>
Becomes 2 / (exp(x) - exp(-x)).
<sech>
Becomes 2 / (exp(x) + exp(-x)).
<coth>
Becomes (exp(2 * x) + 1) / (exp(2 * x) - 1).
<arccsch>
Becomes log(sqrt(1 + 1 / x^2) + 1 / x).
<arcsech>
Becomes log(sqrt(1 / x - 1) * sqrt(1 / x + 1) + 1 / x)
<arccoth>
Becomes 0.5 * (log(1 + 1/x) - log(1 - 1/x)).

Logic and relations

<piecewise>, <piece> and <otherwise>
Becomes a myokit.Piecewise.
<and>, <or> and <not>
Become myokit.And, myokit.Or and myokit.Not.
<xor>
Becomes (x or y) and not(x and y)
<eq> and <neq>
Becomes myokit.Equal and NotEqual.
<lt> and <gt>
Become myokit.Less and myokit.More.
<leq> and <geq>
Become myokit.LessEqual and myokit.MoreEqual.

Constants

<pi>
Becomes 3.14159265358979323846
<exponentiale>
Becomes exp(1)
<true>
Becomes 1
<false>
Becomes 0
parse(element)

Parses a MathML expression, rooted in the given <apply> element, and returns a myokit.Expression.

Arguments:

element
An xml.etree.ElementTree.Element (or similar) to start parsing from. Must be an <apply> element.

Writing expressions

myokit.formats.mathml.ewriters()

Returns a dict of all expression writers available in this module.

class myokit.formats.mathml.MathMLExpressionWriter

This ExpressionWriter translates Myokit expressions to Content MathML or Presentation MathML.

eq(eq, element=None)

Converts an equation to a string.

The optional argument element can be used to pass in an lxml.etree.ElementTree element. If given, this element will be updated with the generated xml and nothing will be returned.

ex(e, element=None)

Converts an expression to a string.

The optional argument element can be used to pass in an lxml.etree.ElementTree element. If given, this element will be updated with the generated xml and nothing will be returned.

set_lhs_function(f)
Sets a naming function, will be called to get the variable name from a
myokit.Name (derivatives will be handled separately).

The argument f should be a function that takes a myokit.Name as input and returns a string.

set_mode(presentation=False)

Enables or disables Presentation MathML mode.

set_time_variable(time)

Sets the time variable to use for this expression writer’s derivatives.