Files
marqdo/ext/linalg/linalg.mq.md
T

429 lines
6.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
markup: occlusion
title: ext/linalg/linalg
description: Formula-first linear algebra (English). MatExpr simplify + display; ABI plugin.
import:
plugin: lib/plugin.mq.md
sys: lib/sys.mq.md
---
Formula-first linear algebra (English). MatExpr simplify + display; ABI plugin.
## ensure_plugin
Load the ABI v2 `linalg` plugin once.
**p = > plugin.native_path name="linalg"**
1. `p`
> plugin.load path=`p`
2. *
> print text=ext/linalg: native linalg plugin not found (build marqdo_plugin_linalg or marqdo ext add linalg)
> sys.exit code=1
****
## ping
Plugin health check.
> ensure_plugin
*> linalg_ping*
## mul
+ `a`
+ `b`
Matrix product (lazy MatMul; shape-checked).
> ensure_plugin
*> linalg_mul a=`a` b=`b`*
## add
+ `a`
+ `b`
Matrix / expression addition.
> ensure_plugin
*> linalg_add a=`a` b=`b`*
## sub
+ `a`
+ `b`
Matrix / expression subtraction.
> ensure_plugin
*> linalg_sub a=`a` b=`b`*
## transpose
+ `expr`
Transpose expression.
> 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=`expr` raw=`raw`*
## latex
+ `expr`
+ `raw`=False
LaTeX display.
> ensure_plugin
*> linalg_latex expr=`expr` raw=`raw`*
## shape
+ `expr`
Matrix shape (rows, cols).
> 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=`expr` path=`path` raw=`raw`*
## eye
+ `n`
Identity matrix of order n.
> ensure_plugin
*> linalg_eye n=`n`*
## zeros
+ `rows`
+ `cols`
Zero matrix.
> ensure_plugin
*> linalg_zeros rows=`rows` cols=`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=`expr` raw=`raw`*
## det
+ `expr`
Determinant.
> ensure_plugin
*> linalg_det expr=`expr`*
## trace
+ `expr`
Trace.
> 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=`a` b=`b` factor=`factor`*
## matmul
+ `a`
+ `b`
Dense matrix product → `linalg_dense`.
> ensure_plugin
*> linalg_matmul a=`a` b=`b`*
## kron
+ `a`
+ `b`
Lazy Kronecker product (does not expand).
> ensure_plugin
*> linalg_kron a=`a` b=`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=`matrix` kind=`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=`factor` kind=`kind` path=`path` theme=`theme`*
## lstsq
+ `a`
+ `b`
Least squares `min ||a x - b||` (QR if tall, SVD if wide).
> ensure_plugin
*> linalg_lstsq a=`a` b=`b`*
## norm
+ `expr`
+ `ord`="fro"
Matrix norm: `fro` | `1` | `inf` | `2`. Complex dense supports `fro` only.
> ensure_plugin
*> linalg_norm expr=`expr` ord=`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=`table` type_tag="matrix"*
## symbol
+ `name`
+ `rows`
+ `cols`
Abstract matrix symbol. Returns a `# matrix` instance (supports `+` / `*` and methods).
> ensure_plugin
*> matrix name=`name` rows=`rows` cols=`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
1. `formula`
*> linalg_from_formula formula=`formula`*
2. `data`
*> linalg_from_list data=`data`*
3. `name`
*> linalg_symbol name=`name` rows=`rows` cols=`cols`*
4. `n`
*> linalg_eye n=`n`*
5. `rows`
*> linalg_zeros rows=`rows` cols=`cols`*
6. *
> print text=ext/linalg: matrix needs name= / data= / formula= / n= / rows=
*None*
## T
Transpose (method).
*> 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`
Matrix / expression addition.
*> linalg_add a=`self` b=`b`*
## sub
+ `b`
Matrix / expression subtraction.
*> linalg_sub a=`self` b=`b`*
## mul
+ `b`
Matrix / expression product.
*> linalg_mul a=`self` b=`b`*
## inv
Inverse.
*> linalg_inv expr=`self`*
## simplify
Simplify MatExpr.
*> linalg_simplify expr=`self`*
## ascii
ASCII display.
*> linalg_ascii expr=`self` raw=False*
## latex
LaTeX display.
*> linalg_latex expr=`self` raw=False*
## explicit
Evaluate to an explicit matrix.
*> linalg_explicit expr=`self` raw=False*
## det
Determinant.
*> linalg_det expr=`self`*
## trace
Trace.
*> linalg_trace expr=`self`*
## show
+ `path`=None
+ `raw`=False
Show / export the expression.
*> linalg_show expr=`self` path=`path` raw=`raw`*
## eye
+ `n`
Identity matrix of order n.
*> linalg_eye n=`n`*
## zeros
+ `rows`
+ `cols`
Zero matrix.
*> linalg_zeros rows=`rows` cols=`cols`*
## from_list
+ `data`
Matrix from nested list.
*> linalg_from_list data=`data`*
## from_formula
+ `formula`
Matrix from formula text.
*> linalg_from_formula formula=`formula`*