@cortex-js/compute-engine

中文文档
0.186.0发布于 2026/11/11MITTypeScriptES Module近 30 天下载 133.7万

Symbolic computing and numeric evaluations for JavaScript and Node.js

原始使用文档
文档目录14 节
  1. Compute Engine
  2. Installation
  3. Quick Start
  4. Basic Parsing and Evaluation
  5. Working with Numbers (Type-Safe)
  6. Working with Symbols
  7. Working with Functions
  8. Simplification and Manipulation
  9. Solving Equations
  10. Epsil Language (Experimental)
  11. FAQ
  12. Related Projects
  13. Support the Project
  14. License
math live

Compute Engine

Symbolic manipulation and numeric evaluation of MathJSON expressions

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

The 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

Epsil Language (Experimental)

Epsil is a text-syntax programming language for scientific computing whose intermediate representation is MathJSON, evaluated by the Compute Engine. It ships as a separate, experimental entry point (syntax and semantics may change between releases):

import { ComputeEngine, executeEpsil } from "@cortex-js/compute-engine/epsil";

const ce = new ComputeEngine();
const { value } = executeEpsil(ce, `
  let x = 1/2
  if (x < 1) { x + 1 } else { 0 }
`);
// value.toString() ➔ "3/2"

See the Epsil language documentation.

Epsil CLI and REPL

The package also installs an epsil command:

# Start an interactive session
npx epsil

# Evaluate source text or a file
npx epsil -e 'Simplify(2 + 2x)'
npx epsil program.epsil

# Epsil programs can also be piped over stdin
printf '1/2 + 1' | npx epsil

The interactive session preserves declarations between inputs and supports persistent history, multiline input, .load, .clear, .ast, and .time. Run epsil --help for output formats and execution-limit options.

MCP Server for AI Assistants

The epsil command includes a Model Context Protocol server, so an AI assistant can evaluate Epsil programs — exact arithmetic and symbolic computation as a tool call. For example, with Claude Code:

claude mcp add epsil -- npx -y @cortex-js/compute-engine mcp

The server exposes evaluate, check, doc, parse, and serialize tools, and serves machine-verified cards so the assistant can learn Epsil, and the Compute Engine JavaScript API, on its own. It also has a native Streamable HTTP transport for ChatGPT and other URL-based clients:

npx -y @cortex-js/compute-engine mcp --transport streamable-http

See the MCP server guide for ChatGPT setup and HTTPS deployment options.

💡 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.

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

Support the Project

License

This project is licensed under the MIT License.

版本与发布记录

@cortex-js/compute-engine 共发布 281 个版本,首次发布于 2021/06/22;过去 12 个月发布了 230 个版本。

版本发布日期距今
0.186.0latest2026/11/11今天
0.185.12026/11/101 天前
0.185.02026/11/101 天前
0.184.02026/11/082 天前
0.183.02026/11/083 天前
0.182.02026/11/074 天前

关于 @cortex-js/compute-engine 的常见问题

@cortex-js/compute-engine 是什么?

Symbolic computing and numeric evaluations for JavaScript and Node.js

如何安装 @cortex-js/compute-engine?

在项目目录运行 npm install @cortex-js/compute-engine,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 epsil,可以用 npx epsil 直接运行。

@cortex-js/compute-engine 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cortex-js/compute-engine 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@cortex-js/compute-engine 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=26.13.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@cortex-js/compute-engine 可以商用吗?

@cortex-js/compute-engine 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cortex-js/compute-engine 的下载量有多少?

根据 npm 官方下载统计,@cortex-js/compute-engine 在 2026/10/11 – 2026/11/09 这 30 天内被下载 1,336,680 次,日均约 44,556 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cortex-js/compute-engine 还在维护吗?

最新版本 0.186.0 发布于 2026/11/11(今天);至今共发布 281 个版本,其中过去 12 个月发布了 230 个;首次发布于 2021/06/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。