# MathML¶

Export of model equations to a Content and Presentation MathML is provided by the mathml module. In addition, a general purpose MathML parsing method is included.

## API¶

myokit.formats.mathml.parse_mathml(s)

Parses a mathml string that should contain a single expression.

myokit.formats.mathml.parse_mathml_rhs(node, var_table=None, logger=None, number_post_processor=None, derivative_post_processor=None)

Takes a MathML node node (using the xml.dom.Node interface) and parses its contents into a myokit.Expression.

Not all of MathML is supported (so no integrals, set theory etc.) but only a subset common to electrophysiology. In addition, some not-so-common elements are supported because they’re allowed appear in CellML documents.

Variable names will be returned as strings, unless the optional dict argument var_table is given. Note that the myokit.VarOwner classes support the dict interface.

If the argument logger is given this will be used to log messages to, assuming the myokit.formats.TextLogger interface.

Optional post-processing of numbers (<cn> tags) can be added by passing in a callable number_post_processor(tag, number). This will be called after parsing each <cn> tag with the original node as the first argument (as an xml.dom.minidom node), and the created number object as the second (as a myokit.Number). The function must return a new myokit.Number object.

Optional checking of derivatives (<diff> tags) can be added by passing in a callable derivative_post_processor(time). This will be called with the myokit.Name representing the variable with respect to which the derivative is being taken. This allows importers to ensure only time-derivatives are being loaded.

The following MathML elements are recognised:

Literals and references

<ci>
Becomes a myokit.Name.
<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.

Algebra

<plus>
Becomes a myokit.PrefixPlus, a :classmyokit.Plus or a tree of myokit.Plus elements.
<minus>
Becomes a myokit.PrefixMinus, a :classmyokit.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

There are a few elements supported by CellML, but not by Myokit.

<semantics>, <annotation> and <annotation-xml>
These are not present in any electrophysiology model in the database.
<notanumber> and <infinity>
These have no place in an ODE.
<factorial>
There is no cardiac electrophysiology model in the database that uses these. Plus, factorials require the idea of integers (Myokit only has Reals) and only factorial(x) for x in [0,1,2,…,12] can be calculated without integer overflows.

Finally, Myokit, but not CellML, supports quotients and remainders.

myokit.formats.mathml.exporters()

Returns a dict of all exporters available in this module.

class myokit.formats.mathml.HTMLExporter

This Exporter generates a HTML file displaying a model’s equations. The equations are encoded using Presentation MathML. This format can be viewed in most modern browsers, but is less suitable as an exchange format.

info()

This should be implemented by each subclass.

logger()

Returns this exporter’s TextLogger.

model(path, model, protocol=None)

Export to a html document.

runnable(path, model, protocol=None, *args)

Exports a myokit.Model and optionally a myokit.Protocol to something that can be run or compiled.

The output will be stored in the directory path. A myokit.ExportError will be raised if any errors occur.

supports_model()

Returns True.

supports_runnable()

Returns True if this exporter supports export of a model and optional protocol to a runnable piece of code.

class myokit.formats.mathml.XMLExporter

This Exporter generates an XML file containing a model’s equations, encoded in Content MathML. This is an XML format containing the bare equations, without any formatting. It can be used to exchange equations with MathML supporting applications.

info()

This should be implemented by each subclass.

logger()

Returns this exporter’s TextLogger.

model(path, model, protocol=None)

Export the model to an xml document.

runnable(path, model, protocol=None, *args)

Exports a myokit.Model and optionally a myokit.Protocol to something that can be run or compiled.

The output will be stored in the directory path. A myokit.ExportError will be raised if any errors occur.

supports_model()

Returns True.

supports_runnable()

Returns True if this exporter supports export of a model and optional protocol to a runnable piece of code.

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 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 ElementTree element. If given, this element will be updated with the generated xml and nothing will be returned.

set_element_tree_class(et)

By default this ExpressionWriter uses the xml.etree.cElementTree module. This method can be used to change this behaviour by passing in a reference to a different implementation.

set_lhs_function(f)
Sets a naming function, will be called to get the variable name from a
myokit.LhsExpression object.

The argument f should be a function that takes an LhsExpression 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.

class myokit.formats.mathml.MathMLError(message)

Raised if an error occurs during MathML import.