Compute Engine
A JavaScript library for symbolic computing and numeric evaluation of mathematical expressions.
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 expressions symbolically or numerically
- solve equations, calculate derivatives and integrals, and perform other calculus operations
- compile expressions to JavaScript
Free Functions
For common operations, use the free functions — no setup required:
| Function | Purpose |
|---|---|
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:
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";
MathJSON Standard Library
The MathJSON Standard Library is a collection of functions and symbols that are available by default.
| 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... |
| Strings and Text | Text Annotated... |
| 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.
In this guide, the ce. prefix in ce.box() 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.box()