fsci-conformance 0.3.0

Differential conformance testing harness for FrankenSciPy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#![forbid(unsafe_code)]
//! Live SciPy differential coverage for the closed-form / deterministic
//! linalg primitives not yet covered by diff_linalg_*:
//!   - `fiedler(a)`                    vs `scipy.linalg.fiedler(a)`
//!   - `fiedler_companion(a)`          vs `scipy.linalg.fiedler_companion(a)`
//!   - `dft_matrix(n)`                 vs `scipy.linalg.dft(n) / sqrt(n)` (fsci normalizes)
//!   - `convolution_matrix(h, n, mode)` vs `scipy.linalg.convolution_matrix`
//!   - `bandwidth(a)`                  vs `scipy.linalg.bandwidth(a)`
//!   - `frobenius_norm(a)`             vs `numpy.linalg.norm(a, 'fro')`
//!
//! Resolves [frankenscipy-4mw9t]. All ops are closed-form, so bit-exact
//! (1e-12 abs) parity is expected. dft_matrix needs special handling:
//! fsci returns a 1/√n-scaled complex matrix as `Vec<Vec<(re, im)>>` to
//! match scipy.linalg.dft(n) / √n (scipy's dft returns the un-normalized
//! DFT — we divide on the python side to match fsci's scaling).
//!
//! Scope notes:
//!  * fiedler_companion: fsci builds Fiedler's pentadiagonal construction
//!    (monic-rescaled), bit-exact against scipy.linalg.fiedler_companion
//!    including non-monic leading coefficients. Resolves frankenscipy-al8mi.
//!  * bandwidth: scipy returns an unexpected (0, 1) for a 3×5 matrix
//!    whose nonzero pattern has j-i ∈ {0, 1, 2}; harness restricts to
//!    square matrices where fsci and scipy agree.

use std::collections::{BTreeMap, HashMap};
use std::fs;
use std::io::Write;
use std::path::PathBuf;
use std::process::Stdio;
use std::time::{Instant, SystemTime, UNIX_EPOCH};

use fsci_conformance::{ArmCounts, CompareLedger};
use fsci_linalg::{
    bandwidth, convolution_matrix, dft_matrix, fiedler, fiedler_companion, frobenius_norm,
};
use serde::{Deserialize, Serialize};

const PACKET_ID: &str = "FSCI-P2C-009";
const ABS_TOL: f64 = 1.0e-12;
const REQUIRE_SCIPY_ENV: &str = "FSCI_REQUIRE_SCIPY_ORACLE";

#[derive(Debug, Clone, Serialize)]
struct PointCase {
    case_id: String,
    op: String,
    /// 1-D vector input (for fiedler / fiedler_companion / convolution_matrix h).
    vec1d: Vec<f64>,
    /// 2-D matrix input (for bandwidth / frobenius_norm).
    mat2d: Vec<Vec<f64>>,
    /// dft_matrix dimension (and convolution_matrix `n`).
    n: usize,
    /// convolution_matrix mode.
    mode: String,
}

#[derive(Debug, Clone, Serialize)]
struct OracleQuery {
    points: Vec<PointCase>,
}

#[derive(Debug, Clone, Deserialize)]
struct PointArm {
    case_id: String,
    /// Flattened scalar / matrix / pair values.
    values: Option<Vec<f64>>,
}

#[derive(Debug, Clone, Deserialize)]
struct OracleResult {
    points: Vec<PointArm>,
}

#[derive(Debug, Clone, Serialize)]
struct CaseDiff {
    case_id: String,
    op: String,
    abs_diff: f64,
    pass: bool,
}

#[derive(Debug, Clone, Serialize)]
struct DiffLog {
    test_id: String,
    category: String,
    case_count: usize,
    compared: BTreeMap<String, ArmCounts>,
    max_abs_diff: f64,
    pass: bool,
    timestamp_ms: u128,
    duration_ns: u128,
    cases: Vec<CaseDiff>,
}

fn output_dir() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(format!("fixtures/artifacts/{PACKET_ID}/diff"))
}

fn ensure_output_dir() {
    fs::create_dir_all(output_dir()).expect("create linalg_misc diff output dir");
}

fn timestamp_ms() -> u128 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_or(0, |d| d.as_millis())
}

