lmnum guide

lmnum provides self-contained numerical computing for LMScript by linking Eigen and SUNDIALS into a single native host binary with deterministic random number generators and strict numerical validation.

Overview

Inside an LMScript _lmcode_ block declared with cli="lmnum", the global num object exposes 70 methods across five types: Num for constructors and scalar solvers, Matrix for dense linear algebra and tabular I/O, Func for compiled expression evaluation, Ode for initial-value differential equations, and System for nonlinear algebraic equations.

Matrices, deterministic randomness, and CSV format

All vectors and matrices in lmnum are dense double-precision Matrix objects with 0-based row and column indices. Column vectors have shape n x 1 (returned by linspace, eig_sym, svd_values, polyfit, minimize, and System.solve).

Dense linear algebra and factorizations (Eigen)

Matrix wraps Eigen decompositions with explicit validity checks so ill-posed problems never fail silently:

let a = num.parse("4 12 -16; 12 37 -43; -16 -43 98");
let l = a.cholesky();
let diff = a.sub(l.mul(l.transpose())).norm();
num.assert_close(diff, 0.0, 1e-12);

Expression strings and Func objects

Because LMScript _lmcode_ blocks compile against a static schema without closures, mathematical functions are passed as expression strings such as "sin(x)*exp(-k*t)". Expressions support arithmetic operators (+, -, *, /, ^), parentheses, constants (pi, e), and standard transcendental functions (sin, cos, tan, asin, acos, atan, atan2, sinh, cosh, tanh, exp, log, log10, sqrt, abs, floor, ceil, min, max).

Use num.eval(expr) for constant expressions, num.func(expr, "x") or num.func(expr, "x,y") for reusable 1D or 2D functions, and matrix.map(fn) to apply a 1D Func elementwise across a Matrix.

Differential equations and nonlinear systems (SUNDIALS)

num.ode(n) creates a builder for an n-dimensional system of first-order ODEs dy_i/dt = f_i(t, y0, ..., y{n-1}):

num.system(n) creates a builder for n coupled nonlinear algebraic equations F_i(x0, ..., x{n-1}) = 0. Define each residual with .eq(i, expr) and call .solve(guess, tol) to solve via SUNDIALS KINSOL with globalization line search and a dense finite-difference Jacobian.

Quadrature, roots, optimization, and fitting

For scalar and low-dimensional calculus tasks, lmnum provides direct methods on num: