Skip to main content

Compute Engine

The Compute Engine is a JavaScript/TypeScript library for symbolic computing and numeric evaluation of mathematical expressions.

Note

To use the Compute Engine, you must write JavaScript or TypeScript code. This guide assumes familiarity with one of these programming 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 make technical computing apps running in the browser or in server-side JavaScript environments such as Node.

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

For example, the expression \(x^2 + 2x + 1\) is represented as:

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

The Compute Engine can:

Note

In this guide, functions such as ce.box() and ce.parse() require a ComputeEngine instance which is denoted by the ce. prefix.

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

Functions that apply to a boxed expression, such as expr.simplify() are denoted with the expr. prefix.

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

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

JavaScript modules are the modern way to load JavaScript code. You can load the Compute Engine module from a CDN using an import statement.

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

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

The ESM (module) version is also available in the npm package in dist/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. You can load the UMD version by using a <script> tag.

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

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

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

Other Versions

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

MathJSON Standard Library

The identifiers 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...

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.