Compiling Expressions
The Compute Engine LaTeX expressions can compile expressions to JavaScript functions!
Introduction
Some expressions can take a long time to evaluate numerically, for example if they contain a large number of terms or involve a loop \((\sum\) or \(\prod\)).
In this case, it is useful to compile the expression into a JavaScript function that can be evaluated much faster.
For example this approximation: \pi \approx \textstyle\sqrt{6\sum^{10^6}_{n=1}\frac{1}{n^2}}
Compiling
To get a compiled version of an expression use the expr.compile()
method:
const expr = ce.parse("2\\prod_{n=1}^{\\infty} \\frac{4n^2}{4n^2-1}");
const fn = expr.compile();
To evaluate the compiled expression call the function returned by
expr.compile()
:
console.log(fn());
// ➔ 3.141592653589793
If the expression cannot be compiled, the compile()
method will throw an error.
Arguments
The function returned by expr.compile()
can be called with an object literal
containing the value of the arguments:
To get a list of the unknows of an expression use the expr.unknowns
property:
Limitations
The calculations are only performed using machine precision numbers.
Complex numbers, arbitrary precision numbers, and symbolic calculations are not supported.
Some functions are not supported.
If the expression cannot be compiled, the compile()
method will throw an
error. The expression can be numerically evaluated as a fallback: