Math-expressions is client- or server-side JavaScript library for
parse expressions like sin^2 (x^3)
and do some basic computer
algebra with them, like symbolic differentiation and numerically
identifying equivalent expressions.
There is a demo available which focuses on the equality testing.
Include the script in build/math-expressions.js
on your page.
<script type="text/javascript" src="math-expressions.js"></script>`
This adds MathExpression
to the global namespace, so you can then perform the following parsing and equality-testing.
var f = MathExpression.fromText("sin^2 (x^3)");
console.log(f.tex());
var g = MathExpression.fromText("sin^2 x + cos^2 x");
var h = MathExpression.fromText("1");
console.log( g.equals(h) );
var g = MathExpression.fromText("x + x^2");
var h = MathExpression.fromText("x + x^3");
console.log( g.equals(h) );
For server-side use, you can load the package as usual via var MathExpression = require('math-expressions');
.
The pre-built library is stored in build/math-expressions.js
. This
is packaged in a "unified" module format, so it can be treated as an
AMD-style or a CommonJS-style module (and imported via a module loader
like RequireJS). If you are not using a module loader, you can import
it via
<script type="text/javascript" src="math-expressions.js"></script>`
to add MathExpression
to the global namespace.
Convert string
to expression through the text parser. For example, var f = MathExpression.fromText('x^2');
will result in f
representing the expression x^2
. Then f.evaluate({x:3})
is 9.
This is often used for student-facing text, considering that even MathExpression.fromText('sin^2 x')
does the right thing and produces the square of the sine function. Exceptions are thrown when string
does not parse.
Convert string
to an expression through the LaTeX parser. For example, var f = MathExpression.fromLatex('\\frac{x+1}{2}');
will result in f
representing the expression (x+1)/2
. Then f.evaluate({x:3})
is 2.
Produce a textual representation of expression
. For example, MathExpression.fromText( f.toString() )
recreates f
.
Produce a LaTeX representation of expression
. For example, if f
is MathExpression.fromText('x^10')
, then f.toLatex()
is x^{10}
since LaTeX requires braces for exponents.
Return the list of variables used in expression
. For example, if f
is MathExpression.fromText('x + cos(y)')
, then f.variables()
is ['x','y']
.
Determine whether expression
and another
represent the "same" expression.
The current algorithm randomly samples the two expressions in a neighborhood of the real line of the complex plane and demands approximate numeric equality most of the time for random assignments to the variables. This is, quite admittedly, not a perfect algorithm, and it is likely to be replaced in a future version of this library.
Symbolically differentiate expression
with respect to the given variable
. For example, if f
is MathExpression.fromText('x^10')
, then f.derivative('x')
is 10 x^9
.
Assuming you have git clone
ed the repository and npm install
ed to
populate node_modules
with jasmine, you can then run the tests with
npm test
.
Most of the tests are used to determine if expression.equals(another)
does the expected thing on student input.
Math-expressions is dual-licensed under GPLv3 and under Apache Version 2.0.