jkd

08-16-2003, 12:33 AM

Alright. Resident moderator Alex Vincent has been after a "toMathML()" method for a while now it seems. So I told him, "Alright, no biggie." :D. Now I gotta deliver.

Content MathML 2.0 is essentially Polish Notation, but in XML. You can directly convert infix expressions (Algebraic notation) to prefix (Polish), however I decided to take a different route and implemented my own algorithm for directly converting an infix expression into an expression tree. And from the tree, I can dynamically evaluate it recursively, pre-order traverse it, post-order traverse it, in-order traverse it, and generate MathML like nothing. :)

So, if you're using Mozilla, check it out here:

http://www.jasonkarldavis.com/math/test.xhtml

The algorithm works fine in any JS-capable browser, however I use DOM2 namespaced node-creation methods that only Mozilla supports for MathML generation. Presentation MathML output will come as soon as I hook up the XSLT stylesheet. Also, support for unary operators and functions (like sin, cos, log), etc will come as soon as I replace the binary tree data structure with an n-ary tree data structure.

You can try this simple example to type in the algebraic input box:

y = 5*x + 2

It's all part of a much bigger plan, so the lack of a UI right now isn't a concern. (The "test.xhtml" filename implies that it is a test page.) I just thought the script might be educational to anyone interested in real computer science and using Javascript to implement and utilize abstract data types.

Oh, and I noticed that I didn't explain the reason why I included "symbolic" in the title... by creating an expression tree, it becomes trivial to perform operations symbolically on it. Differentiation of an expression tree, for example, is exceedingly easy. :)

Alright - to please Mr. Vincent, here is a zip with everything:

www.jasonkarldavis.com/math/math.zip

Content MathML 2.0 is essentially Polish Notation, but in XML. You can directly convert infix expressions (Algebraic notation) to prefix (Polish), however I decided to take a different route and implemented my own algorithm for directly converting an infix expression into an expression tree. And from the tree, I can dynamically evaluate it recursively, pre-order traverse it, post-order traverse it, in-order traverse it, and generate MathML like nothing. :)

So, if you're using Mozilla, check it out here:

http://www.jasonkarldavis.com/math/test.xhtml

The algorithm works fine in any JS-capable browser, however I use DOM2 namespaced node-creation methods that only Mozilla supports for MathML generation. Presentation MathML output will come as soon as I hook up the XSLT stylesheet. Also, support for unary operators and functions (like sin, cos, log), etc will come as soon as I replace the binary tree data structure with an n-ary tree data structure.

You can try this simple example to type in the algebraic input box:

y = 5*x + 2

It's all part of a much bigger plan, so the lack of a UI right now isn't a concern. (The "test.xhtml" filename implies that it is a test page.) I just thought the script might be educational to anyone interested in real computer science and using Javascript to implement and utilize abstract data types.

Oh, and I noticed that I didn't explain the reason why I included "symbolic" in the title... by creating an expression tree, it becomes trivial to perform operations symbolically on it. Differentiation of an expression tree, for example, is exceedingly easy. :)

Alright - to please Mr. Vincent, here is a zip with everything:

www.jasonkarldavis.com/math/math.zip