Compute Engine
A JavaScript library for symbolic computing and numeric evaluation of mathematical expressions.
To use Compute Engine, you must write JavaScript or TypeScript code. This guide assumes you’re familiar with these languages.
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:
- parse and serialize expressions from and to LaTeX.
- simplify symbolic expressions
- evaluate expression symbolically or numerically
- solve equations, calculate derivatives and integrals, and perform other calculus operations
- compile expressions to JavaScript
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 | |
---|---|
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 ... |
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.