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:
elementAn
xml.etree.ElementTree.Element(or similar) to start parsing from. Must be an<apply>element.name_factoryA callable with arguments
(name_as_string, element)that returnsmyokit.Nameobjects.number_factoryA callable with arguments
(number_as_float, element)that returnsmyokit.Numberobjects. Note thatelementcan beNonefor numbers that have no corresponding<cn>element.free_variablesAll
Nameobjects 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
elementcan 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.Expressionobjects.Arguments:
name_factoryA callable with arguments
(name_as_string, element)that returnsmyokit.Nameobjects.number_factoryA callable with arguments
(number_as_float, element)that returnsmyokit.Numberobjects. Note thatelementcan beNonefor numbers that have no corresponding<cn>element.free_variablesAll
Nameobjects 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 behavior is undefined.
The following MathML elements are recognised:
Literals and references
<ci>Is converted to a
myokit.Nameby passing the contents of thecitag to thename_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.Nameby passing the contents of itsdefinitionURLto thename_factory. Note thatcsymbolsrepresenting operators or functions are not supported.
Algebra
<plus>Becomes a
myokit.PrefixPlus, a :class`myokit.Plus` or a tree ofmyokit.Pluselements.<minus>Becomes a
myokit.PrefixMinus, a :class`myokit.Minus` or a tree ofmyokit.Minuselements.<times>Becomes a
myokit.Multiplyor a tree ofmyokit.Multiplyelements.<divide>Becomes a
myokit.Divideor a tree ofmyokit.Divideelements.<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.Log10or amyokit.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.
Minimum and maximum
<max>and<min>Are converted to if-statements. Only binary comparisons are supported, and the resulting
ifwill evaluate its operands twice, which may be inefficient if either operand is a complex expression.
Trigonometry
<sin>,<cos>and<tan>Become
myokit.Sin,myokit.Cosandmyokit.Tan.<arcsin>,<arccos>and<arctan>Become
myokit.ASin,myokit.ACosandmyokit.ATan.<csc>,<sec>and<cot>Become
1/sin,1/cosand1/tan.<arccsc>,<arcsec>and<arccot>Become
asin(1/x),acos(1/x)andatan(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(x*x + 1)).<arccosh>Becomes
log(x + sqrt(x*x - 1)).<arctanh>Becomes
0.5 * log((1 + x) / (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(1 / x + sqrt(1 / x^2 + 1)).<arcsech>Becomes
log(1 / x + sqrt(1 / x^2 - 1))<arccoth>Becomes
0.5 * log((x + 1) / (x - 1)).
Logic and relations
<piecewise>,<piece>and<otherwise>Becomes a
myokit.Piecewise.<and>,<or>and<not>Become
myokit.And,myokit.Orandmyokit.Not.<xor>Becomes
(x or y) and not(x and y)<eq>and<neq>Becomes
myokit.EqualandNotEqual.<lt>and<gt>Become
myokit.Lessandmyokit.More.<leq>and<geq>Become
myokit.LessEqualandmyokit.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 amyokit.Expression.Arguments:
elementAn
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
ExpressionWritertranslates Myokitexpressionsto Content MathML or Presentation MathML.- eq(eq, element=None)¶
Converts an equation to a string.
The optional argument
elementcan be used to pass in anlxml.etree.ElementTreeelement. 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
elementcan be used to pass in anlxml.etree.ElementTreeelement. 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
fshould be a function that takes amyokit.Nameas 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.