Compute Engine
The Compute Engine is a JavaScript/TypeScript library for symbolic computing and numeric evaluation of mathematical expressions.
To use the Compute Engine, you must write JavaScript or TypeScript code. This guide assumes familiarity with one of these programming languages.
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:
- parse and serialize expressions from and to LaTeX
- simplify expressions
- evaluate expression symbolically
- evaluate expressions numerically
- compile expressions to JavaScript functions
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 | |
---|---|
Arithmetic | Add Multiply Power Exp Log ExponentialE ImaginaryUnit ... |
Calculus | D Derivative Integrate ... |
Collections | List Reverse Filter ... |
Complex | Real Conjugate , ComplexRoots ... |
Control Structures | If Block Loop ... |
Core | Declare Assign Error LatexString ... |
Functions | Function Apply Return ... |
Logic | And Or Not True False ... |
Sets | Union Intersection EmptySet RealNumbers Integers ... |
Special Functions | Gamma Factorial ... |
Statistics | StandardDeviation Mean Erf ... |
Styling | Delimiter Style ... |
Trigonometry | Pi 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.