Skip to content

An engine for symbolic manipulation and numeric evaluation of math formulas expressed with MathJSON

License

Notifications You must be signed in to change notification settings

cortex-js/compute-engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1,598 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
math live

Cortex Compute Engine

Symbolic manipulation and numeric evaluation of MathJSON expressions

MathJSON is a lightweight mathematical notation interchange format based on JSON.

The Cortex Compute Engine can parse LaTeX to MathJSON, serialize MathJSON to LaTeX or MathASCII, format, simplify and evaluate MathJSON expressions.

Reference documentation and guides at cortexjs.io/compute-engine.

Installation

$ npm install --save @cortex-js/compute-engine

Quick Start

Basic Parsing and Evaluation

No setup required:

import { parse, simplify, evaluate, N, assign } from "@cortex-js/compute-engine";

simplify("x + x + 1").print();
// ➔ 2x + 1

evaluate("2^{11} - 1").print();
// ➔ 2047

N("\\sqrt{2}").print();
// ➔ 1.414213562...

assign("x", 3);
evaluate("x + 2").print();
// ➔ 5

These functions use a shared ComputeEngine instance created on first use. Use getDefaultEngine() to configure it, or create your own instance for isolated configurations.

Working with Numbers (Type-Safe)

Use type guards to safely access specialized properties:

import { evaluate, isBoxedNumber } from "@cortex-js/compute-engine";

const expr = evaluate("\\frac{5}{2}");

if (isBoxedNumber(expr)) {
  console.log(expr.numericValue);  // 2.5 (type-safe access)
  console.log(expr.isInteger);     // false
}

Working with Symbols

import { parse, isBoxedSymbol, sym } from "@cortex-js/compute-engine";

const expr = parse("x + 1");

// Check if expression is a specific symbol
if (sym(expr) === "x") {
  console.log("This is the variable x");
}

// Or use full type guard for more access
const variable = parse("y");
if (isBoxedSymbol(variable)) {
  console.log(variable.symbol);  // "y"
}

Working with Functions

import { parse, isBoxedFunction } from "@cortex-js/compute-engine";

const expr = parse("2x + 3y");

// Access function structure safely
if (isBoxedFunction(expr)) {
  console.log(expr.operator);    // "Add"
  console.log(expr.ops.length);  // 2

  // Iterate over operands
  for (const op of expr.ops) {
    console.log(op.toString());
  }
}

Simplification and Manipulation

import { parse, simplify, expand } from "@cortex-js/compute-engine";


// Simplify expressions
simplify("x + x").print();
// ➔ 2x

// Expand expressions (free function)
const expr = parse("(x + 1)^2");
expand(expr).print();
// ➔ x^2 + 2x + 1

// Substitute values
const expr2 = parse("x^2 + 2x + 1");
expr2.subs({ x: 3 }).evaluate().print();
// ➔ 16

Solving Equations

// Solve linear system
const system = parse("\\begin{cases}x+y=5\\\\x-y=1\\end{cases}");
const solution = system.solve(["x", "y"]);

console.log(solution.x.json);  // 3
console.log(solution.y.json);  // 2

💡 Best Practices:

  • Always use type guards (isBoxedNumber, isBoxedSymbol, isBoxedFunction) before accessing specialized properties
  • Use the sym() helper for quick symbol name checks

📚 Learn More: Full documentation and guides

FAQ

Q How do I build the project?

Build instructions

Related Projects

MathJSON
A lightweight mathematical notation interchange format
MathLive (on GitHub)
A Web Component for math input.
Cortex (on GitHub)
A programming language for scientific computing

Support the Project

License

This project is licensed under the MIT License.

About

An engine for symbolic manipulation and numeric evaluation of math formulas expressed with MathJSON

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Contributors 16