Skip to main content

Compute Engine

A JavaScript library for symbolic computing and numeric evaluation of mathematical expressions.

console.log(evaluate("e^{i\\pi}"));
console.log(expand("(a+b)^2"));
console.log(simplify( "(sin(alpha)**2 + cos(alpha)**2) * (x**2 + 2*x + 1) / (x + 1)" ));

The Compute Engine is for educators, students, scientists, and engineers who need to run technical computing apps in browsers or server-side JavaScript environments like Node.js.

The Compute Engine manipulates math expressions represented with the MathJSON format.

The expression $x^2 + 2x + 1$ is represented in MathJSON as:

["Add", ["Power", "x", 2], ["Multiply", 2, "x"], 1]

The Compute Engine can:

Free Functions

For common operations, use the free functions — no setup required:

console.log(simplify("x+x+1")); console.log(evaluate("2^{11} - 1")); console.log(N("\\frac{1}{3}"));
FunctionPurpose
evaluate(expr | latex)Evaluate an expression or LaTeX input symbolically.
N(expr | latex)Numerically evaluate an expression or LaTeX input.
simplify(expr | latex)Simplify an expression or LaTeX input.
assign(id, value) / assign(record)Assign one symbol value or many at once.
expand(expr | latex)Expand distributively at the top level (Expression | null).
expandAll(expr | latex)Expand distributively recursively (Expression | null).
factor(expr | latex)Factor an expression.
solve(expr | latex, vars?)Solve equations/systems (returns solve result variants).
compile(expr | latex, options?)Compile to a target language with CompilationResult.
parse(latex)Parse a LaTeX string into an Expression.

You can use either regular LaTeX strings or a looser syntax similar to ASCIIMath or Typst:

console.log(N("(1+sqrt(5))/2"));
Note

These functions use a shared ComputeEngine instance created on first call. Use getDefaultEngine() to configure it.

Getting Started

To load the Compute Engine module from the jsdelivr CDN, use a <script> tag with the type="module" attribute and an import statement.

<script type="module">
import { evaluate } from "https://esm.run/@cortex-js/compute-engine";

console.log(evaluate("e^{i\\pi}"));
// ➔ "-1"
</script>

Alternatively, you can use the unpkg CDN:

import { ComputeEngine } from 
"https://unpkg.com/@cortex-js/compute-engine?module";

MathJSON Standard Library

The MathJSON Standard Library is a collection of functions and symbols that are available by default.

Topic
ArithmeticAdd Multiply Power Exp Log ExponentialE ImaginaryUnit...
CalculusD Derivative Integrate...
CollectionsList Reverse Filter...
ComplexReal Conjugate, ComplexRoots...
Control StructuresIf Block Loop ...
CoreDeclare Assign Error LatexString...
FunctionsFunction Apply Return ...
LogicAnd Or Not True False ...
SetsUnion Intersection EmptySet RealNumbers Integers ...
Special FunctionsGamma Factorial...
StatisticsStandardDeviation Mean Erf...
Strings and TextText Annotated...
TrigonometryPi Cos Sin Tan...

You can add your own definitions to the built-in definitions from the MathJSON Standard Library.

If you use a custom LaTeX syntax, such as macros, you can add your own definitions to the LaTeX dictionary, which defines how to parse and serialize LaTeX to MathJSON.

Note

In this guide, the ce. prefix in ce.box() or ce.parse() indicates that the function is a method of the ComputeEngine class.

Use getDefaultEngine() to access the shared ComputeEngine instance used by the free functions, or create your own instance with new ComputeEngine().

The expr. prefix in expr.evaluate() or expr.simplify() indicates that the function is a method of the Expression class.

To create a new expression use expr = ce.parse() or expr = ce.box()