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 an 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 parse() and serialize() functions.

import { parse, serialize } from 'compute-engine';

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

console.log(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 'compute-engine';

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

console.log(evaluate(parse('\\frac{\\sqrt{5}}{3}'));
// ➔ 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.

Customization

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]);
Dictionary
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