feat(linalg): matrix infix ops and # matrix methods

Dispatch +/−/* to the linalg plugin for matrix values, retag for EN/ZH
method chains, and expand # matrix with T/simplify/ascii-style methods.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-09-07 15:13:47 +08:00
1 parent c0fbe9b182
commit 51bb8767e3
16 files changed
+459 -57

No files matched your search

+2 -1
View File
@@ -171,7 +171,8 @@ pub fn to_dense_value(m: &CMat) -> Result<Value, String> {
})
.collect();
Ok(json!({
"_type": "linalg_dense",
"_type": "matrix",
"kind": "dense",
"rows": rows,
"cols": cols,
"data": data,
+3 -2
View File
@@ -47,7 +47,8 @@ pub fn to_dense_value(m: &Mat) -> Result<Value, String> {
let (rows, cols) = shape(m)?;
check_elems(rows, cols)?;
Ok(json!({
"_type": "linalg_dense",
"_type": "matrix",
"kind": "dense",
"rows": rows,
"cols": cols,
"data": m,
@@ -108,7 +109,7 @@ pub fn from_value(v: &Value) -> Result<Mat, String> {
if v.is_array() {
return parse_data(v);
}
Err("expected linalg_dense, dense expr, or nested list".into())
Err("expected matrix dense, dense expr, or nested list".into())
}
fn parse_data(v: &Value) -> Result<Mat, String> {
+8 -2
View File
@@ -349,7 +349,7 @@ impl Expr {
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("_type".into(), json!("matrix"));
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()));
@@ -421,7 +421,13 @@ impl Expr {
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())?;
.ok_or_else(|| "matrix expr must be a map".to_string())?;
// Accept legacy `linalg_expr` and author types `matrix` / `矩阵`.
if let Some(t) = obj.get("_type").and_then(|x| x.as_str()) {
if !matches!(t, "matrix" | "矩阵" | "linalg_expr" | "linalg_dense") {
// still allow untyped maps that have `kind`
}
}
let kind = obj
.get("kind")
.and_then(|x| x.as_str())