MathDOM is a set of Python 2.4 modules (using PyXML or lxml, and pyparsing)
mathematical terms as a Content
DOM. It currently parses
MathML and literal infix terms into a DOM or lxml document and
writes out MathML and literal infix/prefix/postfix/Python terms.
The DOM elements are enhanced by domain specific methods that make
using the DOM a little easier. Input parsers and output converters are
Newer versions simplify the portability of code between the PyXML and lxml versions. They also extend the latter with an XSLT-based output filter for Presentational MathML and RelaxNG-based document validation. PyXML does not support any of these.
You can call it the shortest path between different term representations and a Content MathML DOM. Ever noticed the annoying differences between terms in different programming languages? Build your application around MathDOM and stop caring about the term representation that users prefer or that your machine can execute. If you need a different representation, add a converter, but don't change the model of your application. Literal terms are connected through an intermediate AST step that makes writing converters for C/Fortran/SQL/yourfavourite easier.
|>>> from mathml.lmathdom import MathDOM||# use lxml implementation|
|>>> doc = MathDOM.fromString("+2^x+4*-5i/6","infix_term")||# parse infix term|
|>>> for apply_tag in doc.xpath(u'//math:apply[math:plus]'):||# replace '+' with '-'|
|>>> [ n.value() for n in doc.xpath(u'//math:cn') ]||# find numbers|
|[2, 4, Complex(0-5j), 6]|
|>>> from mathml.utils import pyterm||# register Python term builder|
|>>> doc.serialize("python")||# serialize to Python term|
|u'2 ** x - 4 * (-5j) / 6'|
MathML is an XML language for representing mathematics. Content MathML is a part of that specification that focuses on the semantics rather than the representation of mathematical expressions. MathML has received a lot of support in mathematical software as well as web browsers and represents a comfortable layer for the semantic exchange of mathematics - see http://www.w3.org/Math/Software/. Note that Content MathML support in MathDOM is not complete, as the primary focus is on term representation.
© Stefan Behnel, 2005