API Reference

Parsing

MathML.parse_node — Function
parse_node(node)

Parse one supported MathML XML element into its Symbolics representation.

Arguments

  • node: An XML element whose tag is one of the MathML tags implemented by MathML.jl, including apply, cn, ci, piecewise, math, and lambda.

Returns

A scalar symbolic value, symbolic expression, callable generated from <lambda>, or vector, depending on the MathML tag.

Errors

Throws a KeyError for unsupported element names.

Examples

julia> parse_node(xml"<apply><plus/><cn>2</cn><cn>3</cn></apply>")
5.0
source
MathML.parse_str — Function
parse_str(str::AbstractString)

Parse a MathML XML string into a Symbolics expression.

Arguments

  • str: A well-formed XML string whose root is a supported MathML element.

Returns

The value returned by parse_node for the document root.

Examples

julia> parse_str("<apply><plus/><cn>2</cn><cn>3</cn></apply>")
5.0
source
MathML.parse_file — Function
parse_file(filename)

Read a MathML XML file and parse the document root into a Symbolics expression.

Arguments

  • filename: Path to a readable XML file whose root is a supported MathML element.

Returns

The value returned by parse_node for the document root.

Examples

julia> filename = tempname(); write(filename, "<apply><times/><cn>3</cn><cn>2</cn></apply>"); parse_file(filename)
6.0
source
MathML.parse_apply — Function
parse_apply(node)

Parse a MathML <apply> element into a Symbolics expression.

Arguments

  • node: An <apply> element whose first child identifies an operation supported by MathML.jl and whose remaining children are its operands.

Returns

The result of applying the represented operation to recursively parsed operands.

Errors

Throws an error when node is not an <apply> element or its operation is unsupported.

Examples

julia> parse_apply(xml"<apply><times/><cn>3</cn><cn>4</cn></apply>")
12.0
source
MathML.parse_cn — Function
parse_cn(node)

Parse a MathML <cn> numeric-constant element.

Arguments

  • node: A <cn> element. Plain text is parsed as Float64; a type attribute with a <sep/> child supports e-notation, rational, complex-cartesian, and complex-polar constants.

Returns

A numeric value represented by the element.

Examples

julia> parse_cn(xml"<cn>2.5</cn>")
2.5
source
MathML.parse_ci — Function
parse_ci(node)

Parse a MathML <ci> identifier element as a Symbolics variable.

Arguments

  • node: A <ci> element whose text content is the identifier name. Surrounding whitespace is ignored.

Returns

A Symbolics.Num variable.

Examples

julia> string(parse_ci(xml"<ci> x </ci>"))
"x"
source
MathML.parse_bvar — Function
parse_bvar(node)

Parse a MathML <bvar> bound-variable element.

Arguments

  • node: A <bvar> element containing an identifier and, optionally, a <degree>.

Returns

A tuple (variable, degree). The degree defaults to 1.

Examples

julia> parse_bvar(xml"<bvar><ci>x</ci><degree><cn>2</cn></degree></bvar>")
(x, 2.0)
source
MathML.parse_lambda — Function
parse_lambda(node)

Parse a MathML <lambda> element as a Julia callable.

Arguments

  • node: A <lambda> element with one or more <bvar> children followed by an <apply> body.

Returns

A generated function that evaluates the parsed symbolic body for the declared bound variables.

Examples

julia> f = parse_lambda(xml"<lambda><bvar><ci>x</ci></bvar><apply><plus/><ci>x</ci><cn>1</cn></apply></lambda>"); f(2)
1-element Vector{Float64}:
 3.0
<lambda>
  <bvar> x1 </bvar><bvar> xn </bvar>
   expression-in-x1-xn
</lambda>
source
MathML.parse_piecewise — Function
parse_piecewise(node)

Parse a MathML <piecewise> node into a nested symbolic conditional expression.

Arguments

  • node: A <piecewise> element containing zero or more <piece> children and an optional <otherwise> child. Each <piece> contains a value followed by its condition.

Returns

A nested ifelse symbolic expression. When <otherwise> is absent, the fallback is 0.0.

Examples

julia> parse_piecewise(xml"<piecewise><piece><cn>1</cn><apply><gt/><cn>2</cn><cn>0</cn></apply></piece><otherwise><cn>0</cn></otherwise></piecewise>")
1.0
source

XML Utilities

MathML.extract_mathml — Function
extract_mathml(input)

Extract all MathML <math> elements from an XML document or element.

Arguments

  • input: A filename as an AbstractString, an XML document with a root property, or an XML element in a document that uses the MathML namespace.

Returns

A vector of XML elements. Before extraction, <eq> nodes below <piecewise> are renamed to <equal> so later parsing distinguishes equality predicates from assignments.

Examples

julia> length(extract_mathml(parsexml("<root xmlns='http://www.w3.org/1998/Math/MathML'><math><cn>1</cn></math></root>")))
1
source
MathML.mathml_to_nums — Function
mathml_to_nums(input)

Return the unique symbolic identifiers declared by MathML <ci> elements.

Arguments

  • input: A filename as an AbstractString, an XML document with a root property, or an XML element rooted in the MathML namespace.

Returns

A Vector{Symbolics.Num} in first-occurrence order. Repeated <ci> values occur once.

Examples

julia> mathml_to_nums(parsexml("<math xmlns='http://www.w3.org/1998/Math/MathML'><ci>x</ci><ci>x</ci><ci>y</ci></math>"))
2-element Vector{Symbolics.Num}:
 x
 y
source
MathML.@xml_str — Macro
xml"..."

Parse an XML string literal into its root element at runtime.

Arguments

  • String literal: Well-formed XML whose root is returned as an EzXML element.

Examples

julia> xml"<cn>2</cn>".name
"cn"
source
MathML.@MathML_str — Macro
MathML"..."

Parse a MathML string literal into its Symbolics representation.

Arguments

  • String literal: MathML XML supported by parse_str.

Examples

julia> MathML"<apply><plus/><cn>2</cn><cn>3</cn></apply>"
5.0
source

Generation

MathML.to_MathML — Function
to_MathML(expression)

Convert a Julia algebraic expression to an EzXML MathML element.

Arguments

  • expression: An Expr or Symbolics.Num containing calls, numbers, and symbols. Supported operators are +, -, *, ^, sin, and cos.

Returns

An EzXML element representing the expression. Nested calls are represented with MathML <apply> elements, numbers with <cn>, and variables with <ci>.

Errors

Throws an ArgumentError when an expression contains an unsupported argument type.

Examples

julia> print(to_MathML(:(x + 2)))
<apply><ci>x</ci><cn>2</cn></apply>
Note

This converter supports a deliberately small algebraic subset of Julia syntax.

source