Compute Engine

The CortexJS Compute Engine is a JavaScript library for symbolic computing and numerical evaluation of mathematical expressions.

The Compute Engine is for anyone who wants to make technical computing apps in the browser or in server-side environments such as Node: educators, students, scientists and engineers.

The Compute Engine can:

Parse and Serialize LaTeX

Internally, the Compute Engine manipulates expressions represented with the MathJSON format. It’s a JSON representation of the Abstract Syntax Tree of the expression. It is easy to manipulate programatically and can be written by hand. However, you might prefer to use a more concise and familiar syntax, such as LaTeX. The Compute Engine includes utilities to convert to and from LaTeX strings.

To parse a LaTeX string and serialize to a LaTeX string, use the ce.parse() and ce.serialize() functions.

import { ComputeEngine } from '@cortex-js/compute-engine';

const ce = new ComputeEngine();

console.log(ce.parse('5x + 1'));
// ->  ["Add", ["Multiply", 5, "x"], 1]

console.log(ce.serialize(["Add", ["Power", "x", 3], 2]));
// ->  x^3 + 2

To input math using an interactive mathfield, use MathLive.

A MathLive mathfield works like a textarea in HTML, but for math. It provide its content as a LaTeX string or a MathJSON expression, ready to be used with the Compute Engine.

Symbolic Computing and Numerical Evaluation

To evaluate a symbolic expression, use the evaluate() function.

The result of evaluate() is an expression:

  • If the expression can be evaluated numerically, the result is a number
  • If it can’t be evaluated numerically, the result is a symbolic expression.
import { evaluate, parse, serialize } from '@cortex-js/compute-engine';

console.log(evaluate(["Add", 2, 3]);
// ➔ 5

// ➔ 0.7453559925

console.log(serialize(evaluate(parse('2x + 3x')));
// ➔ 5x

The Compute Engine supports arbitrary precision floating points and complex numbers.

The Compute Engine can also simplify, find patterns, substitute terms, apply rewrite rules, compare and format expressions.


The Compute Engine includes a robust library of mathematical functions.

To customize the dictionaries that define the math functions, create and configure a ComputeEngine instance.

The ComputeEngine instance also provides access to additional features such as defining assumptions about symbols: x is a positive Real number, n is an Integer.

const ce = new ComputeEngine(ComputeEngine.getDictionary('arithmetic'));
ce.evaluate(['Add', 5, 2]);
Arithmetic Add Multiply
Calculus Derive Integrate
Collections Sequence List Dictionary Set
Core Missing Nothing None All Identity InverseFunction LatexTokens
Logic And Or Not
Sets Union Intersection
Special Functions Erf Gamma Factorial
Trigonometry Cos Sin Tan