Skip to main content

Getting Started

Install Cortex and run your first symbolic program in five minutes.

Experimental

Cortex is experimental. Its syntax and behavior may change between releases.

Install

Cortex is included with the Compute Engine package:

npm install @cortex-js/compute-engine

The package installs a cortex command. During development, run the project-local command through npx.

Try the REPL

Start an interactive session:

npx cortex

Enter a declaration, then use it in another expression:

cortex> let x = 5
5
cortex> x^2
25

The REPL keeps declarations and assignments between inputs. Enter .help for the available commands and .exit when you are done.

Run a Source File

Save this program as squares.cx:

square(x) = x^2
Map(Range(1, 5), square)

Run it:

npx cortex squares.cx

The result is:

[1,4,9,16,25]

The conventional file extensions are .cx and .cortex.

Work Symbolically

Cortex uses the Compute Engine, so expressions remain exact and symbolic by default:

⌘/Ctrl + Enter

Use N() when you want a numeric approximation:

⌘/Ctrl + Enter

Embed Cortex in JavaScript

Import the experimental Cortex entry point, create a ComputeEngine, then execute source text:

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

const ce = new ComputeEngine();
const { value, diagnostics } = executeCortex(
ce,
"factorial(n) = if n <= 1 { 1 } else { n * factorial(n - 1) }\nfactorial(10)"
);

if (diagnostics.length > 0) console.error(diagnostics);
console.log(value.toString()); // 3628800

Calls made with the same ComputeEngine share its top-level declarations, which is useful for notebook cells and other stateful sessions. Create a fresh engine when you want an isolated program.

Where to Go Next