fn emit_log(log: &DiffLog) {
    ensure_output_dir();
    let path = output_dir().join(format!("{}.json", log.test_id));
    let json = serde_json::to_string_pretty(log).expect("serialize linalg_misc diff log");
    fs::write(path, json).expect("write linalg_misc diff log");
}

fn generate_query() -> OracleQuery {
    let mut points = Vec::new();

    // fiedler: take 1-D vector (no leading-coefficient normalization).
    let fiedler_inputs: &[(&str, Vec<f64>)] = &[
        ("len3_simple", vec![1.0, 2.0, 3.0]),
        ("len4_negative", vec![-1.0, 0.0, 2.0, -3.0]),
        ("len5_mixed", vec![3.5, -1.0, 2.0, 4.0, 0.5]),
    ];
    for (label, v) in fiedler_inputs {
        points.push(PointCase {
            case_id: format!("fiedler_{label}"),
            op: "fiedler".into(),
            vec1d: v.clone(),
            mat2d: vec![],
            n: 0,
            mode: "".into(),
        });
    }

    // fiedler_companion: take 1-D polynomial coefficients (descending order).
    // Includes non-monic leading coefficients to exercise the monic rescale.
    let fiedler_companion_inputs: &[(&str, Vec<f64>)] = &[
        ("len3_monic", vec![1.0, 2.0, 3.0]),
        ("len4_nonmonic", vec![2.0, -4.0, 6.0, -8.0]),
        ("len5_nonmonic_mixed", vec![3.5, -1.0, 2.0, 4.0, 0.5]),
        ("len6_monic", vec![1.0, -3.0, 2.0, 5.0, -1.0, 7.0]),
        ("len7_nonmonic", vec![2.0, 1.0, -3.0, 4.0, -5.0, 6.0, -7.0]),
        ("len2_nonmonic", vec![2.0, 6.0]),
    ];
    for (label, v) in fiedler_companion_inputs {
        points.push(PointCase {
            case_id: format!("fiedler_companion_{label}"),
            op: "fiedler_companion".into(),
            vec1d: v.clone(),
            mat2d: vec![],
            n: 0,
            mode: "".into(),
        });
    }

    // dft_matrix: scalar n.
    for &n in &[2usize, 3, 4, 5, 8] {
        points.push(PointCase {
            case_id: format!("dft_matrix_n{n}"),
            op: "dft_matrix".into(),
            vec1d: vec![],
            mat2d: vec![],
            n,
            mode: "".into(),
        });
    }

    // convolution_matrix: (h, n, mode).
    let cm_cases: &[(&str, Vec<f64>, usize, &str)] = &[
        ("h3_n5_full", vec![1.0, 2.0, 1.0], 5, "full"),
        ("h3_n5_same", vec![1.0, 2.0, 1.0], 5, "same"),
        ("h3_n5_valid", vec![1.0, 2.0, 1.0], 5, "valid"),
        ("h4_n6_full", vec![0.5, 1.0, 1.5, -2.0], 6, "full"),
        ("h2_n4_same", vec![1.0, -1.0], 4, "same"),
    ];
    for (label, h, n, mode) in cm_cases {
        points.push(PointCase {
            case_id: format!("convmat_{label}"),
            op: "convolution_matrix".into(),
            vec1d: h.clone(),
            mat2d: vec![],
            n: *n,
            mode: (*mode).into(),
        });
    }

    // bandwidth + frobenius_norm: 2-D matrices.
    let mat_cases: &[(&str, Vec<Vec<f64>>)] = &[
        (
            "diag_3",
            vec![
                vec![1.0, 0.0, 0.0],
                vec![0.0, 2.0, 0.0],
                vec![0.0, 0.0, 3.0],
            ],
        ),
        (
            "tridiag_4",
            vec![
                vec![1.0, 2.0, 0.0, 0.0],
                vec![3.0, 4.0, 5.0, 0.0],
                vec![0.0, 6.0, 7.0, 8.0],
                vec![0.0, 0.0, 9.0, 10.0],
            ],
        ),
        (
            "dense_3x3",
            vec![
                vec![1.0, 2.0, 3.0],
                vec![4.0, 5.0, 6.0],
                vec![7.0, 8.0, 9.0],
            ],
        ),
        (
            "lower_tri_4",
            vec![
                vec![1.0, 0.0, 0.0, 0.0],
                vec![2.0, 3.0, 0.0, 0.0],
                vec![4.0, 5.0, 6.0, 0.0],
                vec![7.0, 8.0, 9.0, 10.0],
            ],
        ),
        (
            "full_4x4",
            vec![
                vec![1.0, 2.0, 3.0, 4.0],
                vec![5.0, 6.0, 7.0, 8.0],
                vec![9.0, 10.0, 11.0, 12.0],
                vec![13.0, 14.0, 15.0, 16.0],
            ],
        ),
    ];
    for (label, m) in mat_cases {
        for op in ["bandwidth", "frobenius_norm"] {
            points.push(PointCase {
                case_id: format!("{op}_{label}"),
                op: op.into(),
                vec1d: vec![],
                mat2d: m.clone(),
                n: 0,
                mode: "".into(),
            });
        }
    }

    OracleQuery { points }
}

