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).
- num.parse("1 2; 3 4") parses inline text with rows separated by semicolons or newlines and entries separated by spaces or commas.
- num.random(rows, cols, seed) and num.normal(rows, cols, seed) use std::mt19937_64 with explicit 53-bit uniform scaling and Marsaglia polar normal transformations rather than C++ standard library distributions, producing identical matrices across Linux, macOS, and Windows.
- num.load_csv(path) accepts whitespace- or comma-separated numeric files and automatically skips one non-numeric header line.
- Matrix.save_csv(path) writes space-separated numbers using the shortest of %.15g to %.17g that round-trips back to the exact IEEE 754 double, with no header row.
Dense linear algebra and factorizations (Eigen)
Matrix wraps Eigen decompositions with explicit validity checks so ill-posed problems never fail silently:
- a.solve(b) uses Eigen FullPivLU and raises a run error (exit 3) if the matrix is numerically rank-deficient.
- a.lstsq(b) solves overdetermined or underdetermined linear least-squares problems.
- a.eig_sym() and a.eigvec_sym() verify that the receiver is symmetric before computing real eigenvalues and orthonormal eigenvectors.
- a.svd_values(), a.svd_u(), and a.svd_v() return singular values and thin U and V factors such that A = U * diag(s) * V^T.
- a.cholesky() returns the lower triangular factor L of a symmetric positive-definite matrix such that A = L * L^T.
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}):
- Chain .param(name, val), .rhs(i, expr), and .init(i, y0_val) to define the system.
- By default, Ode.solve(t0, t1, points) integrates with SUNDIALS CVODE Adams-Moulton (nonstiff) using relative tolerance 1e-8 and absolute tolerance 1e-10.
- Call .stiff() to select CVODE's Backward Differentiation Formulas (BDF) for stiff chemical kinetics, thermal diffusion, or method-of-lines PDEs.
- Call .tol(rtol, atol) to customize integration tolerances.
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:
- num.integrate(expr, var, a, b) and num.integrate_tol(expr, var, a, b, tol) evaluate definite integrals using adaptive Gauss-Kronrod quadrature.
- num.root(expr, var, a, b) finds a root inside a sign-changing bracket [a, b] via Brent's method.
- num.minimize(expr, vars, guess) and num.minimize_path(expr, vars, guess) minimize a multivariable expression using the Nelder-Mead simplex algorithm; minimize_path returns every simplex iteration as rows x0, ..., x{n-1}, f.
- num.polyfit(x, y, deg), num.polyval(coeffs, xq), and num.interp(x, y, xq) perform least-squares polynomial fitting and piecewise linear interpolation.