2

I would like to be able to convert a python mathematical expression into content MathML using Python. The goal would be to turn an expression like (a*x^2 + b*x + c)/(2*a) into the content MathML example in the Wikipedia article on MathML.

The asciimathml package will generate display MathML, but as far as I can tell, not content MathML.

I have not been able to google up an existing package, or even suggestions for how to do it.

Thanks in advance...

abalter
  • 9,663
  • 17
  • 90
  • 145

2 Answers2

2

MathDOM: http://mathdom.sourceforge.net/

Does exactly what you want. Quoting the page:

MathDOM is a set of Python 2.4 modules (using PyXML or lxml, and pyparsing) that import mathematical terms as a Content MathML 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 easily extensible.

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.

Edit: Found here. http://www.w3.org/Math/Software/mathml_software_cat_converters.html#Imathdom

Community
  • 1
  • 1
agf
  • 171,228
  • 44
  • 289
  • 238
  • I did look at that, but I can't find any documentation showing how to convert a string like exp(-x^2) to content MathML. HOWEVER, I did find that SymPy can do it! Scroll down to "MathML". [link](http://docs.sympy.org/0.7.0/tutorial.html#printing) – abalter Jul 20 '11 at 18:10
  • Right on the page I linked it has `from mathml.lmathdom import MathDOM # use lxml implementation doc = MathDOM.fromString("+2^x+4*-5i/6","infix_term")`, does this not do what you want? – agf Jul 20 '11 at 18:22
  • Thanks for your answer. I the "infix", "prefix" stuff and the lxml and PyXML stuff made it all seem quite complicated. I'm really not very sophisticated. SymPy really does the trick. It even handles functions, derivatives, integrals, etc. The one thing I haven't figured out how to do yet is equations (i.e. with "=" signs), e.g. "x(t) = A*sin(2*pi*f*t)". – abalter Jul 22 '11 at 04:16
1

See comments relating to SymPy. Solved my problem.

abalter
  • 9,663
  • 17
  • 90
  • 145