MCPcopy Index your code
hub / github.com/cortex-js/compute-engine

github.com/cortex-js/compute-engine @main

Chat with this repo
repository ↗ · DeepWiki ↗ · + Follow
5,598 symbols 26,107 edges 649 files 1,428 documented · 26%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README
<img alt="math live" src="https://github.com/cortex-js/compute-engine/raw/main/assets/compute-engine.jpg?raw=true"/>

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 { 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, isNumber } from "@cortex-js/compute-engine";

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

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

Working with Symbols

import { parse, isSymbol, 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 (isSymbol(variable)) {
  console.log(variable.symbol);  // "y"
}

Working with Functions

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

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

// Access function structure safely
if (isFunction(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 from LaTeX or Expression
expand("(x + 1)^2").print();
// ➔ x^2 + 2x + 1

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

Solving Equations

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

// Solve from LaTeX
solve("x^2 - 5x + 6 = 0", "x");
// ➔ [2, 3]

// Solve a 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 (isNumber, isSymbol, isFunction) 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

Q How is the project structured?

See ARCHITECTURE.md for an overview of the codebase.

Related Projects

MathJSON
A lightweight mathematical notation interchange format
MathLive (on GitHub)
A Web Component for math input.

Support the Project

  • 🌟 Star the GitHub repo (it really helps)
  • 💬 Ask questions and give feedback on our Discussion Forum
  • 📨 Drop a line to arno@arno.org

License

This project is licensed under the MIT License.

Extension points exported contracts — how you extend this code

LanguageTarget (Interface)
(no doc) [10 implementers]
src/compute-engine/compilation/types.ts
Rules (Interface)
(no doc) [2 implementers]
src/point-free-parser/parsers.ts
ConfigurationChangeListener (Interface)
(no doc) [1 implementers]
src/common/configuration-change.ts
BenchResult (Interface)
(no doc)
test/big-decimal/comprehensive-benchmark.ts
BigDecimal (Interface)
(no doc)
src/big-decimal/transcendentals.ts
SourceEntry (Interface)
(no doc)
scripts/fungrim/compile-properties.ts
Case (Interface)
(no doc)
benchmarks/audit/solve.ts
Complex (Interface)
(no doc) [4 implementers]
src/compute-engine/numerics/complex-esm-augment.ts

Core symbols most depended-on inside this repo

expr
called by 3943
src/compute-engine/types-engine.ts
parse
called by 2943
src/point-free-parser/parsers.ts
evaluate
called by 2720
src/compute-engine/types-expression.ts
toString
called by 2043
src/compute-engine/types-expression.ts
isFunction
called by 1389
src/compute-engine/boxed-expression/type-guards.ts
mul
called by 1315
src/compute-engine/types-expression.ts
isSame
called by 1283
src/compute-engine/types-expression.ts
number
called by 1210
src/compute-engine/types-engine.ts

Shape

Function 3,789
Method 1,505
Class 181
Interface 123

Languages

TypeScript99%
Python1%

Modules by API surface

src/compute-engine/rubi/rubi-utils.ts160 symbols
src/compute-engine/numerics/special-functions.ts134 symbols
src/compute-engine/boxed-expression/abstract-boxed-expression.ts132 symbols
src/compute-engine/types-expression.ts130 symbols
src/compute-engine/boxed-expression/boxed-function.ts100 symbols
src/compute-engine/index.ts97 symbols
src/compute-engine/boxed-expression/boxed-symbol.ts77 symbols
src/compute-engine/latex-syntax/parse.ts76 symbols
src/compute-engine/types-engine.ts72 symbols
src/cortex/formatter.ts70 symbols
src/compute-engine/symbolic/fu-transforms.ts65 symbols
src/compute-engine/numeric-value/exact-numeric-value.ts65 symbols

For agents

$ claude mcp add compute-engine \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact