Use lmpy from LMScript

lmpy is a native numerical host for LMScript programs. This skill explains how to declare the released host in an _lmcode_ block, run it with run_lmcode, record checked results, and verify them. It describes lmpy 0.1.46.

Purpose

Use lmpy to compute numerical answers from LMScript in one native process: vectors, matrices and tables, linear algebra, sparse systems, probability distributions and tests, optimisation, roots, quadrature, linear programming, differential equations, interpolation, signal processing, spatial queries, graphs, regression and time-series models, and a scikit-learn style machine-learning subset. A block chains many operations in memory and returns one JSON result with the values and checks it recorded.

When to use it

Do not use it for plotting, for arbitrary Python code, or for the unsupported items listed in the guide: random forests, gradient boosting, the trust-constr method, analog filter design and convex hulls above three dimensions.

Install

Install the LMScript compiler (lmscript 0.2.460 or newer). Do not install lmpy by hand: declare it in the block and lmscript downloads the release asset for the platform into its tool cache, verified against the SHA-256 digest of the release asset. Assets exist for Linux x64 and arm64, macOS x64 and arm64, and Windows x64.

npm i -g @lmctl-ai/lmctl
lmscript --version
p = _lmcode_ cli="lmpy" install="github:lmctl-ai/lmpy@latest";
# statements
end _lmcode_

Pin a release by replacing latest with a tag such as v0.1.46. A local build is selected in the repository examples by passing an explicit command to run_lmcode.

Commands

CommandUse
lmscript --check program.lmsCompile and type-check every _lmcode_ block against the lmpy schema without running it
lmscript program.lmsCompile, then run_lmcode runs the block on lmpy and returns the decoded result
lmpy --versionPrint the release identity
lmpy --lmcode-schemaPrint the plain-text API summary; add --json for the exact schema bytes
lmpy --lmcode-signaturesPrint canonical signatures, one per line
lmpy --api-mdPrint the full API reference as Markdown
lmpy --lmcode FILE [FILE ...]Host protocol used by run_lmcode; add --dry-run before --lmcode to load without running

Inside the block, write statements with let, if, for over range, and # comment lines. The host is reached through free functions such as vec, mat, read_csv, dist and minimize, through methods on the returned objects, and through the context object out.

Inputs and outputs

Inputs are literals in the block, CSV files read with read_csv, text read with read_text, and arrays read with read_npy or read_safetensors. Relative paths resolve against the working directory of the run. Option values go in an Opts built with opts().set(key, value); an unknown key is an error.

Outputs are recorded on out. out.put(name, value) accepts float, int, string, bool, Vec, StrVec, Mat, Res and Frame. out.check(name, condition), out.close(name, got, want, tol) and out.close_rel(name, got, want, rtol) record named checks. run_lmcode returns a map: on success ok is true and result holds values and checks; on failure ok is false with phase, code and message.

r = run_lmcode(p);
require(r.ok, json_stringify(r));
values = r.result.values;
passed = r.result.checks.passed;

Operations return a Res. Read its fields with the typed getters num, int, vec, mat, res, strs, text and flag; ok() is true when the status field is OK. Files are written only by explicit calls such as write_csv on a Frame, write_text, write_npy, write_safetensors and save on an Estimator.

Safety rules

Worked examples

Least-squares line

p = _lmcode_ cli="lmpy" install="github:lmctl-ai/lmpy@latest";
let x = arange(5);
let fit = cols(ones(5), x).lstsq(x.mul(2.0).add(3.0));
let coefficients = fit.vec("x");
out.check("fit succeeded", fit.ok());
out.close("intercept and slope", coefficients, vec(3.0, 2.0), 0.000000000001);
out.put("coefficients", coefficients);
end _lmcode_

r = run_lmcode(p);
require(r.ok, json_stringify(r));
println("Fitted coefficients [intercept, slope]: " + str(r.result.values.coefficients));
println("Checks passed: " + str(r.result.checks.passed));
lmscript line.lms
Fitted coefficients [intercept, slope]: [2.999999999999999,2]
Checks passed: 2

Minimise an expression with options

The objective is an expression string in the host grammar, the unknowns are named by a StrVec, and the result is a nested Res.

p = _lmcode_ cli="lmpy" install="github:lmctl-ai/lmpy@latest";
let fit = minimize("100*(y-x^2)^2 + (1-x)^2", strs("x, y"), vec(-1.2, 1.0), opts().set("method", "BFGS").set("tol", 0.000001));
out.check("status OK", fit.ok());
out.close("x", fit.res("par").num("x"), 1.0, 0.001);
out.close("y", fit.res("par").num("y"), 1.0, 0.001);
out.put("status", fit.status());
out.put("x", fmt(fit.res("par").num("x"), 4));
out.put("y", fmt(fit.res("par").num("y"), 4));
end _lmcode_

r = run_lmcode(p);
require(r.ok, json_stringify(r));
v = r.result.values;
println(f"status={v.status} x={v.x} y={v.y}");
lmscript rosen.lms
status=OK x=1.0000 y=1.0000

A failed check

p = _lmcode_ cli="lmpy" install="github:lmctl-ai/lmpy@latest";
out.put("pi", pi());
out.close("pi is three", pi(), 3.0, 0.01);
end _lmcode_

r = run_lmcode(p);
println(json_stringify(r));
lmscript fail.lms
lmpy: {"values": {"pi": 3.141592653589793}, "checks": {"passed": 0, "failed": 1, "failures": [{"name": "pi is three", "detail": "got 3.141592653589793, want 3 +- 0.01"}]}}
{"ok":false,"phase":"app","code":"APP_FAIL","message":"1 check(s) failed: pi is three (got 3.141592653589793, want 3 +- 0.01)"}

The first line is the host's standard error, which keeps the values recorded before the failure; the second is the decoded result printed by the program.

Errors and exit codes

WhereSignalFix
lmscript compileExit 2 with a located message, for example: type Dist has no method 'quantile' followed by the list of methodsUse a name and argument types from the reference or lmpy --lmcode-schema
lmpy usageExit 2, frame with phase cli and code CLI_USAGEPass --lmcode FILE, or one of the documented query flags
lmpy loadExit 3, phase load, for example code LOAD_BAD_BYTECODERecompile with the same lmpy binary that runs the block; let run_lmcode manage the bytecode
lmpy runExit 3, phase run, code RUN_HOST, message such as dsl line 2:25: Mat.solve: the matrix is singular, with line and columnCorrect the input named by the message
lmpy applicationExit 4, phase app, code APP_FAIL; the message names every failed checkCorrect the inputs or the expected value and tolerance

Those exit codes belong to the lmpy process. The LMScript program sees them as the map returned by run_lmcode; when require(r.ok, ...) fails, lmscript itself exits 1.

How to verify

  1. Run lmscript --check on the program: exit 0 means every call matches the schema of the installed lmpy.
  2. Run the program and require r.ok; then read r.result.checks: failed must be 0 and passed must equal the number of checks you wrote.
  3. Give every important value a check: out.close against a known answer, a recomputation by a second method, or a conservation law.
  4. Confirm the host: lmpy --version prints the release, and lmpy --lmcode-signatures prints 757 lines for 0.1.46.
  5. For files a block writes, read them back in the same block or check that they exist from LMScript.

See the reference for every signature and the example book for release-installed scripts.