lmpy

lmpy logo

lmpy is a native numerical host for LMScript: linear algebra, statistics, tables, optimisation, differential equations, signal processing, graphs, statistical models and machine learning in one executable, with no Python, R or Docker at run time. A program is an LMScript _lmcode_ block that lmscript type-checks against the lmpy schema and run_lmcode executes, returning checked JSON values.

Install and run

Install the LMScript compiler once; the block declaration downloads the lmpy release binary for your platform (Linux x64 and arm64, macOS x64 and arm64, Windows x64) into the lmscript tool cache and verifies its SHA-256 digest.

npm i -g @lmctl-ai/lmctl
lmscript --check program.lms
lmscript program.lms

The repository examples need lmscript 0.2.460 or newer. This page describes lmpy 0.1.46, the release that install="github:lmctl-ai/lmpy@latest" resolved to when it was written.

Minimal program

p = _lmcode_ cli="lmpy" install="github:lmctl-ai/lmpy@latest";
let q = dist("norm").ppf(0.975);
out.close("normal 97.5% quantile", q, 1.959963984540054, 0.00000000000001);
let x = mat("1 2 3; 0 1 4; 5 6 0").solve(vec(14.0, 14.0, 17.0));
out.close("known linear-system solution", x, vec(1.0, 2.0, 3.0), 0.000000000001);
out.put("quantile", q);
out.put("solution", x);
end _lmcode_

r = run_lmcode(p);
require(r.ok, json_stringify(r));
println(json_stringify(r.result));
lmscript quick.lms
{"values":{"quantile":1.959963984540054,"solution":[1,2,3]},"checks":{"passed":2,"failed":0,"failures":[]}}

Host protocol and flags

Command or formWhat it does
_lmcode_ cli="lmpy" install="github:lmctl-ai/lmpy@latest"; ... end _lmcode_Declares the host; lmscript reads the binary's embedded schema and compiles the block against it
_lmcode_ schema="lmpy.dsl.json"; ... end _lmcode_Compiles against a schema file instead of asking a binary
run_lmcode(p)Runs the block on lmpy and returns the decoded frame: ok and result, or ok, phase, code and message
lmpy --lmcode FILE [FILE ...]Host side of run_lmcode: runs compiled bytecode and prints one LMCODE_RETURN_BEGIN / JSON / LMCODE_RETURN_END frame
lmpy --dry-run --lmcode FILE [FILE ...]Loads and validates without running; results are null
lmpy --lmcode-schemaPlain-text API summary
lmpy --lmcode-schema --jsonExact schema bytes the bytecode binds to
lmpy --lmcode-signaturesCanonical signatures, one per line (757 in 0.1.46)
lmpy --api-mdGenerated API reference as Markdown
lmpy --versionRelease identity, lmpy X.Y.Z
Exit codeFrame phaseMeaning
0noneSuccess
2cliUsage error; lmscript compile errors also exit 2
3load, runBytecode load error or run-time error
4appout.fail or a failed check

Signature counts

SurfaceCount in lmpy 0.1.46
Host types15: Vec, StrVec, Mat, Opts, Res, Out, Frame, Sparse, Dist, Rng, OdeSol, Interp, Filter, Graph, Estimator
Host signatures551 lines: the out context, 211 function signatures and 339 methods
Standard catalog shippedlmcode_std_v3 with 206 signatures (str_, math_, file_, collections, Json, encoding, template_render, lmcode_return_)
Current lmcode_std_v3 in lmcode-cpp226 signatures; the 20 added REST and CLI signatures are not in lmpy 0.1.46 and arrive when lmpy syncs its runtime and releases

Main objects and functions

NameKindCreate or callUse
outcontext object (Out)out.put(name, value), out.check(name, bool), out.close(name, got, want, tol), out.close_rel, out.log, out.failRecord returned values and named checks; a failed check makes the run fail with exit 4
Vectypevec(1.0, 2.0), vec("1 2.5 -3"), arange(5), linspace(0.0, 1.0, 5), zeros(n), ones(n), full(n, x)1-D float64 array; elementwise +, -, * and methods such as add, mul, sum, mean, std, sort, slice, apply
StrVectypestrs("x1, x2"), split(text, sep)Names of columns, unknowns and labels
Mattypemat("1 2; 3 4"), mat(rows, cols), cols(v1, v2), eye(n), diag(v)Dense matrix; * is the matrix product; solve, lstsq, inv, det, decompositions and matrix functions
Optstypeopts().set("method", "BFGS").set("tol", 0.000001)Named options for an operation; unknown keys are errors; keys starting with $ bind names in expressions
Restypereturned by operationsTyped result fields: ok(), status(), num, int, vec, mat, res, strs, text, flag, keys, json
Frametyperead_csv(path), csv(text), frame()Immutable column table: filter, mutate, select, sort, groupby, merge, rolling, resample
Sparsetypesparse(rows, cols), sparse(dense), speye(n), tridiag(n, lower, diagonal, upper)Sparse matrix with direct and iterative solvers and eigsh
Dist, Rngtypesdist("norm"), dist("gamma", opts().set("a", 3.0)), rng(seed)31 distribution families with pdf, cdf, ppf, rvs; numpy-compatible seeded random numbers
minimize, least_squares, root_scalar, root, quad, linprogfunctionsminimize(expr, strs("x, y"), x0, opts)Optimisation, roots, quadrature and LP/MILP on expression strings
ode, OdeSolfunction, typeode(rhs, states, y0, t0, t1, opts)Initial value problems with events
interp, Interpfunction, typeinterp(kind, x, y)Linear, cubic, pchip, akima, smoothing and rbf interpolants
iir, Filter, welch, find_peaks, fftfunctions, typesee referenceFilter design and filtering, spectra, peaks
graph, Graphfunction, typegraph(directed)Shortest paths and flows on named nodes
ols, glm, arima, acffunctionsols(frame, "y", strs("x1, x2"))Regression and time-series models returning Res
ml_fit, Estimatorfunction, typeml_fit(kind, x, y)Linear, logistic, SVC, naive Bayes, LDA, kNN, k-means, PCA, scaler

Detail pages