feat(linalg): L6 lstsq, norms, complex dtype, draw themes

Add QR/SVD least squares, norm/cond/rank, complex dense Frobenius
baseline, and light/dark/bw draw themes aligned with quantum Q8.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-09-07 14:47:30 +08:00
1 parent 01287d9e7e
commit c0fbe9b182
15 files changed
+910 -136

No files matched your search

+211 -85
View File
@@ -1,20 +1,108 @@
//! Structure SVG for factorizations (teaching layouts).
//! Structure SVG for factorizations (teaching layouts) with Q8-aligned themes.
use serde_json::Value;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ThemeName {
Dark,
Light,
Bw,
}
impl ThemeName {
pub fn parse(raw: &str) -> Result<Self, String> {
match raw.trim().to_ascii_lowercase().as_str() {
"dark" | "暗" => Ok(Self::Dark),
"light" | "亮" | "浅" => Ok(Self::Light),
"bw" | "mono" | "黑白" => Ok(Self::Bw),
other => Err(format!("unknown theme `{other}` (dark|light|bw)")),
}
}
pub fn as_str(self) -> &'static str {
match self {
Self::Dark => "dark",
Self::Light => "light",
Self::Bw => "bw",
}
}
}
struct Theme {
name: ThemeName,
bg: &'static str,
ink: &'static str,
muted: &'static str,
stroke: &'static str,
box_a: &'static str,
box_b: &'static str,
box_c: &'static str,
heat_neg: &'static str,
heat_pos: &'static str,
}
impl Theme {
fn of(name: ThemeName) -> Self {
match name {
ThemeName::Dark => Self {
name,
bg: "#0a0e14",
ink: "#e8eef6",
muted: "#93a4b8",
stroke: "#6b7f96",
box_a: "#1e3a5f",
box_b: "#3d2e14",
box_c: "#1e3f2e",
heat_neg: "#cc5555",
heat_pos: "#4c8fff",
},
ThemeName::Light => Self {
name,
bg: "#eef3f9",
ink: "#15202b",
muted: "#5a6a7a",
stroke: "#4a5a6a",
box_a: "#e8f1ff",
box_b: "#fff3d6",
box_c: "#e8ffe8",
heat_neg: "#cc3333",
heat_pos: "#2266aa",
},
ThemeName::Bw => Self {
name,
bg: "#ffffff",
ink: "#111111",
muted: "#555555",
stroke: "#222222",
box_a: "#f0f0f0",
box_b: "#e0e0e0",
box_c: "#d0d0d0",
heat_neg: "#000000",
heat_pos: "#666666",
},
}
}
}
/// Draw a factor object or matrix as structure SVG.
pub fn draw_factor(factor: &Value, kind: Option<&str>, path: Option<&str>) -> Result<String, String> {
pub fn draw_factor(
factor: &Value,
kind: Option<&str>,
path: Option<&str>,
theme: ThemeName,
) -> Result<String, String> {
let kind = kind
.or_else(|| factor.get("kind").and_then(|k| k.as_str()))
.unwrap_or("structure");
let th = Theme::of(theme);
let svg = match kind {
"eig" => draw_eig(factor),
"svd" => draw_svd(factor),
"qr" => draw_qr(factor),
"lu" | "ge" => draw_lu(factor),
"chol" => draw_chol(factor),
"structure" => draw_structure(factor),
"heatmap" | "hinton" => draw_heatmap(factor, kind == "hinton"),
"eig" => draw_eig(factor, &th),
"svd" => draw_svd(factor, &th),
"qr" => draw_qr(factor, &th),
"lu" | "ge" => draw_lu(factor, &th),
"chol" => draw_chol(factor, &th),
"structure" => draw_structure(factor, &th),
"heatmap" | "hinton" => draw_heatmap(factor, kind == "hinton", &th),
other => {
return Err(format!(
"unknown draw kind `{other}` (want eig|svd|qr|lu|ge|chol|structure|heatmap|hinton)"
@@ -31,17 +119,29 @@ fn escape(s: &str) -> String {
.replace('>', "&gt;")
}
fn box_svg(x: f64, y: f64, w: f64, h: f64, label: &str, fill: &str) -> String {
fn box_svg(x: f64, y: f64, w: f64, h: f64, label: &str, fill: &str, th: &Theme) -> String {
format!(
"<rect x=\"{x}\" y=\"{y}\" width=\"{w}\" height=\"{h}\" fill=\"{fill}\" stroke=\"#333\" stroke-width=\"1.5\" rx=\"4\"/>\
<text x=\"{}\" y=\"{}\" text-anchor=\"middle\" dominant-baseline=\"middle\" font-size=\"16\" font-family=\"serif\">{}</text>",
"<rect x=\"{x}\" y=\"{y}\" width=\"{w}\" height=\"{h}\" fill=\"{fill}\" stroke=\"{}\" stroke-width=\"1.5\" rx=\"4\"/>\
<text x=\"{}\" y=\"{}\" text-anchor=\"middle\" dominant-baseline=\"middle\" font-size=\"16\" font-family=\"serif\" fill=\"{}\">{}</text>",
th.stroke,
x + w / 2.0,
y + h / 2.0,
th.ink,
escape(label)
)
}
fn draw_eig(factor: &Value) -> String {
fn root(w: f64, h: f64, kind: &str, th: &Theme, body: &str) -> String {
format!(
"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"{w}\" height=\"{h}\" data-linalg=\"{kind}\" data-theme=\"{}\" style=\"background:{}\">\
{body}\
</svg>",
th.name.as_str(),
th.bg
)
}
fn draw_eig(factor: &Value, th: &Theme) -> String {
let latex = factor
.get("latex")
.and_then(|x| x.as_str())
@@ -57,23 +157,24 @@ fn draw_eig(factor: &Value) -> String {
.join(", ")
})
.unwrap_or_default();
format!(
"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"520\" height=\"140\" data-linalg=\"eig\">\
<text x=\"16\" y=\"28\" font-size=\"18\" font-family=\"serif\">{}</text>\
let body = format!(
"<text x=\"16\" y=\"28\" font-size=\"18\" font-family=\"serif\" fill=\"{}\">{}</text>\
{}\
{}\
{}\
<text x=\"16\" y=\"120\" font-size=\"13\" font-family=\"sans-serif\" fill=\"#444\">λ = [{}]</text>\
</svg>",
<text x=\"16\" y=\"120\" font-size=\"13\" font-family=\"sans-serif\" fill=\"{}\">λ = [{}]</text>",
th.ink,
escape(latex),
box_svg(40.0, 48.0, 70.0, 50.0, "P", "#e8f1ff"),
box_svg(130.0, 48.0, 70.0, 50.0, "D", "#fff3d6"),
box_svg(220.0, 48.0, 90.0, 50.0, "P⁻¹", "#e8f1ff"),
box_svg(40.0, 48.0, 70.0, 50.0, "P", th.box_a, th),
box_svg(130.0, 48.0, 70.0, 50.0, "D", th.box_b, th),
box_svg(220.0, 48.0, 90.0, 50.0, "P⁻¹", th.box_a, th),
th.muted,
escape(&evals)
)
);
root(520.0, 140.0, "eig", th, &body)
}
fn draw_svd(factor: &Value) -> String {
fn draw_svd(factor: &Value, th: &Theme) -> String {
let s = factor
.get("S")
.and_then(|x| x.as_array())
@@ -85,61 +186,59 @@ fn draw_svd(factor: &Value) -> String {
.join(", ")
})
.unwrap_or_default();
format!(
"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"560\" height=\"140\" data-linalg=\"svd\">\
<text x=\"16\" y=\"28\" font-size=\"18\" font-family=\"serif\">A = U Σ V⊤</text>\
let body = format!(
"<text x=\"16\" y=\"28\" font-size=\"18\" font-family=\"serif\" fill=\"{}\">A = U Σ V⊤</text>\
{}\
{}\
{}\
<text x=\"16\" y=\"120\" font-size=\"13\" font-family=\"sans-serif\" fill=\"#444\">σ = [{}]</text>\
</svg>",
box_svg(40.0, 48.0, 70.0, 50.0, "U", "#e8f1ff"),
box_svg(130.0, 48.0, 70.0, 50.0, "Σ", "#fff3d6"),
box_svg(220.0, 48.0, 80.0, 50.0, "V⊤", "#e8f1ff"),
<text x=\"16\" y=\"120\" font-size=\"13\" font-family=\"sans-serif\" fill=\"{}\">σ = [{}]</text>",
th.ink,
box_svg(40.0, 48.0, 70.0, 50.0, "U", th.box_a, th),
box_svg(130.0, 48.0, 70.0, 50.0, "Σ", th.box_b, th),
box_svg(220.0, 48.0, 80.0, 50.0, "V⊤", th.box_a, th),
th.muted,
escape(&s)
)
);
root(560.0, 140.0, "svd", th, &body)
}
fn draw_qr(factor: &Value) -> String {
let _ = factor;
format!(
"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"420\" height=\"120\" data-linalg=\"qr\">\
<text x=\"16\" y=\"28\" font-size=\"18\" font-family=\"serif\">A = Q R</text>\
fn draw_qr(_factor: &Value, th: &Theme) -> String {
let body = format!(
"<text x=\"16\" y=\"28\" font-size=\"18\" font-family=\"serif\" fill=\"{}\">A = Q R</text>\
{}\
{}\
</svg>",
box_svg(40.0, 48.0, 70.0, 50.0, "Q", "#e8f1ff"),
box_svg(130.0, 48.0, 70.0, 50.0, "R", "#ffe8e8"),
)
{}",
th.ink,
box_svg(40.0, 48.0, 70.0, 50.0, "Q", th.box_a, th),
box_svg(130.0, 48.0, 70.0, 50.0, "R", th.box_b, th),
);
root(420.0, 120.0, "qr", th, &body)
}
fn draw_lu(factor: &Value) -> String {
let _ = factor;
format!(
"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"420\" height=\"120\" data-linalg=\"lu\">\
<text x=\"16\" y=\"28\" font-size=\"18\" font-family=\"serif\">A = L U</text>\
fn draw_lu(_factor: &Value, th: &Theme) -> String {
let body = format!(
"<text x=\"16\" y=\"28\" font-size=\"18\" font-family=\"serif\" fill=\"{}\">A = L U</text>\
{}\
{}\
</svg>",
box_svg(40.0, 48.0, 70.0, 50.0, "L", "#e8ffe8"),
box_svg(130.0, 48.0, 70.0, 50.0, "U", "#ffe8e8"),
)
{}",
th.ink,
box_svg(40.0, 48.0, 70.0, 50.0, "L", th.box_c, th),
box_svg(130.0, 48.0, 70.0, 50.0, "U", th.box_b, th),
);
root(420.0, 120.0, "lu", th, &body)
}
fn draw_chol(factor: &Value) -> String {
let _ = factor;
format!(
"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"420\" height=\"120\" data-linalg=\"chol\">\
<text x=\"16\" y=\"28\" font-size=\"18\" font-family=\"serif\">A = L L⊤</text>\
fn draw_chol(_factor: &Value, th: &Theme) -> String {
let body = format!(
"<text x=\"16\" y=\"28\" font-size=\"18\" font-family=\"serif\" fill=\"{}\">A = L L⊤</text>\
{}\
{}\
</svg>",
box_svg(40.0, 48.0, 70.0, 50.0, "L", "#e8ffe8"),
box_svg(130.0, 48.0, 80.0, 50.0, "L⊤", "#e8ffe8"),
)
{}",
th.ink,
box_svg(40.0, 48.0, 70.0, 50.0, "L", th.box_c, th),
box_svg(130.0, 48.0, 80.0, 50.0, "L⊤", th.box_c, th),
);
root(420.0, 120.0, "chol", th, &body)
}
fn draw_structure(factor: &Value) -> String {
fn draw_structure(factor: &Value, th: &Theme) -> String {
let label = if let (Some(r), Some(c)) = (factor.get("rows"), factor.get("cols")) {
format!("{}×{}", r, c)
} else if let Some(Value::Array(a)) = factor.get("shape") {
@@ -159,25 +258,17 @@ fn draw_structure(factor: &Value) -> String {
.unwrap_or("matrix")
.to_string()
};
format!(
"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"280\" height=\"120\" data-linalg=\"structure\">\
{}\
</svg>",
box_svg(40.0, 30.0, 160.0, 60.0, &label, "#f4f4f4")
)
let body = box_svg(40.0, 30.0, 160.0, 60.0, &label, th.box_a, th);
root(280.0, 120.0, "structure", th, &body)
}
fn draw_heatmap(factor: &Value, hinton: bool) -> String {
fn draw_heatmap(factor: &Value, hinton: bool, th: &Theme) -> String {
let data = factor
.get("data")
.or_else(|| {
factor
.get("eigenvectors")
.and_then(|v| v.get("data"))
})
.or_else(|| factor.get("eigenvectors").and_then(|v| v.get("data")))
.and_then(|d| d.as_array());
let Some(rows) = data else {
return draw_structure(factor);
return draw_structure(factor, th);
};
let n = rows.len().min(16);
let m = rows
@@ -187,34 +278,35 @@ fn draw_heatmap(factor: &Value, hinton: bool) -> String {
.unwrap_or(0)
.min(16);
if n == 0 || m == 0 {
return draw_structure(factor);
return draw_structure(factor, th);
}
let mut max_abs: f64 = 1e-12;
for row in rows.iter().take(n) {
if let Some(cells) = row.as_array() {
for c in cells.iter().take(m) {
if let Some(v) = c.as_f64() {
max_abs = max_abs.max(v.abs());
}
let v = cell_mag(c);
max_abs = max_abs.max(v);
}
}
}
let cell = 18.0;
let w = 40.0 + m as f64 * cell;
let h = 40.0 + n as f64 * cell;
let tag = if hinton { "hinton" } else { "heatmap" };
let mut parts = vec![format!(
"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"{w}\" height=\"{h}\" data-linalg=\"{}\">",
if hinton { "hinton" } else { "heatmap" }
"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"{w}\" height=\"{h}\" data-linalg=\"{tag}\" data-theme=\"{}\" style=\"background:{}\">",
th.name.as_str(),
th.bg
)];
for (i, row) in rows.iter().take(n).enumerate() {
let Some(cells) = row.as_array() else { continue };
for (j, c) in cells.iter().take(m).enumerate() {
let v = c.as_f64().unwrap_or(0.0);
let v = cell_signed(c);
let x = 20.0 + j as f64 * cell;
let y = 20.0 + i as f64 * cell;
if hinton {
let s = (v.abs() / max_abs).sqrt() * (cell - 4.0);
let fill = if v >= 0.0 { "#2266aa" } else { "#cc3333" };
let fill = if v >= 0.0 { th.heat_pos } else { th.heat_neg };
parts.push(format!(
"<rect x=\"{}\" y=\"{}\" width=\"{s}\" height=\"{s}\" fill=\"{fill}\"/>",
x + (cell - s) / 2.0,
@@ -224,8 +316,15 @@ fn draw_heatmap(factor: &Value, hinton: bool) -> String {
let t = ((v / max_abs) + 1.0) * 0.5;
let r = (255.0 * (1.0 - t)) as u8;
let b = (255.0 * t) as u8;
let fill = if th.name == ThemeName::Bw {
let g = (255.0 * (1.0 - v.abs() / max_abs)) as u8;
format!("rgb({g},{g},{g})")
} else {
format!("rgb({r},80,{b})")
};
parts.push(format!(
"<rect x=\"{x}\" y=\"{y}\" width=\"{cell}\" height=\"{cell}\" fill=\"rgb({r},80,{b})\" stroke=\"#fff\" stroke-width=\"0.5\"/>"
"<rect x=\"{x}\" y=\"{y}\" width=\"{cell}\" height=\"{cell}\" fill=\"{fill}\" stroke=\"{}\" stroke-width=\"0.5\"/>",
th.bg
));
}
}
@@ -233,3 +332,30 @@ fn draw_heatmap(factor: &Value, hinton: bool) -> String {
parts.push("</svg>".into());
parts.join("")
}
fn cell_mag(c: &Value) -> f64 {
if let Some(v) = c.as_f64() {
return v.abs();
}
if let Some(a) = c.as_array() {
if a.len() >= 2 {
let re = a[0].as_f64().unwrap_or(0.0);
let im = a[1].as_f64().unwrap_or(0.0);
return (re * re + im * im).sqrt();
}
}
0.0
}
fn cell_signed(c: &Value) -> f64 {
if let Some(v) = c.as_f64() {
return v;
}
// complex: use real part for signed heatmap, magnitude for abs
if let Some(a) = c.as_array() {
if a.len() >= 2 {
return a[0].as_f64().unwrap_or(0.0);
}
}
0.0
}