Skip to main content

Compute Engine

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

Note

To use Compute Engine, you must write JavaScript or TypeScript code. This guide assumes you’re familiar with these languages.

console.log("exp(i*pi) =", ce.parse("e^{i\\pi}").evaluate());
const expr = ce.parse("(a+b)^2"); ce.box(["Expand", expr]).evaluate().print();
const lhs = ce.parse("1 + x(1 + 2x) + 2x"); const rhs = ce.parse("2x^2 + 3x + 1"); console.log(lhs, lhs.isEqual(rhs) ? "=" : "≠", rhs);

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:

Getting Started

The easiest way to get started is to load the Compute Engine JavaScript module from a CDN, then create a ComputeEngine instance.

Using JavaScript Modules

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 { ComputeEngine } from "https://esm.run/@cortex-js/compute-engine";

const ce = new ComputeEngine();
ce.parse("e^{i\\pi}").evaluate().print();
// ➔ "-1"
</script>

Alternatively, you can use the unpkg CDN to load the module:

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

The ESM (module) version is also available in the npm package as /compute-engine.min.esm.js

Using Vintage JavaScript

If you are using a vintage environment, or if your toolchain does not support modern JavaScript features, use the UMD version.

For example, WebPack 4 does not support the optional chaining operator, using the UMD version will make use of polyfills as necessary.

To load the UMD version, use a <script> tag with the src attribute.

<script 
src="https://cdn.jsdelivr.net/npm/@cortex-js/compute-engine/compute-engine.min.js">
</script>
<script>
window.onload = function() {
const ce = new ComputeEngine.ComputeEngine();
console.log(ce.parse("e^{i\\pi}").evaluate());
// ➔ "-1"
}
</script>

Alternatively, use the unpkg CDN to load the library:

<script src="//unpkg.com/@cortex-js/compute-engine"></script>

The UMD version is also available in the npm package in /compute-engine.min.js

Other Versions

A non-minified module which may be useful for debugging is available in the npm package as /compute-engine.esm.js.

MathJSON Standard Library

The operators in a MathJSON expression are defined in libraries. The MathJSON Standard Library is a collection of functions and symbols that are available by default to a ComputeEngine instance.

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...
StylingDelimiter Style...
TrigonometryPi Cos Sin Tan...
Note

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

To create a new ComputeEngine instance use ce = new ComputeEngine()

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

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

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.