http://qs1969.pair.com?node_id=386064

Zaxo has asked for the wisdom of the Perl Monks concerning the following question:

I want to do some algebraic manipulation, preferably in Perl. I first looked at Math::Expr, which I've had around for a while and fooled with a little, but I now see that M::E has a lot of shortcomings. Its data file format is undocumented, its error checking and handling is inadequate, and it appears to have been long abandoned in an unfinished state.

One of the things M::E does is output a parse tree in MathML. Looking at the MathML specification to see what the module was doing, it dawned on me that a MathML parser ought to be able to do the whole job, with XSLT to do the algebraic transformations and representational output. The xml parse tree for content MathML should look almost like the kind of algebraic parse tree M::E produces. That ought to settle just about all the problems I have with M::E.

Neither CPAN nor the W3 MathML site list an existing perl module specialized for MathML. I'm not very savvy with XML, but this sure seems like an opportunity to learn (this project is a personal itch - no deadline).

So, I have both the Schema and DTD available for MathML, and all the XML resources of CPAN. I have the green monkey book, which I'm sure will lead me where I want to go as soon as I pick a strategy.

What would your preferred approach to this problem be? What strategy would you use to take an xml namespace and its specs and produce a parser and XSLT engine?

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: XML Strategy For Computer Algebra
by idsfa (Vicar) on Aug 26, 2004 at 16:32 UTC

    My suggestion would be to extend   XML::Xalan, a Perl interface to Xalan-C++. This lets you apply a (potentially dynamically generated) XSL transformation to an XML document. Example from the docs:

    use XML::Xalan; my $tr = new XML::Xalan::Transformer; # parse an XML string: my $parsed = $tr->parse_string(<<"XML"); <?xml version="1.0"?> <doc>Hello</doc> XML # compile a stylesheet string: my $compiled = $tr->compile_stylesheet_string(<<"XSLT"); <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="doc"> <out><xsl:value-of select="."/></out> </xsl:template> </xsl:stylesheet> XSLT my $res = $tr->transform_to_data($parsed, $compiled);

    If anyone needs me I'll be in the Angry Dome.
Re: XML Strategy For Computer Algebra
by hsmyers (Canon) on Aug 26, 2004 at 16:27 UTC
    Interesting idea! I'd also look into talking to Mathematica, muMath or MacSyma(sp?) in Perl using your data and possibly the XML structure...

    --hsm

    "Never try to teach a pig to sing...it wastes your time and it annoys the pig."
Re: XML Strategy For Computer Algebra
by wufnik (Friar) on Aug 26, 2004 at 20:30 UTC
    this is a bit of a cheat, but if the problem is to produce the mathML that corresponds to a particular mathematical expression, why not approach the problem via

    humanreadablemath->latex->mathML?

    here are some human readable thoughts on stage 1 transform (humanreadablemath->mathML) and the stage 2 transform (latex->mathML).

    STAGE 1

    the first step is to take create your latex from your original mathematical input.

    you can create .tex files from 'understandable' mathematical expressions via a perl file naturalmath.pl, which can be found at

    http://www.math.missouri.edu/~stephen/naturalmath.

    this will enable full transform from the text $input:
    integral from 0 to infinity of e ^ (-x^2/2)dx = sqrt(pi over 2)
    to latex file test.tex

    STAGE 2

    now you just need to push this through some sort of latex->mathML converter, ie stage 2. are there some?

    apparently yes:

    one such can be found at: http://www.cse.ohio-state.edu/~gurari/docs/mml-00/mml-00.html . ermmm, i have not actually used the above - but apparently 'several' people are working on this, so 'one hopes' there is a reasonable chance of an adequate solution.

    if not, you will still have all of the tex tools to play with creating gifs etc from your resultant tex file. of course, there are demerits to this chaining, but hey, this problem is imho big & nasty...

    best wishes -
    ...wufnik

    -- in the world of the mules there are no rules --
Re: XML Strategy For Computer Algebra
by kvale (Monsignor) on Aug 27, 2004 at 00:41 UTC
    If your goal is to manipulate algebraic expressions, rather than investigate XML and XSL, you may want to try using a perl interface to an already extant symbolic engine. Writing symbolic engines for all but the simplest tasks and getting them correct in all the corner cases is hard. Mathematica had serious bugs in their integration code for years.

    MAXIMA (yes, all caps, this started as a mainframe prog) is one of the original computer algebra systems, It was proprietary for years and was then released as open source. Symaxx is a Perl/Tk interface to MAXIMA that should serve as a nice example for interfacing with the engine.

    -Mark

      Someone already put togther an ASCII to MathML translation program. While I have not had a chance to look at it (and it seems to be written in Javascript) the link may be of use: http://asciimathml.sourceforge.net/
Re: XML Strategy For Computer Algebra
by Solo (Deacon) on Aug 27, 2004 at 13:03 UTC
    XML::LibXML and XML::LibXSLT are my preferred modules for XML work.

    You'll want an XML editor like XML Cooktop or one of the many alternatives on Freshmeat. An editor that allows interactive XPath and XSL transforms will save you a lot of time in the learning curve, IMO.

    --Solo

    --
    You said you wanted to be around when I made a mistake; well, this could be it, sweetheart.