Skip to content

Latest commit

 

History

History
180 lines (125 loc) · 7.19 KB

File metadata and controls

180 lines (125 loc) · 7.19 KB

Introduction - Compute Engine

# Compute Engine A JavaScript library for symbolic computing and numeric evaluation of mathematical expressions.
console.log(evaluate("e^{i\\pi}"));
console.log(expand("(a+b)^2"));
console.log(simplify(
  "(sin(alpha)**2 + cos(alpha)**2) * (x**2 + 2*x + 1) / (x + 1)"
));

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:

Free Functions

For common operations, use the free functions — no setup required:

console.log(simplify("x+x+1"));
console.log(evaluate("2^{11} - 1"));
console.log(N("\\frac{1}{3}"));
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:

console.log(N("(1+sqrt(5))/2"));

:::info[Note]

These functions use a shared ComputeEngine instance created on first call. Use getDefaultEngine() to configure it. :::

Try the **interactive demo** now

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...
Read more about the **MathJSON Standard Library**

You can add your own definitions to the built-in definitions from the MathJSON Standard Library.

Read more about **Extending 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.

Read more about **Parsing and Serializing LaTeX**

:::info[Note] In this guide, the ce. prefix in ce.expr() 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.expr()

:::