feat(linalg): L0–L1 formula-first MatExpr plugin

Add plugins/linalg with symbol/mul/transpose/inv simplify (R1–R5),
EN/ZH L1, CATALOG/ext add wiring, release native assets, and smokes.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-09-07 14:10:54 +08:00
1 parent b6cceab218
commit 96da88e168
15 files changed
+1542 -22

No files matched your search

+15
View File
@@ -0,0 +1,15 @@
[package]
name = "marqdo_plugin_linalg"
version = "0.1.0"
edition = "2021"
license = "Apache-2.0"
description = "Marqdo linear algebra plugin — formula-first MatExpr (ABI v2)"
publish = false
[lib]
name = "linalg"
crate-type = ["cdylib"]
[dependencies]
serde = { version = "1", features = ["derive"] }
serde_json = "1"
+387
View File
@@ -0,0 +1,387 @@
//! Marqdo linalg plugin (C ABI v2): formula-first matrix expressions.
mod matexpr;
use std::ffi::{CStr, CString};
use std::os::raw::{c_char, c_int, c_void};
use std::ptr;
use serde_json::{json, Value};
use matexpr::{Dim, Expr};
const ABI_VERSION: u32 = 2;
type PluginFn = unsafe extern "C" fn(
args_json: *const c_char,
out_json: *mut *mut c_char,
err_msg: *mut *mut c_char,
) -> c_int;
type HostQueryFn = unsafe extern "C" fn(
userdata: *mut c_void,
name: *const c_char,
args_json: *const c_char,
out_json: *mut *mut c_char,
err_msg: *mut *mut c_char,
) -> c_int;
#[repr(C)]
pub struct MarqdoHostApi {
pub userdata: *mut c_void,
pub register_fn: Option<
unsafe extern "C" fn(
userdata: *mut c_void,
name: *const c_char,
params: *const c_char,
fn_ptr: PluginFn,
) -> c_int,
>,
pub alloc: Option<unsafe extern "C" fn(n: usize) -> *mut c_void>,
pub free: Option<unsafe extern "C" fn(p: *mut c_void)>,
pub host_query: Option<HostQueryFn>,
}
static mut HOST_FREE: Option<unsafe extern "C" fn(*mut c_void)> = None;
static mut HOST_ALLOC: Option<unsafe extern "C" fn(usize) -> *mut c_void> = None;
static mut HOST_QUERY: Option<HostQueryFn> = None;
static mut HOST_USERDATA: *mut c_void = ptr::null_mut();
unsafe fn host_strdup(s: &str) -> *mut c_char {
let alloc = HOST_ALLOC.expect("host alloc");
let bytes = s.as_bytes();
let p = alloc(bytes.len() + 1) as *mut u8;
if p.is_null() {
return ptr::null_mut();
}
std::ptr::copy_nonoverlapping(bytes.as_ptr(), p, bytes.len());
*p.add(bytes.len()) = 0;
p as *mut c_char
}
fn set_out(out: *mut *mut c_char, s: &str) {
if out.is_null() {
return;
}
unsafe {
*out = host_strdup(s);
}
}
fn set_err(err: *mut *mut c_char, s: &str) {
if err.is_null() {
return;
}
unsafe {
*err = host_strdup(s);
}
}
fn parse_args(args_json: *const c_char) -> Result<Value, String> {
if args_json.is_null() {
return Ok(json!({}));
}
let s = unsafe { CStr::from_ptr(args_json) }
.to_str()
.map_err(|_| "args not utf-8".to_string())?;
if s.trim().is_empty() {
return Ok(json!({}));
}
serde_json::from_str(s).map_err(|e| e.to_string())
}
fn reply(out_json: *mut *mut c_char, err_msg: *mut *mut c_char, r: Result<Value, String>) -> c_int {
match r {
Ok(v) => {
set_out(out_json, &v.to_string());
0
}
Err(e) => {
set_err(err_msg, &e);
1
}
}
}
macro_rules! la_ffi {
($name:ident, $body:expr) => {
unsafe extern "C" fn $name(
args_json: *const c_char,
out_json: *mut *mut c_char,
err_msg: *mut *mut c_char,
) -> c_int {
let args = match parse_args(args_json) {
Ok(v) => v,
Err(e) => {
set_err(err_msg, &format!("{}: {e}", stringify!($name)));
return 1;
}
};
let r: Result<Value, String> = (|| $body(&args))();
reply(
out_json,
err_msg,
r.map_err(|e| format!("{}: {e}", stringify!($name))),
)
}
};
}
fn take_host_string(p: *mut c_char) -> Option<String> {
if p.is_null() {
return None;
}
let s = unsafe { CStr::from_ptr(p) }
.to_str()
.ok()
.map(|s| s.to_string());
unsafe {
if let Some(free) = HOST_FREE {
free(p as *mut c_void);
}
}
s
}
fn host_query_json(name: &str, args: &Value) -> Result<Value, String> {
let query = unsafe { HOST_QUERY }.ok_or_else(|| "host_query not available".to_string())?;
let userdata = unsafe { HOST_USERDATA };
let c_name = CString::new(name).map_err(|e| e.to_string())?;
let c_args = CString::new(args.to_string()).map_err(|e| e.to_string())?;
let mut out_ptr: *mut c_char = ptr::null_mut();
let mut err_ptr: *mut c_char = ptr::null_mut();
let rc = unsafe {
query(
userdata,
c_name.as_ptr(),
c_args.as_ptr(),
&mut out_ptr,
&mut err_ptr,
)
};
let err = take_host_string(err_ptr);
let out = take_host_string(out_ptr);
if rc != 0 {
return Err(err.unwrap_or_else(|| format!("host_query `{name}` failed")));
}
let out = out.unwrap_or_else(|| "null".into());
serde_json::from_str(&out).map_err(|e| format!("host_query `{name}` bad JSON: {e}"))
}
fn record_plot(svg: &str, path: Option<&str>) -> Result<(), String> {
let mut args = json!({ "svg": svg });
if let Some(p) = path {
if !p.is_empty() {
args.as_object_mut()
.unwrap()
.insert("path".into(), json!(p));
}
}
host_query_json("record_plot", &args)?;
Ok(())
}
fn arg_dim(v: &Value, key: &str) -> Result<Dim, String> {
Dim::from_json(v.get(key).ok_or_else(|| format!("missing `{key}`"))?)
}
fn arg_expr_opt(v: &Value, keys: &[&str]) -> Result<Expr, String> {
for k in keys {
if let Some(e) = v.get(*k) {
if !e.is_null() {
return Expr::from_value(e);
}
}
}
Err(format!("missing one of {:?}", keys))
}
fn wrap(expr: Expr) -> Result<Value, String> {
expr.to_value()
}
la_ffi!(linalg_ping, |_args: &Value| {
Ok(json!({
"ok": true,
"name": "linalg",
"abi": ABI_VERSION,
"features": ["matexpr", "simplify"],
}))
});
la_ffi!(linalg_symbol, |args: &Value| {
let name = args
.get("name")
.or_else(|| args.get("名"))
.and_then(|x| x.as_str())
.ok_or_else(|| "missing `name`".to_string())?;
let rows = arg_dim(args, "rows").or_else(|_| arg_dim(args, "行"))?;
let cols = arg_dim(args, "cols").or_else(|_| arg_dim(args, "列"))?;
wrap(matexpr::symbol(name, rows, cols))
});
la_ffi!(linalg_eye, |args: &Value| {
let n = arg_dim(args, "n")
.or_else(|_| arg_dim(args, "阶"))
.or_else(|_| arg_dim(args, "rows"))?;
wrap(matexpr::eye(n))
});
la_ffi!(linalg_zeros, |args: &Value| {
let rows = arg_dim(args, "rows").or_else(|_| arg_dim(args, "行"))?;
let cols = arg_dim(args, "cols").or_else(|_| arg_dim(args, "列"))?;
wrap(matexpr::zeros(rows, cols))
});
la_ffi!(linalg_from_list, |args: &Value| {
let data = args
.get("data")
.or_else(|| args.get("数据"))
.ok_or_else(|| "missing `data`".to_string())?;
let rows = data
.as_array()
.ok_or_else(|| "`data` must be list of rows".to_string())?;
let mut mat = Vec::new();
for row in rows {
let cells = row
.as_array()
.ok_or_else(|| "each row must be a list".to_string())?;
let mut r = Vec::new();
for c in cells {
let n = c
.as_f64()
.or_else(|| c.as_i64().map(|i| i as f64))
.or_else(|| c.as_u64().map(|u| u as f64))
.ok_or_else(|| "cell must be a number".to_string())?;
r.push(n);
}
mat.push(r);
}
wrap(matexpr::from_list(mat)?)
});
la_ffi!(linalg_mul, |args: &Value| {
let a = arg_expr_opt(args, &["a", "左"])?;
let b = arg_expr_opt(args, &["b", "右"])?;
wrap(matexpr::mul(a, b)?)
});
la_ffi!(linalg_add, |args: &Value| {
let a = arg_expr_opt(args, &["a", "左"])?;
let b = arg_expr_opt(args, &["b", "右"])?;
wrap(matexpr::add(a, b)?)
});
la_ffi!(linalg_sub, |args: &Value| {
let a = arg_expr_opt(args, &["a", "左"])?;
let b = arg_expr_opt(args, &["b", "右"])?;
wrap(matexpr::sub(a, b)?)
});
la_ffi!(linalg_transpose, |args: &Value| {
let e = arg_expr_opt(args, &["expr", "a", "式"])?;
wrap(matexpr::transpose(e)?)
});
la_ffi!(linalg_inv, |args: &Value| {
let e = arg_expr_opt(args, &["expr", "a", "式"])?;
wrap(matexpr::inv(e)?)
});
la_ffi!(linalg_simplify, |args: &Value| {
let e = arg_expr_opt(args, &["expr", "a", "式"])?;
wrap(matexpr::simplify(e)?)
});
la_ffi!(linalg_ascii, |args: &Value| {
let e = arg_expr_opt(args, &["expr", "a", "式"])?;
Ok(json!(e.ascii()))
});
la_ffi!(linalg_latex, |args: &Value| {
let e = arg_expr_opt(args, &["expr", "a", "式"])?;
Ok(json!(e.latex()))
});
la_ffi!(linalg_shape, |args: &Value| {
let e = arg_expr_opt(args, &["expr", "a", "式"])?;
let (r, c) = e.shape()?;
Ok(json!([r.to_json(), c.to_json()]))
});
la_ffi!(linalg_show, |args: &Value| {
let e = arg_expr_opt(args, &["expr", "a", "式"])?;
let latex = e.latex();
let ascii = e.ascii();
let path = args
.get("path")
.or_else(|| args.get("路径"))
.and_then(|x| x.as_str())
.filter(|s| !s.is_empty());
// Minimal SVG so view/plot channel can embed the formula text.
let escaped = latex
.replace('&', "&amp;")
.replace('<', "&lt;")
.replace('>', "&gt;");
let svg = format!(
"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"480\" height=\"64\" data-linalg=\"formula\">\
<text x=\"12\" y=\"40\" font-size=\"18\" font-family=\"serif\">{escaped}</text></svg>"
);
let _ = record_plot(&svg, path);
let mut out = e.to_value()?;
if let Some(obj) = out.as_object_mut() {
obj.insert("shown".into(), json!(true));
obj.insert("ascii".into(), json!(ascii));
obj.insert("latex".into(), json!(latex));
}
Ok(out)
});
#[no_mangle]
pub unsafe extern "C" fn marqdo_plugin_abi_version() -> u32 {
ABI_VERSION
}
#[no_mangle]
pub unsafe extern "C" fn marqdo_plugin_shutdown() {}
#[no_mangle]
pub unsafe extern "C" fn marqdo_plugin_init(host: *const MarqdoHostApi) -> c_int {
if host.is_null() {
return 1;
}
let host = &*host;
HOST_ALLOC = host.alloc;
HOST_FREE = host.free;
HOST_QUERY = host.host_query;
HOST_USERDATA = host.userdata;
let register = match host.register_fn {
Some(f) => f,
None => return 1,
};
let regs = [
("linalg_ping", "", linalg_ping as PluginFn),
("linalg_symbol", "name,rows,cols", linalg_symbol as PluginFn),
("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_mul", "a,b", linalg_mul as PluginFn),
("linalg_add", "a,b", linalg_add as PluginFn),
("linalg_sub", "a,b", linalg_sub as PluginFn),
("linalg_transpose", "expr", linalg_transpose as PluginFn),
("linalg_inv", "expr", linalg_inv as PluginFn),
("linalg_simplify", "expr", linalg_simplify as PluginFn),
("linalg_ascii", "expr", linalg_ascii as PluginFn),
("linalg_latex", "expr", linalg_latex as PluginFn),
("linalg_shape", "expr", linalg_shape as PluginFn),
("linalg_show", "expr,path", linalg_show as PluginFn),
];
for (name, params, f) in regs {
let c_name = CString::new(name).unwrap();
let c_params = CString::new(params).unwrap();
if register(host.userdata, c_name.as_ptr(), c_params.as_ptr(), f) != 0 {
return 1;
}
}
0
}
+725
View File
@@ -0,0 +1,725 @@
//! Symbolic matrix expressions (MatExpr) for formula-first linear algebra.
//!
//! Trees stay abstract until `explicit` (L2). Simplify applies R1–R5.
use serde_json::{json, Value};
#[derive(Debug, Clone, PartialEq)]
pub enum Dim {
Int(usize),
Sym(String),
}
impl Dim {
pub fn to_json(&self) -> Value {
match self {
Dim::Int(n) => json!(*n),
Dim::Sym(s) => json!(s),
}
}
pub fn from_json(v: &Value) -> Result<Self, String> {
match v {
Value::Number(n) => {
let u = n
.as_u64()
.or_else(|| n.as_i64().map(|i| i as u64))
.ok_or_else(|| "bad dim number".to_string())?;
Ok(Dim::Int(u as usize))
}
Value::String(s) => {
let t = s.trim();
if let Ok(n) = t.parse::<usize>() {
Ok(Dim::Int(n))
} else {
Ok(Dim::Sym(t.to_string()))
}
}
_ => Err("dim must be int or string".into()),
}
}
fn ascii(&self) -> String {
match self {
Dim::Int(n) => n.to_string(),
Dim::Sym(s) => s.clone(),
}
}
fn latex(&self) -> String {
self.ascii()
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Expr {
Symbol {
name: String,
rows: Dim,
cols: Dim,
},
Eye {
n: Dim,
},
Zero {
rows: Dim,
cols: Dim,
},
Dense {
data: Vec<Vec<f64>>,
},
Mul {
factors: Vec<Expr>,
},
Add {
terms: Vec<Expr>,
},
Sub {
left: Box<Expr>,
right: Box<Expr>,
},
Transpose {
arg: Box<Expr>,
},
Inv {
arg: Box<Expr>,
},
}
impl Expr {
pub fn shape(&self) -> Result<(Dim, Dim), String> {
match self {
Expr::Symbol { rows, cols, .. } => Ok((rows.clone(), cols.clone())),
Expr::Eye { n } => Ok((n.clone(), n.clone())),
Expr::Zero { rows, cols } => Ok((rows.clone(), cols.clone())),
Expr::Dense { data } => {
let r = data.len();
let c = data.first().map(|row| row.len()).unwrap_or(0);
if data.iter().any(|row| row.len() != c) {
return Err("dense matrix rows must have equal length".into());
}
Ok((Dim::Int(r), Dim::Int(c)))
}
Expr::Mul { factors } => {
if factors.is_empty() {
return Err("empty MatMul".into());
}
let (rows, mut cols) = factors[0].shape()?;
for f in &factors[1..] {
let (r, c) = f.shape()?;
if !dims_eq(&cols, &r) {
return Err(format!(
"incompatible shapes for mul: …×{} with {}×{}",
cols.ascii(),
r.ascii(),
c.ascii()
));
}
cols = c;
}
Ok((rows, cols))
}
Expr::Add { terms } => {
if terms.is_empty() {
return Err("empty MatAdd".into());
}
let (r0, c0) = terms[0].shape()?;
for t in &terms[1..] {
let (r, c) = t.shape()?;
if !dims_eq(&r0, &r) || !dims_eq(&c0, &c) {
return Err(format!(
"incompatible shapes for add: {}×{} vs {}×{}",
r0.ascii(),
c0.ascii(),
r.ascii(),
c.ascii()
));
}
}
Ok((r0, c0))
}
Expr::Sub { left, right } => {
let (r0, c0) = left.shape()?;
let (r1, c1) = right.shape()?;
if !dims_eq(&r0, &r1) || !dims_eq(&c0, &c1) {
return Err(format!(
"incompatible shapes for sub: {}×{} vs {}×{}",
r0.ascii(),
c0.ascii(),
r1.ascii(),
c1.ascii()
));
}
Ok((r0, c0))
}
Expr::Transpose { arg } => {
let (r, c) = arg.shape()?;
Ok((c, r))
}
Expr::Inv { arg } => {
let (r, c) = arg.shape()?;
if !dims_eq(&r, &c) {
return Err(format!(
"inverse needs square matrix, got {}×{}",
r.ascii(),
c.ascii()
));
}
Ok((r, c))
}
}
}
pub fn ascii(&self) -> String {
match self {
Expr::Symbol { name, .. } => name.clone(),
Expr::Eye { n } => format!("I({})", n.ascii()),
Expr::Zero { rows, cols } => format!("0({}×{})", rows.ascii(), cols.ascii()),
Expr::Dense { data } => format_dense_ascii(data),
Expr::Mul { factors } => factors
.iter()
.map(|f| {
let s = f.ascii();
if matches!(f, Expr::Add { .. } | Expr::Sub { .. }) {
format!("({s})")
} else {
s
}
})
.collect::<Vec<_>>()
.join("*"),
Expr::Add { terms } => terms
.iter()
.map(|t| t.ascii())
.collect::<Vec<_>>()
.join(" + "),
Expr::Sub { left, right } => format!("{} - {}", left.ascii(), right.ascii()),
Expr::Transpose { arg } => {
let inner = arg.ascii();
if matches!(
arg.as_ref(),
Expr::Symbol { .. } | Expr::Eye { .. } | Expr::Zero { .. } | Expr::Dense { .. }
) {
format!("{inner}^T")
} else {
format!("({inner})^T")
}
}
Expr::Inv { arg } => {
let inner = arg.ascii();
if matches!(
arg.as_ref(),
Expr::Symbol { .. } | Expr::Eye { .. } | Expr::Zero { .. } | Expr::Dense { .. }
) {
format!("{inner}^-1")
} else {
format!("({inner})^-1")
}
}
}
}
pub fn latex(&self) -> String {
match self {
Expr::Symbol { name, .. } => name.clone(),
Expr::Eye { n } => format!("I_{{{}}}", n.latex()),
Expr::Zero { rows, cols } => {
format!("0_{{{}\\times {}}}", rows.latex(), cols.latex())
}
Expr::Dense { data } => format_dense_latex(data),
Expr::Mul { factors } => factors
.iter()
.map(|f| {
let s = f.latex();
if matches!(f, Expr::Add { .. } | Expr::Sub { .. }) {
format!("\\left({s}\\right)")
} else {
s
}
})
.collect::<Vec<_>>()
.join(""),
Expr::Add { terms } => terms
.iter()
.map(|t| t.latex())
.collect::<Vec<_>>()
.join(" + "),
Expr::Sub { left, right } => format!("{} - {}", left.latex(), right.latex()),
Expr::Transpose { arg } => {
let inner = arg.latex();
if matches!(
arg.as_ref(),
Expr::Symbol { .. } | Expr::Eye { .. } | Expr::Zero { .. } | Expr::Dense { .. }
) {
format!("{inner}^{{\\top}}")
} else {
format!("\\left({inner}\\right)^{{\\top}}")
}
}
Expr::Inv { arg } => {
let inner = arg.latex();
if matches!(
arg.as_ref(),
Expr::Symbol { .. } | Expr::Eye { .. } | Expr::Zero { .. } | Expr::Dense { .. }
) {
format!("{inner}^{{-1}}")
} else {
format!("\\left({inner}\\right)^{{-1}}")
}
}
}
}
pub fn to_value(&self) -> Result<Value, String> {
let (rows, cols) = self.shape()?;
let mut obj = serde_json::Map::new();
obj.insert("_type".into(), json!("linalg_expr"));
obj.insert("shape".into(), json!([rows.to_json(), cols.to_json()]));
obj.insert("ascii".into(), json!(self.ascii()));
obj.insert("latex".into(), json!(self.latex()));
match self {
Expr::Symbol { name, rows, cols } => {
obj.insert("kind".into(), json!("symbol"));
obj.insert("name".into(), json!(name));
obj.insert("rows".into(), rows.to_json());
obj.insert("cols".into(), cols.to_json());
}
Expr::Eye { n } => {
obj.insert("kind".into(), json!("eye"));
obj.insert("n".into(), n.to_json());
}
Expr::Zero { rows, cols } => {
obj.insert("kind".into(), json!("zero"));
obj.insert("rows".into(), rows.to_json());
obj.insert("cols".into(), cols.to_json());
}
Expr::Dense { data } => {
obj.insert("kind".into(), json!("dense"));
obj.insert("data".into(), json!(data));
obj.insert("dtype".into(), json!("real"));
}
Expr::Mul { factors } => {
obj.insert("kind".into(), json!("mul"));
let kids: Result<Vec<_>, _> = factors.iter().map(|f| f.to_value()).collect();
obj.insert("factors".into(), Value::Array(kids?));
}
Expr::Add { terms } => {
obj.insert("kind".into(), json!("add"));
let kids: Result<Vec<_>, _> = terms.iter().map(|t| t.to_value()).collect();
obj.insert("terms".into(), Value::Array(kids?));
}
Expr::Sub { left, right } => {
obj.insert("kind".into(), json!("sub"));
obj.insert("left".into(), left.to_value()?);
obj.insert("right".into(), right.to_value()?);
}
Expr::Transpose { arg } => {
obj.insert("kind".into(), json!("transpose"));
obj.insert("arg".into(), arg.to_value()?);
}
Expr::Inv { arg } => {
obj.insert("kind".into(), json!("inv"));
obj.insert("arg".into(), arg.to_value()?);
}
}
Ok(Value::Object(obj))
}
pub fn from_value(v: &Value) -> Result<Self, String> {
let obj = v
.as_object()
.ok_or_else(|| "linalg_expr must be a map".to_string())?;
let kind = obj
.get("kind")
.and_then(|x| x.as_str())
.ok_or_else(|| "missing kind".to_string())?;
match kind {
"symbol" => Ok(Expr::Symbol {
name: obj
.get("name")
.and_then(|x| x.as_str())
.ok_or_else(|| "symbol missing name")?
.to_string(),
rows: Dim::from_json(obj.get("rows").ok_or("symbol missing rows")?)?,
cols: Dim::from_json(obj.get("cols").ok_or("symbol missing cols")?)?,
}),
"eye" => Ok(Expr::Eye {
n: Dim::from_json(obj.get("n").ok_or("eye missing n")?)?,
}),
"zero" => Ok(Expr::Zero {
rows: Dim::from_json(obj.get("rows").ok_or("zero missing rows")?)?,
cols: Dim::from_json(obj.get("cols").ok_or("zero missing cols")?)?,
}),
"dense" => {
let data = parse_dense(obj.get("data").ok_or("dense missing data")?)?;
Ok(Expr::Dense { data })
}
"mul" => {
let factors = obj
.get("factors")
.and_then(|x| x.as_array())
.ok_or("mul missing factors")?;
let factors: Result<Vec<_>, _> =
factors.iter().map(Expr::from_value).collect();
Ok(Expr::Mul {
factors: factors?,
})
}
"add" => {
let terms = obj
.get("terms")
.and_then(|x| x.as_array())
.ok_or("add missing terms")?;
let terms: Result<Vec<_>, _> = terms.iter().map(Expr::from_value).collect();
Ok(Expr::Add { terms: terms? })
}
"sub" => Ok(Expr::Sub {
left: Box::new(Expr::from_value(
obj.get("left").ok_or("sub missing left")?,
)?),
right: Box::new(Expr::from_value(
obj.get("right").ok_or("sub missing right")?,
)?),
}),
"transpose" => Ok(Expr::Transpose {
arg: Box::new(Expr::from_value(
obj.get("arg").ok_or("transpose missing arg")?,
)?),
}),
"inv" => Ok(Expr::Inv {
arg: Box::new(Expr::from_value(obj.get("arg").ok_or("inv missing arg")?)?),
}),
other => Err(format!("unknown linalg_expr kind `{other}`")),
}
}
}
fn dims_eq(a: &Dim, b: &Dim) -> bool {
match (a, b) {
(Dim::Int(x), Dim::Int(y)) => x == y,
(Dim::Sym(x), Dim::Sym(y)) => x == y,
// symbolic vs int: allow only if we cannot prove mismatch — treat as compatible
// for teaching formulas like n×n with concrete later; strict mode would reject.
(Dim::Int(_), Dim::Sym(_)) | (Dim::Sym(_), Dim::Int(_)) => true,
}
}
fn parse_dense(v: &Value) -> Result<Vec<Vec<f64>>, String> {
let rows = v
.as_array()
.ok_or_else(|| "dense data must be list of rows".to_string())?;
let mut out = Vec::new();
let mut width = None;
for row in rows {
let cells = row
.as_array()
.ok_or_else(|| "dense row must be a list".to_string())?;
let mut r = Vec::new();
for c in cells {
let n = c
.as_f64()
.or_else(|| c.as_i64().map(|i| i as f64))
.or_else(|| c.as_u64().map(|u| u as f64))
.or_else(|| c.as_str().and_then(|s| s.trim().parse().ok()))
.ok_or_else(|| "dense cell must be a number".to_string())?;
r.push(n);
}
if let Some(w) = width {
if r.len() != w {
return Err("dense matrix rows must have equal length".into());
}
} else {
width = Some(r.len());
}
out.push(r);
}
if out.is_empty() {
return Err("empty dense matrix".into());
}
Ok(out)
}
fn format_dense_ascii(data: &[Vec<f64>]) -> String {
let rows: Vec<String> = data
.iter()
.map(|row| {
let cells: Vec<String> = row.iter().map(|x| format_num(*x)).collect();
format!("[{}]", cells.join(","))
})
.collect();
format!("[{}]", rows.join(","))
}
fn format_dense_latex(data: &[Vec<f64>]) -> String {
let body: Vec<String> = data
.iter()
.map(|row| {
row.iter()
.map(|x| format_num(*x))
.collect::<Vec<_>>()
.join(" & ")
})
.collect();
format!(
"\\begin{{bmatrix}}{}\\end{{bmatrix}}",
body.join(" \\\\ ")
)
}
fn format_num(n: f64) -> String {
if n.fract() == 0.0 && n.abs() < 1e15 {
format!("{}", n as i64)
} else {
format!("{n}")
}
}
/// Flatten nested Mul.
fn flatten_mul(factors: Vec<Expr>) -> Vec<Expr> {
let mut out = Vec::new();
for f in factors {
match f {
Expr::Mul { factors: inner } => out.extend(flatten_mul(inner)),
other => out.push(other),
}
}
out
}
fn is_eye(e: &Expr) -> bool {
matches!(e, Expr::Eye { .. })
}
fn is_zero(e: &Expr) -> bool {
matches!(e, Expr::Zero { .. })
}
/// Apply R1–R5 once bottom-up, then iterate to fixpoint (small bound).
pub fn simplify(expr: Expr) -> Result<Expr, String> {
let mut cur = expr;
for _ in 0..32 {
let next = simplify_once(cur.clone())?;
// shape check keeps trees valid
let _ = next.shape()?;
if next == cur {
return Ok(cur);
}
cur = next;
}
Ok(cur)
}
fn simplify_once(expr: Expr) -> Result<Expr, String> {
match expr {
Expr::Mul { factors } => {
let factors: Result<Vec<_>, _> =
flatten_mul(factors).into_iter().map(simplify_once).collect();
let mut factors = factors?;
let eye_n = factors.iter().find_map(|f| match f {
Expr::Eye { n } => Some(n.clone()),
_ => None,
});
// R1: drop Eye
factors.retain(|f| !is_eye(f));
if factors.is_empty() {
return Ok(Expr::Eye {
n: eye_n.unwrap_or(Dim::Sym("n".into())),
});
}
if factors.len() == 1 {
return Ok(factors.pop().unwrap());
}
// R1 zero absorb: …*0*… → 0 with product shape
if let Some(z) = factors.iter().find(|f| is_zero(f)) {
let (r, _) = factors.first().unwrap().shape()?;
let (_, c) = factors.last().unwrap().shape()?;
let _ = z;
return Ok(Expr::Zero { rows: r, cols: c });
}
Ok(Expr::Mul { factors })
}
Expr::Add { terms } => {
let terms: Result<Vec<_>, _> = terms.into_iter().map(simplify_once).collect();
let mut terms = terms?;
terms.retain(|t| !is_zero(t));
if terms.is_empty() {
return Ok(Expr::Zero {
rows: Dim::Int(0),
cols: Dim::Int(0),
});
}
if terms.len() == 1 {
return Ok(terms.pop().unwrap());
}
Ok(Expr::Add { terms })
}
Expr::Sub { left, right } => {
let left = simplify_once(*left)?;
let right = simplify_once(*right)?;
if is_zero(&right) {
return Ok(left);
}
Ok(Expr::Sub {
left: Box::new(left),
right: Box::new(right),
})
}
Expr::Transpose { arg } => {
let arg = simplify_once(*arg)?;
match arg {
// R3: (AB…)^T → …^T B^T A^T
Expr::Mul { factors } => {
let factors: Vec<Expr> = factors
.into_iter()
.rev()
.map(|f| Expr::Transpose {
arg: Box::new(f),
})
.collect();
simplify_once(Expr::Mul { factors })
}
// R3: (A^T)^T → A
Expr::Transpose { arg: inner } => Ok(*inner),
// (A+B)^T → A^T+B^T
Expr::Add { terms } => {
let terms = terms
.into_iter()
.map(|t| Expr::Transpose {
arg: Box::new(t),
})
.collect();
simplify_once(Expr::Add { terms })
}
Expr::Sub { left, right } => simplify_once(Expr::Sub {
left: Box::new(Expr::Transpose { arg: left }),
right: Box::new(Expr::Transpose { arg: right }),
}),
// R4 partial: (A^-1)^T → (A^T)^-1
Expr::Inv { arg: inner } => Ok(Expr::Inv {
arg: Box::new(Expr::Transpose { arg: inner }),
}),
Expr::Eye { n } => Ok(Expr::Eye { n }),
Expr::Zero { rows, cols } => Ok(Expr::Zero {
rows: cols,
cols: rows,
}),
other => Ok(Expr::Transpose {
arg: Box::new(other),
}),
}
}
Expr::Inv { arg } => {
let arg = simplify_once(*arg)?;
match arg {
// R2: (A^-1)^-1 → A
Expr::Inv { arg: inner } => Ok(*inner),
// R4: (A^T)^-1 stays; (A^-1)^T handled above
Expr::Eye { n } => Ok(Expr::Eye { n }),
other => Ok(Expr::Inv {
arg: Box::new(other),
}),
}
}
Expr::Symbol { .. } | Expr::Eye { .. } | Expr::Zero { .. } | Expr::Dense { .. } => {
Ok(expr)
}
}
}
pub fn mul(a: Expr, b: Expr) -> Result<Expr, String> {
let expr = Expr::Mul {
factors: flatten_mul(vec![a, b]),
};
let _ = expr.shape()?;
Ok(expr)
}
pub fn add(a: Expr, b: Expr) -> Result<Expr, String> {
let expr = Expr::Add {
terms: vec![a, b],
};
let _ = expr.shape()?;
Ok(expr)
}
pub fn sub(a: Expr, b: Expr) -> Result<Expr, String> {
let expr = Expr::Sub {
left: Box::new(a),
right: Box::new(b),
};
let _ = expr.shape()?;
Ok(expr)
}
pub fn transpose(a: Expr) -> Result<Expr, String> {
let expr = Expr::Transpose {
arg: Box::new(a),
};
let _ = expr.shape()?;
Ok(expr)
}
pub fn inv(a: Expr) -> Result<Expr, String> {
let expr = Expr::Inv {
arg: Box::new(a),
};
let _ = expr.shape()?;
Ok(expr)
}
pub fn symbol(name: &str, rows: Dim, cols: Dim) -> Expr {
Expr::Symbol {
name: name.to_string(),
rows,
cols,
}
}
pub fn eye(n: Dim) -> Expr {
Expr::Eye { n }
}
pub fn zeros(rows: Dim, cols: Dim) -> Expr {
Expr::Zero { rows, cols }
}
pub fn from_list(data: Vec<Vec<f64>>) -> Result<Expr, String> {
if data.is_empty() {
return Err("empty matrix".into());
}
let w = data[0].len();
if w == 0 || data.iter().any(|r| r.len() != w) {
return Err("dense matrix rows must have equal non-zero length".into());
}
Ok(Expr::Dense { data })
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn transpose_of_product() {
let a = symbol("A", Dim::Int(2), Dim::Int(3));
let b = symbol("B", Dim::Int(3), Dim::Int(2));
let p = mul(a, b).unwrap();
let t = transpose(p).unwrap();
let s = simplify(t).unwrap();
assert_eq!(s.ascii(), "B^T*A^T");
}
#[test]
fn double_inv() {
let a = symbol("A", Dim::Sym("n".into()), Dim::Sym("n".into()));
let i = inv(inv(a).unwrap()).unwrap();
let s = simplify(i).unwrap();
assert_eq!(s.ascii(), "A");
}
#[test]
fn shape_mismatch() {
let a = symbol("A", Dim::Int(2), Dim::Int(2));
let b = symbol("B", Dim::Int(3), Dim::Int(3));
assert!(mul(a, b).is_err());
}
}