lmnum authoring skill

lmnum is a native numerical computing host for LMScript that embeds Eigen linear algebra, SUNDIALS CVODE and KINSOL solvers, Gauss-Kronrod quadrature, Brent root finding, Nelder-Mead optimization, and least-squares fitting in one executable.

Purpose

Use lmnum to solve dense linear algebra problems, integrate nonstiff and stiff ordinary differential equation systems, solve nonlinear equation systems, evaluate definite integrals, find roots and local minima, and fit polynomials or linear regressions from LMScript.

When to use it

Use lmnum when a script needs deterministic, dependency-free numerical computing with strict rank, convergence, and finiteness checks and no Python or external BLAS runtime. Pair lmnum with lmplot or lmdraw in multi-host LMScript programs to visualize computed trajectories, phase portraits, and spectral decompositions.

Install

Install Node.js 24.15 or newer and the lmctl CLI globally. Declare cli="lmnum" and install="github:lmctl-ai/lmnum@latest" (or @v0.1.4) in an LMScript _lmcode_ block so LMScript downloads and verifies the platform release binary automatically. Linux release binaries require glibc 2.35 or newer (Ubuntu 22.04+).

npm install -g @lmctl-ai/lmctl
lmnum --version
lmnum --lmcode-schema --json
lmnum --lmcode-signatures

Commands

Execute an LMScript numerical program locally or from a public URL, or invoke the lmnum binary directly for bytecode execution, dry-run validation, schema inspection, and license notices:

lmscript analysis.lms
lmctl https://raw.githubusercontent.com/lmctl-ai/lmnum/main/examples/linear_algebra.lms
lmnum --lmcode program.dslb
lmnum --dry-run --lmcode program.dslb
lmnum --lmcode-schema --json
lmnum --lmcode-signatures
lmnum --notices

Inputs and outputs

All numerical operations start from the global num context (type Num). Matrices are dense double-precision arrays with 0-based indexing created via num.matrix, num.identity, num.parse, num.linspace, num.random, num.normal, or num.load_csv. Differential equations, nonlinear systems, integrands, and objective functions are passed as mathematical expression strings over named variables and parameters.

Matrix.save_csv(path) writes space-separated rows using exact round-trip floating-point formatting without a header line. Ode.solve(t0, t1, points) returns a Matrix with points rows and columns t, y0, ..., y{n-1}. Diagnostics printed via num.print go to stderr, while the host writes its single JSON execution result envelope to stdout.

Safety rules

lmnum treats numerical pathologies as hard runtime errors rather than returning silent NaNs: numerically singular linear systems, non-symmetric matrices passed to eig_sym or eigvec_sym, non-increasing interpolation nodes, unbracketed roots, non-convergent KINSOL or Nelder-Mead runs, and non-finite expression evaluations all fail immediately with exit code 3.

Host objects live in a process arena capped at 4,000,000 objects per kind and a 4096 MiB total memory budget (configurable via LMNUM_MAX_MEMORY_MB). In tight loops, mutate existing matrices in place with Matrix.set instead of allocating millions of large temporary matrices. For stiff differential equations such as chemical kinetics or method-of-lines PDEs, call .stiff() on the Ode builder to switch CVODE from Adams-Moulton to BDF.

Worked examples

Integrate the Lotka-Volterra predator-prey ODE system with SUNDIALS CVODE and export the trajectory to CSV:

p = _lmcode_ cli="lmnum" install="github:lmctl-ai/lmnum@latest";
let sys = num.ode(2);
sys.param("a", 1.1).param("b", 0.4).param("c", 0.4).param("d", 0.1);
sys.rhs(0, "a*y0 - b*y0*y1").rhs(1, "d*y0*y1 - c*y1");
sys.init(0, 10.0).init(1, 10.0);
let sol = sys.solve(0.0, 60.0, 601);
sol.save_csv("lv.csv");
num.assert_close(sol.rows() * 1.0, 601.0, 1e-9);
end _lmcode_
r = run_lmcode(p);
require(r.ok, "Lotka-Volterra integration failed");

Solve a linear system, compute symmetric eigenvalues, integrate a function with Gauss-Kronrod quadrature, and find a root with Brent's method:

p = _lmcode_ cli="lmnum" install="github:lmctl-ai/lmnum@latest";
let a = num.parse("4 1; 1 3");
let b = num.parse("1; 2");
let x = a.solve(b);
num.assert_close(x.get(0, 0), 0.09090909090909091, 1e-12);
num.assert_close(x.get(1, 0), 0.6363636363636364, 1e-12);

let area = num.integrate("sin(x)", "x", 0.0, num.eval("pi"));
num.assert_close(area, 2.0, 1e-10);

let r0 = num.root("cos(x) - x", "x", 0.0, 1.0);
num.assert_close(r0, 0.7390851332151607, 1e-10);
end _lmcode_
r = run_lmcode(p);
require(r.ok, "calculus and linear algebra check failed");

Errors and exit codes

lmnum follows the SDK invocation contract: exit code 0 indicates success, code 2 indicates CLI usage or metadata flag errors, code 3 indicates bytecode load or runtime numerical errors (including failed num.assert_close checks, singular matrices, solver non-convergence, or exceeding LMNUM_MAX_MEMORY_MB), and code 4 indicates application or output failures.

When CVODE exceeds 500,000 internal steps between output points, the error message advises enabling .stiff() on the Ode object or relaxing tolerances with .tol(rtol, atol).

How to verify

Check the binary version and schema signatures, then run the self-contained linear algebra and ODE examples:

lmnum --version
lmnum --lmcode-schema --json > /tmp/lmnum.schema.json
lmnum --lmcode-signatures > /tmp/lmnum.sigs.txt
lmctl https://raw.githubusercontent.com/lmctl-ai/lmnum/main/examples/linear_algebra.lms
lmctl https://raw.githubusercontent.com/lmctl-ai/lmnum/main/examples/ode_stiff.lms

In a repository checkout, run ./test.sh to execute the full known-answer suite, host flag checks, and invocation protocol tests.