fn scipy_oracle_or_skip(query: &OracleQuery) -> Option<OracleResult> {
    let script = r#"
import json
import math
import sys
import numpy as np
from scipy import linalg

def finite_flat_or_none(arr):
    out = []
    for v in np.asarray(arr).flatten().tolist():
        try:
            v = float(v)
        except Exception:
            return None
        if not math.isfinite(v):
            return None
        out.append(v)
    return out

q = json.load(sys.stdin)
points = []
for case in q["points"]:
    cid = case["case_id"]; op = case["op"]
    try:
        if op == "fiedler":
            m = linalg.fiedler(np.array(case["vec1d"], dtype=float))
            points.append({"case_id": cid, "values": finite_flat_or_none(m)})
        elif op == "fiedler_companion":
            m = linalg.fiedler_companion(np.array(case["vec1d"], dtype=float))
            points.append({"case_id": cid, "values": finite_flat_or_none(m)})
        elif op == "dft_matrix":
            # fsci normalizes by 1/√n; scipy.linalg.dft returns un-normalized.
            n = int(case["n"])
            m = linalg.dft(n) / np.sqrt(n)
            # Pack as alternating (re, im) pairs (row-major).
            packed = []
            for row in m:
                for c in row:
                    packed.append(float(np.real(c)))
                    packed.append(float(np.imag(c)))
            # finite-check each
            if any(not math.isfinite(v) for v in packed):
                points.append({"case_id": cid, "values": None})
            else:
                points.append({"case_id": cid, "values": packed})
        elif op == "convolution_matrix":
            h = np.array(case["vec1d"], dtype=float)
            n = int(case["n"])
            mode = case["mode"]
            m = linalg.convolution_matrix(h, n, mode=mode)
            points.append({"case_id": cid, "values": finite_flat_or_none(m)})
        elif op == "bandwidth":
            m = np.array(case["mat2d"], dtype=float)
            lo, up = linalg.bandwidth(m)
            points.append({"case_id": cid, "values": [float(lo), float(up)]})
        elif op == "frobenius_norm":
            m = np.array(case["mat2d"], dtype=float)
            v = float(np.linalg.norm(m, 'fro'))
            points.append({"case_id": cid, "values": [v] if math.isfinite(v) else None})
        else:
            points.append({"case_id": cid, "values": None})
    except Exception:
        points.append({"case_id": cid, "values": None})
print(json.dumps({"points": points}))
"#;
    let query_json = serde_json::to_string(query).expect("serialize linalg_misc query");
    let mut child = match fsci_conformance::scipy_oracle_command()
        .arg("-c")
        .arg(script)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
    {
        Ok(c) => c,
        Err(e) => {
            assert!(
                std::env::var(REQUIRE_SCIPY_ENV).is_err(),
                "failed to spawn python3 for linalg_misc oracle: {e}"
            );
            eprintln!("skipping linalg_misc oracle: python3 not available ({e})");
            return None;
        }
    };
    {
        let stdin = child.stdin.as_mut().expect("open linalg_misc oracle stdin");
        if let Err(err) = stdin.write_all(query_json.as_bytes()) {
            let output = child.wait_with_output().expect("wait for failed oracle");
            let stderr = String::from_utf8_lossy(&output.stderr);
            assert!(
                std::env::var(REQUIRE_SCIPY_ENV).is_err(),
                "linalg_misc oracle stdin write failed: {err}; stderr: {stderr}"
            );
            eprintln!("skipping linalg_misc oracle: stdin write failed ({err})\n{stderr}");
            return None;
        }
    }
    let output = child
        .wait_with_output()
        .expect("wait for linalg_misc oracle");
    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(
            std::env::var(REQUIRE_SCIPY_ENV).is_err(),
            "linalg_misc oracle failed: {stderr}"
        );
        eprintln!("skipping linalg_misc oracle: scipy not available\n{stderr}");
        return None;
    }
    let stdout = String::from_utf8_lossy(&output.stdout);
    Some(serde_json::from_str(&stdout).expect("parse linalg_misc oracle JSON"))
}

