From 3ade036d34e56546a71c22024854c941e1b443ac Mon Sep 17 00:00:00 2001 From: cflmy Date: Mon, 7 Sep 2026 16:13:22 +0800 Subject: [PATCH] feat(linalg): F3 formula-doc public pages and T_ascii MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .cursor/skills/marqdo/examples.md | 16 ++++----- CHANGELOG.md | 2 +- doc/design/ext-linalg-formula-doc.md | 10 +++--- doc/roadmap/ext-linalg.md | 15 +++++---- examples/linalg-transpose/index.mq.md | 3 +- ext/linalg/linalg.mq.md | 16 ++++++--- ext/linalg/线性代数.mq.md | 17 +++++++--- plugins/linalg/src/lib.rs | 16 +++++++-- public/README.md | 2 +- public/features/07-linalg.mq.md | 47 ++++++++++++++------------- public/features/07-线性代数.mq.md | 47 ++++++++++++++------------- tests/ext/linalg-doc-smoke.mq.md | 4 +-- 12 files changed, 114 insertions(+), 81 deletions(-) diff --git a/.cursor/skills/marqdo/examples.md b/.cursor/skills/marqdo/examples.md index 21b7362..5449c8d 100644 --- a/.cursor/skills/marqdo/examples.md +++ b/.cursor/skills/marqdo/examples.md @@ -346,15 +346,11 @@ import la:ext/linalg/linalg.mq.md Prefer: ```markdown -*`A` = > la.symbol name="A" rows=2 cols=2* -*`B` = > la.symbol name="B" rows=2 cols=2* -*`P` = `A` * `B`* -*`Pt` = > `P`.T* -*`s` = > `Pt`.simplify* -*`t` = > `s`.ascii* -> print text=`t` +*`env` = > la.declare table=`shapes`* +*`P` = env[^A] * env[^B]* +*`t` = > `P`.T_ascii* ``` -- Infix `+` / `-` / `*` dispatch when both sides are matrices (needs linalg plugin loaded). -- `# matrix` methods: `T`, `add`, `mul`, `simplify`, `ascii`, … -- Examples: `examples/linalg-svd/`, `examples/linalg-least-squares/`. +- Infix `+` / `-` / `*`;`ascii` / `T_ascii` 默认化简(`raw=True` 可关)。 +- Formula-doc design: [ext-linalg-formula-doc.md](../../doc/design/ext-linalg-formula-doc.md). +- Examples: `examples/linalg-transpose/`, `examples/linalg-svd/`, `examples/linalg-least-squares/`. diff --git a/CHANGELOG.md b/CHANGELOG.md index 808a7e3..9c6dd59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ## Unreleased ### Added -- **`ext/linalg`**:L0–L6 + 中缀/`# matrix`;公式文档面 [ext-linalg-formula-doc.md](doc/design/ext-linalg-formula-doc.md);F1 展示默认 simplify;F2 `declare`(GFM 表声明符号)+ 示例 `linalg-transpose`;金样 `linalg-doc-smoke` · `linalg-infix-smoke`。设计:[ext-linalg.md](doc/design/ext-linalg.md)。 +- **`ext/linalg`**:L0–L6 + 公式文档面 **F1–F3**(中缀、`declare`、展示/`explicit` 默认化简、`T_ascii`、public「如何写公式文档」);示例 `linalg-transpose` · `linalg-svd` · `linalg-least-squares`。设计:[ext-linalg-formula-doc.md](doc/design/ext-linalg-formula-doc.md)。 ### Fixed diff --git a/doc/design/ext-linalg-formula-doc.md b/doc/design/ext-linalg-formula-doc.md index b9c69af..0624f51 100644 --- a/doc/design/ext-linalg-formula-doc.md +++ b/doc/design/ext-linalg-formula-doc.md @@ -120,7 +120,7 @@ | C. `$$` 符号方言 | `$$A_{m\times n}$$` → MatExpr | 最像讲义 | 解析歧义;勿污染标量 formula | | D. 保持 `symbol` + 短别名 | `` `A` = > la.A rows=m cols=n `` | 实现便宜 | 文档感一般 | -**锁定倾向:** 短期 **A + 默认化简(F1)**;中期评估 **B 或受限 C**(C 必须留在 ext 吸收层,**不**扩展核心 `formula::Expr` 符号矩阵)。 +**锁定倾向:** 短期 **A + 默认化简(F1)**;**F2 采用 B(GFM `@` 表 + `la.declare`)**;受限 C(`$$` 符号方言)仍待评估且不得污染核心 formula。 ### 3.3 明确不做(本波语法) @@ -175,7 +175,9 @@ | 中缀 `+` `-` `*` + `# matrix` 方法 | done(v1) | | `ascii`/`latex`/`show` 默认 simplify | **done(F1)** | | 更少噪音的符号引入 | **done(F2:`declare` + 表)** | -| 更紧凑转置 / 混合公式书写 | F3 | +| 更紧凑转置 / 混合公式书写 | **done(F3:`T_ascii`)** | +| `explicit` 前可选 auto-simplify | **done(F3,默认开)** | +| public「如何写公式文档」 | **done(F3)** | | 矩阵微积分等 | L7+ / 规则表扩展 | --- @@ -185,8 +187,8 @@ | 阶段 | 内容 | 出口 | |------|------|------| | **F1** | 展示路径默认 simplify;金样「`.T` + `ascii` → 规范形」;设计本文落地 | **done** | -| **F2** | 符号引入降噪(选定 §3.2 候选);更新 examples 使其读起来像讲义 | 至少 1 个 example 少 `la.mul` | -| **F3** | 可选 postfix / 链式糖;`explicit` 前可选 auto-simplify;public 短文「如何写公式文档」 | skill + public 同步 | +| **F2** | 符号引入降噪(选定 §3.2 候选);更新 examples 使其读起来像讲义 | **done**(`declare` + `linalg-transpose`) | +| **F3** | 可选 postfix / 链式糖;`explicit` 前可选 auto-simplify;public 短文「如何写公式文档」 | **done** | | **F4+** | 新规则(伴随、多项式矩阵…);矩阵微积分仅当明确需要 | 规则表可测 | --- diff --git a/doc/roadmap/ext-linalg.md b/doc/roadmap/ext-linalg.md index fbb94b2..115d9f7 100644 --- a/doc/roadmap/ext-linalg.md +++ b/doc/roadmap/ext-linalg.md @@ -2,7 +2,7 @@ | | | |---|---| -| 状态 | **L0–L6 + 中缀 v1 已落地 · F1–F3 公式文档面进行中** | +| 状态 | **L0–L6 + 公式文档面 F1–F3 已落地 · L7+ 远期** | | 日期 | 2026-09-07 | | **锁定设计** | [design/ext-linalg.md](../design/ext-linalg.md) · [design/ext-linalg-formula-doc.md](../design/ext-linalg-formula-doc.md) | | 调研 | [research/ext-linalg-formula.md](../research/ext-linalg-formula.md) | @@ -32,7 +32,7 @@ | **L6** | 复数 dtype;`lstsq`;`norm`/`cond`;主题令牌对齐 | **done** | | **F1** | 展示路径默认 simplify;公式文档金样 | **done** | | **F2** | 符号引入降噪;examples 讲义化 | **done** | -| **F3** | 紧凑糖 + public「如何写公式文档」 | **pending** | +| **F3** | 紧凑糖 + public「如何写公式文档」 | **done** | | **L7+** | 矩阵微积分 / 更大 BLAS 依赖(仅当用户明确要求) | **deferred** | 验收金样(规划路径):`tests/ext/linalg-*-smoke.mq.md`;示例:`examples/linalg-least-squares/`、`examples/linalg-svd/`。 @@ -94,6 +94,12 @@ - [x] 符号引入降噪(选定候选) — GFM `@` 表 + `la.declare` - [x] 至少 1 个 example 以代数面为主叙述 — `examples/linalg-transpose/` +### F3 + +- [x] `T_ascii` / `转置文本` 一步展示糖 +- [x] `explicit` 默认先 simplify(`raw=` 可关) +- [x] public `07-linalg` / `07-线性代数`「如何写公式文档」 + --- ## 4. 依赖与并行 @@ -111,10 +117,7 @@ ## 5. 建议开发顺序(下一手) -1. **F1** 展示默认化简(公式文档面)。 -2. **F2** 符号引入与 examples 讲义化。 -3. **F3** public / skill 收口。 -4. **L7+** 仅当用户明确要求更深符号(微积分等)。 +1. **L7+** 仅当用户明确要求更深符号(微积分等)。公式文档面 F1–F3 已收口。 --- diff --git a/examples/linalg-transpose/index.mq.md b/examples/linalg-transpose/index.mq.md index 1c3653c..d8e99fa 100644 --- a/examples/linalg-transpose/index.mq.md +++ b/examples/linalg-transpose/index.mq.md @@ -21,8 +21,7 @@ Declare shapes in a table, form \(P = AB\), then check \((AB)^\top = B^\top A^\t *`A` = env[^A]* *`B` = env[^B]* *`P` = `A` * `B`* -*`Pt` = > `P`.T* -*`t` = > `Pt`.ascii* +*`t` = > `P`.T_ascii* > print text=`t` 1. `t` == "B^T*A^T" > print text=identity-ok diff --git a/ext/linalg/linalg.mq.md b/ext/linalg/linalg.mq.md index dc86f2e..710c7d1 100644 --- a/ext/linalg/linalg.mq.md +++ b/ext/linalg/linalg.mq.md @@ -130,11 +130,12 @@ Absorb a `$$` numeric matrix formula (or nested list / dense map) as a dense lea ## explicit + `expr` + + `raw`=False -Evaluate a MatExpr tree of dense/eye/zero ops to `linalg_dense` (symbols rejected). +Evaluate to dense (default **simplify** first). > ensure_plugin -**> linalg_explicit expr=`expr`** +**> linalg_explicit expr=`expr` raw=`raw`** ## det + `expr` @@ -252,7 +253,7 @@ Numerical rank (SVD). Declare several matrix symbols from an `@` record table (`name` / `rows` / `cols`). Returns a map env. > ensure_plugin -**> linalg_declare table=`table`** +**> linalg_declare table=`table` type_tag="matrix"** ## symbol + `name` @@ -293,6 +294,13 @@ Matrix value (`_type=matrix`). Prefer infix `A + B` / `A * B`, or methods `` `A` **> 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` @@ -326,7 +334,7 @@ Matrix value (`_type=matrix`). Prefer infix `A + B` / `A * B`, or methods `` `A` ## explicit -**> linalg_explicit expr=`self`** +**> linalg_explicit expr=`self` raw=False** ## det diff --git a/ext/linalg/线性代数.mq.md b/ext/linalg/线性代数.mq.md index 1d03d38..361e410 100644 --- a/ext/linalg/线性代数.mq.md +++ b/ext/linalg/线性代数.mq.md @@ -126,11 +126,12 @@ import sys:lib/sys.mq.md ## 求值 + `式` + + `原始`=False -将仅含稠密/单位/零阵运算的表达式求成 `linalg_dense`。 +求成稠密(默认先 **化简**)。 > 确保插件 -**> linalg_explicit expr=`式`** +**> linalg_explicit expr=`式` raw=`原始`** ## 行列式 + `式` @@ -246,7 +247,7 @@ import sys:lib/sys.mq.md 用 `@` 记录表一次声明多个符号矩阵(`名` / `行` / `列`)。返回环境 map。 > 确保插件 -**> linalg_declare table=`表`** +**> linalg_declare table=`表` type_tag="矩阵"** ## 符号 + `名` @@ -287,6 +288,13 @@ import sys:lib/sys.mq.md **> linalg_transpose expr=`self`** +## 转置文本 + +转置后输出 ASCII(默认化简)。适合讲义恒等式验收。 + +*`转` = > linalg_transpose expr=`self`* +**> linalg_ascii expr=`转` raw=False** + ## 加 + `右` @@ -319,8 +327,9 @@ import sys:lib/sys.mq.md **> linalg_latex expr=`self` raw=False** ## 求值 + + `原始`=False -**> linalg_explicit expr=`self`** +**> linalg_explicit expr=`self` raw=`原始`** ## 行列式 diff --git a/plugins/linalg/src/lib.rs b/plugins/linalg/src/lib.rs index 7fadaf6..13a8316 100644 --- a/plugins/linalg/src/lib.rs +++ b/plugins/linalg/src/lib.rs @@ -283,6 +283,11 @@ la_ffi!(linalg_declare, |args: &Value| { let rows = table .as_array() .ok_or_else(|| "`table` must be a list of row maps".to_string())?; + let tag = args + .get("type_tag") + .or_else(|| args.get("类型标签")) + .and_then(|x| x.as_str()) + .unwrap_or("matrix"); let mut out = serde_json::Map::new(); out.insert("_type".into(), json!("matrix_env")); for (i, row) in rows.iter().enumerate() { @@ -306,7 +311,11 @@ la_ffi!(linalg_declare, |args: &Value| { .ok_or_else(|| format!("declare `{name}` missing cols"))?, )?; let expr = matexpr::symbol(&name, r, c); - out.insert(name, expr.to_value()?); + let mut v = expr.to_value()?; + if let Some(m) = v.as_object_mut() { + m.insert("_type".into(), json!(tag)); + } + out.insert(name, v); } Ok(Value::Object(out)) }); @@ -426,6 +435,7 @@ la_ffi!(linalg_explicit, |args: &Value| { .ok_or_else(|| "missing `expr`".to_string())?; coerce_expr(f) })?; + let e = display_expr(args, e)?; let m = dense::explicit(&e)?; dense::to_dense_value(&m) }); @@ -640,7 +650,7 @@ pub unsafe extern "C" fn marqdo_plugin_init(host: *const MarqdoHostApi) -> c_int ("linalg_eye", "n", linalg_eye as PluginFn), ("linalg_zeros", "rows,cols", linalg_zeros as PluginFn), ("linalg_from_list", "data", linalg_from_list as PluginFn), - ("linalg_declare", "table", linalg_declare as PluginFn), + ("linalg_declare", "table,type_tag", linalg_declare as PluginFn), ("linalg_from_formula", "formula", linalg_from_formula as PluginFn), ("linalg_mul", "a,b", linalg_mul as PluginFn), ("linalg_add", "a,b", linalg_add as PluginFn), @@ -652,7 +662,7 @@ pub unsafe extern "C" fn marqdo_plugin_init(host: *const MarqdoHostApi) -> c_int ("linalg_latex", "expr,raw", linalg_latex as PluginFn), ("linalg_shape", "expr", linalg_shape as PluginFn), ("linalg_show", "expr,path,raw", linalg_show as PluginFn), - ("linalg_explicit", "expr", linalg_explicit as PluginFn), + ("linalg_explicit", "expr,raw", linalg_explicit as PluginFn), ("linalg_det", "expr", linalg_det as PluginFn), ("linalg_trace", "expr", linalg_trace as PluginFn), ("linalg_solve", "a,b,factor", linalg_solve as PluginFn), diff --git a/public/README.md b/public/README.md index 0f2b6b8..e24a470 100644 --- a/public/README.md +++ b/public/README.md @@ -12,7 +12,7 @@ | [`stdlib/`](stdlib/) | **标准库**(导入方式 + text/表/文件/时间/系统/json/网络/数学/外联/插件/**自写回**/**子任务**;v0.1.2+ 内置于 CLI) | | [`features/`](features/) | 特性迭代(含对象、官方 `ext/`) | -官方扩展(非 stdlib):`ext/llm`、`ext/agent` — 见 `features/05-extensions.mq.md` / `05-扩展.mq.md` 与 `doc/design/ext-*.md`。 +官方扩展(非 stdlib):`ext/llm`、`ext/agent`、`ext/web`、`ext/quantum`、`ext/linalg` — 见 `features/05-*.mq.md`、`06-*.mq.md`、`07-linalg.mq.md` / `07-线性代数.mq.md` 与 `doc/design/ext-*.md`。 **不放** `errors/`:失败样例只在 `tests/errors/`。 diff --git a/public/features/07-linalg.mq.md b/public/features/07-linalg.mq.md index 9b14f44..9119188 100644 --- a/public/features/07-linalg.mq.md +++ b/public/features/07-linalg.mq.md @@ -1,32 +1,35 @@ --- -title: Linear algebra (ext/linalg) -description: Formula MatExpr, dense eval, factorize, structure SVG +title: How to write a linear-algebra formula document +description: ext/linalg formula-doc — declare, infix, run-implies-correct import la:ext/linalg/linalg.mq.md --- +# Formula documents with ext/linalg + +Not stdlib. Install: `marqdo ext add linalg` (or build `marqdo_plugin_linalg`). + +**Idea:** if the `.mq.md` runs and the asserted form matches, the identity holds under the library rules. Algebra reads like a lecture; `factorize` / `lstsq` / `draw` stay explicit calculation steps. + # main -Optional package (not stdlib). Install: `marqdo ext add linalg` (or build `marqdo_plugin_linalg`). +Declare symbols in a table, multiply, transpose — `T_ascii` is one-step display (auto-simplify). -Formula-first: symbols stay symbolic until `explicit` / `factorize`. `(AB)^T` simplifies to `B^T*A^T` without dense data. +`shapes` = -L4–L5: `factorize kind=eig|svd|qr|lu|chol`, `draw kind=eig|svd|heatmap|hinton`, examples `examples/linalg-svd/` · `examples/linalg-least-squares/`. - -*`A` = > la.symbol name="A" rows=2 cols=2* -*`B` = > la.symbol name="B" rows=2 cols=2* -*`P` = > la.mul a=`A` b=`B`* -*`Pt` = > la.transpose expr=`P`* -*`s` = > la.simplify expr=`Pt`* -*`t` = > la.ascii expr=`s`* - -`M` = -$$ -\begin{bmatrix}2&0\\0&3\end{bmatrix} -$$ - -*`M` = > la.from_formula formula=`M`* -*`f` = > la.factorize matrix=`M` kind="eig"* -*`_` = > la.draw factor=`f` kind="eig"* +| @ | name | rows | cols | +|---|------|------|------| +| 1 | A | 2 | 3 | +| 2 | B | 3 | 2 | +*`env` = > la.declare table=`shapes`* +*`A` = env[^A]* +*`B` = env[^B]* +*`P` = `A` * `B`* +*`t` = > `P`.T_ascii* > print text=`t` -> print text=linalg-feature-ok +1. `t` == "B^T*A^T" + > print text=formula-doc-ok +2. * + > print text=formula-doc-fail + +> print text=see-examples-linalg-transpose diff --git a/public/features/07-线性代数.mq.md b/public/features/07-线性代数.mq.md index 6f25602..a2dbe52 100644 --- a/public/features/07-线性代数.mq.md +++ b/public/features/07-线性代数.mq.md @@ -1,32 +1,35 @@ --- -title: 线性代数(ext/linalg) -description: 公式 MatExpr、稠密求值、分解与结构 SVG +title: 如何写线性代数公式文档 +description: ext/linalg 公式文档面 — 声明、中缀、跑通即正确 导入 la:ext/linalg/线性代数.mq.md --- +# 用 ext/linalg 写公式文档 + +可选扩展(非 stdlib)。安装:`marqdo ext add linalg`(中文 id:`线性代数`)。 + +**思想:** `.mq.md` 能跑通且断言的规范形一致,则在库规则下该恒等式成立。代数叙述像讲义;`分解` / `最小二乘` / `绘图` 才是显式计算步骤。 + # main -可选扩展(非 stdlib)。安装:`marqdo ext add linalg`(中文 id:`线性代数`),或先编译 `marqdo_plugin_linalg`。 +用表声明符号,再乘与转置 — `转置文本` 一步展示(默认化简)。 -公式优先:符号式在 `求值` / `分解` 前保持惰性。`(AB)^T` 化简为 `B^T*A^T`,不生成稠密数据。 +`形状` = -L4–L5:`分解`、`绘图`(`eig`/`svd`/`heatmap`/`hinton`);示例 `examples/linalg-svd/` · `examples/linalg-least-squares/`。 - -*`A` = > la.符号 名="A" 行=2 列=2* -*`B` = > la.符号 名="B" 行=2 列=2* -*`P` = > la.乘 左=`A` 右=`B`* -*`Pt` = > la.转置 式=`P`* -*`s` = > la.化简 式=`Pt`* -*`t` = > la.文本 式=`s`* - -`M` = -$$ -\begin{bmatrix}2&0\\0&3\end{bmatrix} -$$ - -*`M` = > la.自公式 公式=`M`* -*`f` = > la.分解 矩阵=`M` 类型="eig"* -*`_` = > la.绘图 分解=`f` 类型="eig"* +| @ | 名 | 行 | 列 | +|---|----|----|----| +| 1 | A | 2 | 3 | +| 2 | B | 3 | 2 | +*`环境` = > la.声明 表=`形状`* +*`A` = 环境[^A]* +*`B` = 环境[^B]* +*`P` = `A` * `B`* +*`t` = > `P`.转置文本* > 打印 内容=`t` -> 打印 内容=linalg-feature-ok +1. `t` == "B^T*A^T" + > 打印 内容=formula-doc-ok +2. * + > 打印 内容=formula-doc-fail + +> 打印 内容=see-examples-linalg-transpose diff --git a/tests/ext/linalg-doc-smoke.mq.md b/tests/ext/linalg-doc-smoke.mq.md index 4641d09..6bbc183 100644 --- a/tests/ext/linalg-doc-smoke.mq.md +++ b/tests/ext/linalg-doc-smoke.mq.md @@ -13,8 +13,7 @@ Narration: \((AB)^\top = B^\top A^\top\). The identity is checked by running thi *`A` = > la.symbol name="A" rows=2 cols=3* *`B` = > la.symbol name="B" rows=3 cols=2* *`P` = `A` * `B`* -*`Pt` = > `P`.T* -*`t` = > `Pt`.ascii* +*`t` = > `P`.T_ascii* > print text=`t` 1. `t` == "B^T*A^T" > print text=identity-ok @@ -38,6 +37,7 @@ Narration: \((AB)^\top = B^\top A^\top\). The identity is checked by running thi 2. * > print text=declare-fail +*`Pt` = > `P`.T* *`raw` = > la.ascii expr=`Pt` raw=True* 1. `raw` == "(A*B)^T" > print text=raw-ok