
The CortexJS Compute Engine is a JavaScript/TypeScript library for symbolic computing and numeric evaluation of mathematical expressions.
const ce = new ComputeEngine();
console.log(ce.parse("e^{i\\pi}").N().latex);
// ➔ "-1"
const expr = ce.parse("(a+b)^2");
console.log(ce.box(["Expand", expr]).evaluate().latex);
// ➔ "a^2 + 2ab + b^2"
The Compute Engine is for anyone who wants to make technical computing apps in the browser or in server-side environments such as Node: educators, students, scientists and engineers.
The CortexJS Compute Engine manipulates math expressions represented with the MathJSON format.
The Compute Engine can:
- parse and serialize expressions from and to LaTeX
- simplify expressions
- evaluate symbolically expressions
- evaluate numerically expressions
Getting Started
The easiest way to get started it to load the Compute Engine JavaScript module from a CDN.
Using JavaScript Modules
<script type="module">
import { ComputeEngine } from
'https://unpkg.com/@cortex-js/compute-engine?module';
const ce = new ComputeEngine();
console.log(ce.parse("e^{i\\pi}").N().latex);
// ➔ "-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.
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}").N().latex);
// ➔ "-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
.
Standard Library
Expressions reference identifiers that are defined in libraries.
By default, a ComputeEngine
instance includes a robust set of
functions and symbols, the standard library, grouped in several categories.
Category | Identifiers |
---|---|
Arithmetic | Add Multiply Power Exp Log ExponentialE ImaginaryUnit … |
Calculus | Derive Integrate … |
Collections | Sequence List Dictionary Set … |
Control Structures | If Block Loop Sum … |
Core | InverseFunction LatexTokens … |
Domains | Anything Nothing Number Integer … |
Functions | Function Apply Return … |
Logic | And Or Not True False Maybe … |
Sets | Union Intersection EmptySet … |
Special Functions | Erf Gamma Factorial … |
Styling | Delimiter Style … |
Trigonometry | Pi Cos Sin Tan … |
You can define your own identifiers to complement or replace the standard library.
const ce = new ComputeEngine({
symbolTables: ComputeEngine.getSymbolTable('arithmetic')
});
console.log(ce.box(['Add', 5, 2]).evaluate().json);
Each entry in a symbol table defines the properties of that function or symbol, as well as how to put expressions using that function in canonical form and how to simplify and evaluate it.
You can also customize the LaTeX syntax, that is how to parse and serialize LaTeX to MathJSON.