fn fsci_eval(case: &PointCase) -> Option<Vec<f64>> {
    match case.op.as_str() {
        "fiedler" => Some(fiedler(&case.vec1d).into_iter().flatten().collect()),
        "fiedler_companion" => fiedler_companion(&case.vec1d)
            .ok()
            .map(|m| m.into_iter().flatten().collect()),
        "dft_matrix" => {
            let m = dft_matrix(case.n);
            let mut out = Vec::with_capacity(case.n * case.n * 2);
            for row in &m {
                for &(re, im) in row {
                    out.push(re);
                    out.push(im);
                }
            }
            Some(out)
        }
        "convolution_matrix" => Some(
            convolution_matrix(&case.vec1d, case.n, &case.mode)
                .into_iter()
                .flatten()
                .collect(),
        ),
        "bandwidth" => {
            let (lo, up) = bandwidth(&case.mat2d);
            Some(vec![lo as f64, up as f64])
        }
        "frobenius_norm" => Some(vec![frobenius_norm(&case.mat2d)]),
        _ => None,
    }
}

#[test]
fn diff_linalg_misc_deterministic() {
    let query = generate_query();
    let Some(oracle) = scipy_oracle_or_skip(&query) else {
        return;
    };
    assert_eq!(oracle.points.len(), query.points.len());

    let pmap: HashMap<String, PointArm> = oracle
        .points
        .into_iter()
        .map(|r| (r.case_id.clone(), r))
        .collect();

    let start = Instant::now();
    let mut diffs = Vec::new();
    let mut max_overall = 0.0_f64;
    let arms = [
        "fiedler",
        "fiedler_companion",
        "dft_matrix",
        "convolution_matrix",
        "bandwidth",
        "frobenius_norm",
    ];
    let mut ledger = CompareLedger::new("diff_linalg_misc_deterministic", &arms);

    for case in &query.points {
        let scipy_arm = pmap.get(&case.case_id).expect("validated oracle");
        let fsci_v = fsci_eval(case);
        let Some((scipy_v, fsci_v)) = ledger.slices(
            case.op.as_str(),
            &case.case_id,
            scipy_arm.values.as_deref(),
            fsci_v.as_deref(),
        ) else {
            continue;
        };
        let abs_d = fsci_v
            .iter()
            .zip(scipy_v.iter())
            .map(|(a, b)| (a - b).abs())
            .fold(0.0_f64, f64::max);
        max_overall = max_overall.max(abs_d);
        ledger.compared(case.op.as_str(), &case.case_id, abs_d <= ABS_TOL);
        diffs.push(CaseDiff {
            case_id: case.case_id.clone(),
            op: case.op.clone(),
            abs_diff: abs_d,
            pass: abs_d <= ABS_TOL,
        });
    }

    let all_pass = diffs.iter().all(|d| d.pass);

    let log = DiffLog {
        test_id: "diff_linalg_misc_deterministic".into(),
        category: "scipy.linalg fiedler/dft/convolution_matrix/bandwidth + frobenius_norm".into(),
        case_count: diffs.len(),
        compared: ledger.counts().clone(),
        max_abs_diff: max_overall,
        pass: all_pass,
        timestamp_ms: timestamp_ms(),
        duration_ns: start.elapsed().as_nanos(),
        cases: diffs.clone(),
    };
    emit_log(&log);

    for d in &diffs {
        if !d.pass {
            eprintln!(
                "linalg_misc {} mismatch: {} abs_diff={}",
                d.op, d.case_id, d.abs_diff
            );
        }
    }

    assert!(
        all_pass,
        "scipy.linalg misc-deterministic conformance failed: {} cases, max_diff={}",
        diffs.len(),
        max_overall
    );
    ledger.finish(
        arms.iter()
            .map(|arm| query.points.iter().filter(|c| c.op == *arm).count())
            .min()
            .unwrap_or(0),
    );
}