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";

To find a Compute Engine that the page has already loaded, without importing it yourself, read the global slot Symbol.for("io.cortexjs.compute-engine"). This is for a component, such as a web component in its own bundle, that must work with a copy of the Compute Engine loaded by the page. When the main entry point (@cortex-js/compute-engine) loads, it stores an object in this slot:

const global = globalThis[Symbol.for("io.cortexjs.compute-engine")];
if (global?.ComputeEngine) {
const ce = new global.ComputeEngine();
console.log(global.version, ce.parse("1+1").evaluate().toString());
}

The object holds ComputeEngine (the class), version, LatexSyntax, LATEX_DICTIONARY, compile, the type guards (isExpression, isNumber, isSymbol, isFunction, isString, isCharacter, isTensor, isDictionary, isObject, isCollection, isIndexedCollection), and the helpers numericValue and sym. Its TypeScript type is ComputeEngineGlobal. The type guards test the structure of an expression, so they work on an expression made by another copy of the bundle.

The contract is additive: a later version can add a member, but a member keeps its meaning and is not removed. Test each member before you use it, since an older version can lack a newer one. When several copies load, the last one to load owns the slot. The main entry point and the Epsil entry point (/epsil) store it; /core and the other entry points do not.

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.expr() 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.expr()