...

View Full Version : Symbolic mathematics in Javascript



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

james.mclellan
09-23-2011, 03:29 PM
I get a 404 error when attempting to connect to the links above.

I had searched here, first, for a few libraries, and not finding anything I felt I could use started my own.

It's located here on GitHub:

https://github.com/jamesmclellan/Symbolic-Math-Library-for-Javascript

And, hopefully, will not be going anywhere as long as GitHub is around.

Presently, it handles: addition, subtraction, multiplication, division, exponents, parentheticals, simple differentials (with and without product rule).

I would be very grateful for contributions to the baseline, if anyone would be so inclined.

sela
10-03-2011, 12:34 PM
i get this:


Not Found

The requested URL /math/test.xhtml was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

martin.donk
09-11-2013, 06:39 PM
I also needed a symbolic parser in javascript so I decided to roll my own. It can use some polishing and maybe some more testing. At present it parses symbolically and can do derivatives. You can get an idea of how it works at http://www.nerdamer.com/demo and the code can be found at https://github.com/nerdamer/nerdamer/tree/differentiation. The math is rendered with mathquill. Hope this helps.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum