Ship runnable public how-to pages, T_ascii/转置文本 one-step display, explicit auto-simplify, and mark formula-doc F1–F3 complete. Co-authored-by: Cursor <cursoragent@cursor.com>
6.0 KiB
title: ext/linalg/linalg description: Formula-first linear algebra (English). MatExpr simplify + display; ABI plugin. import plugin:lib/plugin.mq.md import sys:lib/sys.mq.md
ensure_plugin
Load the ABI v2 linalg plugin once.
p = > plugin.native_path name="linalg"
p
plugin.load path=
p
print text=ext/linalg: native linalg plugin not found (build marqdo_plugin_linalg or marqdo ext add linalg) sys.exit code=1
ping
ensure_plugin > linalg_ping
mul
+ `a`
+ `b`
Matrix product (lazy MatMul; shape-checked).
ensure_plugin > linalg_mul a=
ab=b
add
+ `a`
+ `b`
ensure_plugin > linalg_add a=
ab=b
sub
+ `a`
+ `b`
ensure_plugin > linalg_sub a=
ab=b
transpose
+ `expr`
ensure_plugin > linalg_transpose expr=
expr
inv
+ `expr`
Symbolic inverse (lazy).
ensure_plugin > linalg_inv expr=
expr
simplify
+ `expr`
Apply MatExpr rewrite rules (R1–R5).
ensure_plugin > linalg_simplify expr=
expr
ascii
+ `expr`
+ `raw`=False
Display ASCII (default simplify first). Pass raw=True to keep the written tree.
ensure_plugin > linalg_ascii expr=
exprraw=raw
latex
+ `expr`
+ `raw`=False
ensure_plugin > linalg_latex expr=
exprraw=raw
shape
+ `expr`
ensure_plugin > linalg_shape expr=
expr
show
+ `expr`
+ `path`=None
+ `raw`=False
Record a formula SVG via host plot channel (default simplify). Returns the expr map.
ensure_plugin > linalg_show expr=
exprpath=pathraw=raw
eye
+ `n`
ensure_plugin > linalg_eye n=
n
zeros
+ `rows`
+ `cols`
ensure_plugin > linalg_zeros rows=
rowscols=cols
from_list
+ `data`
Dense leaf from nested list.
ensure_plugin > linalg_from_list data=
data
from_formula
+ `formula`
Absorb a $$ numeric matrix formula (or nested list / dense map) as a dense leaf.
ensure_plugin > linalg_from_formula formula=
formula
explicit
+ `expr`
+ `raw`=False
Evaluate to dense (default simplify first).
ensure_plugin > linalg_explicit expr=
exprraw=raw
det
+ `expr`
ensure_plugin > linalg_det expr=
expr
trace
+ `expr`
ensure_plugin > linalg_trace expr=
expr
solve
+ `a`=None
+ `b`
+ `factor`=None
Solve a x = b (Gaussian elimination), or reuse factorize kind=lu via factor + b.
ensure_plugin > linalg_solve a=
ab=bfactor=factor
matmul
+ `a`
+ `b`
Dense matrix product → linalg_dense.
ensure_plugin > linalg_matmul a=
ab=b
kron
+ `a`
+ `b`
Lazy Kronecker product (does not expand).
ensure_plugin > linalg_kron a=
ab=b
block
+ `blocks`
Block matrix from a nested list of expressions.
ensure_plugin > linalg_block blocks=
blocks
collapse
+ `expr`
Block-level collapse (Block*Block → block of products).
ensure_plugin > linalg_collapse expr=
expr
factorize
+ `matrix`
+ `kind`="lu"
Dense factorization → linalg_factor (kind = lu | qr | svd | eig | chol).
ensure_plugin > linalg_factorize matrix=
matrixkind=kind
draw
+ `factor`
+ `kind`=None
+ `path`=None
+ `theme`="light"
Structure SVG (eig | svd | qr | lu | ge | chol | structure | heatmap | hinton). Theme tokens match quantum Q8: light | dark | bw.
ensure_plugin > linalg_draw factor=
factorkind=kindpath=paththeme=theme
lstsq
+ `a`
+ `b`
Least squares min ||a x - b|| (QR if tall, SVD if wide).
ensure_plugin > linalg_lstsq a=
ab=b
norm
+ `expr`
+ `ord`="fro"
Matrix norm: fro | 1 | inf | 2. Complex dense supports fro only.
ensure_plugin > linalg_norm expr=
exprord=ord
cond
+ `expr`
Spectral condition number via SVD (ord=2).
ensure_plugin > linalg_cond expr=
expr
rank
+ `expr`
Numerical rank (SVD).
ensure_plugin > linalg_rank expr=
expr
declare
+ `table`
Declare several matrix symbols from an @ record table (name / rows / cols). Returns a map env.
ensure_plugin > linalg_declare table=
tabletype_tag="matrix"
symbol
+ `name`
+ `rows`
+ `cols`
Abstract matrix symbol. Returns a # matrix instance (supports + / * and methods).
ensure_plugin > matrix name=
namerows=rowscols=cols
matrix
+ `name`=None
+ `rows`=None
+ `cols`=None
+ `data`=None
+ `formula`=None
+ `n`=None
Matrix value (_type=matrix). Prefer infix A + B / A * B, or methods `A`.T.
ensure_plugin
formula> linalg_from_formula formula=formuladata> linalg_from_list data=dataname> linalg_symbol name=namerows=rowscols=colsn> linalg_eye n=nrows> linalg_zeros rows=rowscols=cols-
print text=ext/linalg: matrix needs name= / data= / formula= / n= / rows= None
T
> linalg_transpose expr=self
T_ascii
Transpose then display ASCII (auto-simplified). Handy for lecture identities.
Pt = > linalg_transpose expr=self
> linalg_ascii expr=Pt raw=False
add
+ `b`
> linalg_add a=self b=b
sub
+ `b`
> linalg_sub a=self b=b
mul
+ `b`
> linalg_mul a=self b=b
inv
> linalg_inv expr=self
simplify
> linalg_simplify expr=self
ascii
> linalg_ascii expr=self raw=False
latex
> linalg_latex expr=self raw=False
explicit
> linalg_explicit expr=self raw=False
det
> linalg_det expr=self
trace
> linalg_trace expr=self
show
+ `path`=None
+ `raw`=False
> linalg_show expr=self path=path raw=raw
eye
+ `n`
> linalg_eye n=n
zeros
+ `rows`
+ `cols`
> linalg_zeros rows=rows cols=cols
from_list
+ `data`
> linalg_from_list data=data
from_formula
+ `formula`
> linalg_from_formula formula=formula