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
#![forbid(unsafe_code)]
//! Live SciPy differential coverage for the Gauss
//! hypergeometric `scipy.special.hyp2f1` ( ₂F₁(a, b; c; z) ).
//!
//! Resolves [frankenscipy-6cbyo]. Companion to
//! `diff_special_hyp1f1`. fsci-special's hyp2f1 is the most
//! precision-sensitive of the hypergeometric kernels — series
//! converges for |z| < 1, with branch-cut and asymptotic-form
//! handling for |z| ≥ 1. fsci's implementation handles the
//! z<0 and 0<z<1 series regimes; near-z=1 and |z|>1 are tested
//! separately.
//!
//! 40 finite-valued (a, b, c, z) cases plus 4 real branch-cut
//! cases via subprocess. Tolerance: 1e-9 of max(|value|, 1); the widest
//! gap measured against SciPy 1.17.1 is 2.6e-14 (frankenscipy-14k5d, which
//! added the z down to -1e8 cases and tightened this from 5e-7).

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_runtime::RuntimeMode;
use fsci_special::hyp2f1;
use fsci_special::types::SpecialTensor;
use serde::{Deserialize, Serialize};

const PACKET_ID: &str = "FSCI-P2C-007";
const TOL_REL: f64 = 1.0e-9;
const REQUIRE_SCIPY_ENV: &str = "FSCI_REQUIRE_SCIPY_ORACLE";

#[derive(Debug, Clone, Serialize)]
struct PointCase {
    case_id: String,
    a: f64,
    b: f64,
    c: f64,
    z: f64,
}

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

#[derive(Debug, Clone, Deserialize)]
struct PointArm {
    case_id: String,
    value: Option<f64>,
}

#[derive(Debug, Clone, Deserialize)]
struct BranchCutArm {
    case_id: String,
    positive_infinity: bool,
}

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

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

#[derive(Debug, Clone, Serialize)]
struct BranchCutDiff {
    case_id: String,
    scipy_positive_infinity: bool,
    rust_positive_infinity: bool,
    hardened_error: bool,
    pass: bool,
}

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

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 hyp2f1 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 hyp2f1 diff log");
    fs::write(path, json).expect("write hyp2f1 diff log");
}

fn fsci_eval(a: f64, b: f64, c: f64, z: f64) -> Option<f64> {
    let pa = SpecialTensor::RealScalar(a);
    let pb = SpecialTensor::RealScalar(b);
    let pc = SpecialTensor::RealScalar(c);
    let pz = SpecialTensor::RealScalar(z);
    match hyp2f1(&pa, &pb, &pc, &pz, RuntimeMode::Strict) {
        Ok(SpecialTensor::RealScalar(v)) => Some(v),
        _ => None,
    }
}

fn fsci_hardened_errors(a: f64, b: f64, c: f64, z: f64) -> bool {
    let pa = SpecialTensor::RealScalar(a);
    let pb = SpecialTensor::RealScalar(b);
    let pc = SpecialTensor::RealScalar(c);
    let pz = SpecialTensor::RealScalar(z);
    hyp2f1(&pa, &pb, &pc, &pz, RuntimeMode::Hardened).is_err()
}

fn generate_query() -> OracleQuery {
    // Pinned (a, b, c, z) — emphasizing identities and
    // small/moderate-z safe regimes:
    //   ₂F₁(1, 1, 2, z) = -ln(1-z)/z
    //   ₂F₁(0.5, 0.5, 1, z) = (2/π)·K(z) (related to ellipk)
    //   ₂F₁(1, 1, 3/2, z²) = arcsin(z)/(z√(1-z²))
    let cases: &[(f64, f64, f64, f64)] = &[
        // Identity: -ln(1-z)/z
        (1.0, 1.0, 2.0, 0.5),
        (1.0, 1.0, 2.0, -0.5),
        (1.0, 1.0, 2.0, 0.1),
        (1.0, 1.0, 2.0, -0.9),
        // Identity: (2/π)·K(z)
        (0.5, 0.5, 1.0, 0.25),
        (0.5, 0.5, 1.0, 0.5),
        (0.5, 0.5, 1.0, 0.9),
        // Generic mid-range
        (1.5, 2.5, 3.0, 0.3),
        (1.5, 2.5, 3.0, -0.3),
        (2.0, 3.0, 4.0, 0.5),
        (2.0, 3.0, 4.0, -0.5),
        (0.3, 0.7, 1.5, 0.4),
        (0.3, 0.7, 1.5, -0.7),
        // Larger parameters
        (5.0, 5.0, 10.0, 0.5),
        (5.0, 5.0, 10.0, -0.5),
        (3.0, 7.0, 11.0, 0.7),
        // Small z
        (1.5, 2.5, 3.5, 0.001),
        (2.0, 4.0, 6.0, 1.0e-6),
        // Negative z.
        (1.0, 2.0, 3.0, -0.9),
        (0.5, 1.5, 2.5, -0.5),
        // ₂F₁(a, b, b, z) = (1-z)^(-a)
        (1.0, 5.0, 5.0, 0.5),
        (2.0, 3.0, 3.0, -0.5),
        // Misc
        (1.0, 0.5, 1.5, -0.3),
        (2.5, 1.5, 4.0, 0.2),
        // |z| near and at 1, and z < −1. These were left out because fsci's series returned
        // NaN for |z| ≥ 0.99 in some regimes.
        (1.0, 2.0, 3.0, -0.99),
        (1.0, 2.0, 3.0, -0.999),
        (1.0, 2.0, 3.0, -1.0),
        (0.5, 1.5, 2.5, -0.995),
        (1.5, 2.5, 3.0, -0.99),
        (0.5, 0.5, 1.0, 0.99),
        (1.0, 1.0, 2.0, 0.999),
        (2.0, 3.0, 4.0, -5.0),
        (0.3, 0.7, 1.5, -3.0),
        // z far below -1 (frankenscipy-14k5d): Pfaff's series at z/(z - 1) → 1 ran out of
        // terms and fsci returned NaN past z ≈ -140. b - a near an integer (-3.05), generic
        // (-1.71, -0.38), an integer (2), c - a a nonpositive integer, and |z| = 1e8.
        (
            3.960983117050649,
            0.910412268763193,
            8.652771041686874,
            -648.335145138167,
        ),
        (
            2.516937449187653,
            0.802092593722219,
            3.0624938125185626,
            -2007.9925247805832,
        ),
        (
            1.0728917813246568,
            0.693772918681244,
            6.97221658335025,
            -6219.058167385678,
        ),
        (1.5, 3.5, 2.25, -1000.0),
        (3.5, 1.25, 1.5, -2000.0),
        (0.5, 1.75, 2.5, -1.0e8),
        (-2.5, 1.5, 4.0, -300.0),
    ];
    let mut points = Vec::new();
    for (i, &(a, b, c, z)) in cases.iter().enumerate() {
        points.push(PointCase {
            case_id: format!("a{a}_b{b}_c{c}_z{z}_i{i}"),
            a,
            b,
            c,
            z,
        });
    }
    let branch_cut_cases: [(f64, f64, f64, f64); 4] = [
        (1.0, 2.0, 3.0, 1.5),
        (0.25, 0.75, 2.5, 1.25),
        (2.0, 1.0, 5.0, 1.1),
        (0.5, 1.5, 4.0, 2.0),
    ];
    let mut branch_cut_points = Vec::new();
    for (i, &(a, b, c, z)) in branch_cut_cases.iter().enumerate() {
        branch_cut_points.push(PointCase {
            case_id: format!("branch_cut_a{a}_b{b}_c{c}_z{z}_i{i}"),
            a,
            b,
            c,
            z,
        });
    }
    OracleQuery {
        points,
        branch_cut_points,
    }
}

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

def finite_or_none(v):
    try:
        v = float(v)
    except Exception:
        return None
    return v if math.isfinite(v) else None

q = json.loads(sys.argv[1])
points = []
for case in q["points"]:
    cid = case["case_id"]
    a = float(case["a"]); b = float(case["b"])
    c = float(case["c"]); z = float(case["z"])
    try:
        value = special.hyp2f1(a, b, c, z)
        points.append({"case_id": cid, "value": finite_or_none(value)})
    except Exception:
        points.append({"case_id": cid, "value": None})
branch_cut_points = []
for case in q["branch_cut_points"]:
    cid = case["case_id"]
    a = float(case["a"]); b = float(case["b"])
    c = float(case["c"]); z = float(case["z"])
    try:
        value = float(special.hyp2f1(a, b, c, z))
        branch_cut_points.append({
            "case_id": cid,
            "positive_infinity": math.isinf(value) and value > 0.0,
        })
    except Exception:
        branch_cut_points.append({"case_id": cid, "positive_infinity": False})
print(json.dumps({"points": points, "branch_cut_points": branch_cut_points}))
"#;

    let query_json = serde_json::to_string(query).expect("serialize hyp2f1 query");
    let mut child = match fsci_conformance::scipy_oracle_command()
        .arg("-")
        .arg(&query_json)
        .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 hyp2f1 oracle: {e}"
            );
            eprintln!("skipping hyp2f1 oracle: python3 not available ({e})");
            return None;
        }
    };
    {
        let stdin = child.stdin.as_mut().expect("open hyp2f1 oracle stdin");
        if let Err(err) = stdin.write_all(script.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(),
                "hyp2f1 oracle script write failed: {err}; stderr: {stderr}"
            );
            eprintln!("skipping hyp2f1 oracle: script write failed ({err})\n{stderr}");
            return None;
        }
    }
    let output = child.wait_with_output().expect("wait for hyp2f1 oracle");
    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(
            std::env::var(REQUIRE_SCIPY_ENV).is_err(),
            "hyp2f1 oracle failed: {stderr}"
        );
        eprintln!("skipping hyp2f1 oracle: scipy not available\n{stderr}");
        return None;
    }
    let stdout = String::from_utf8_lossy(&output.stdout);
    Some(serde_json::from_str(&stdout).expect("parse hyp2f1 oracle JSON"))
}

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

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

    let start = Instant::now();
    let mut diffs = Vec::new();
    let mut branch_cut_diffs = Vec::new();
    let mut max_abs_overall = 0.0_f64;
    let mut max_rel_overall = 0.0_f64;
    let mut ledger = CompareLedger::new("diff_special_hyp2f1", &["hyp2f1", "branch_cut"]);

    for case in &query.points {
        let oracle = pmap.get(&case.case_id).expect("validated oracle");
        let Some((scipy_v, rust_v)) = ledger.pair(
            "hyp2f1",
            &case.case_id,
            oracle.value,
            fsci_eval(case.a, case.b, case.c, case.z),
        ) else {
            continue;
        };
        let abs_diff = (rust_v - scipy_v).abs();
        let scale = scipy_v.abs().max(1.0);
        let rel_diff = abs_diff / scale;
        max_abs_overall = max_abs_overall.max(abs_diff);
        max_rel_overall = max_rel_overall.max(rel_diff);
        ledger.compared("hyp2f1", &case.case_id, abs_diff <= TOL_REL * scale);
        diffs.push(CaseDiff {
            case_id: case.case_id.clone(),
            abs_diff,
            rel_diff,
            pass: abs_diff <= TOL_REL * scale,
        });
    }

    for case in &query.branch_cut_points {
        let oracle = branch_cut_map
            .get(&case.case_id)
            .expect("validated branch-cut oracle");
        // The oracle always reports a bool (a raise reads as `false`, which fails the case);
        // a strict-mode fsci error is recorded as an fsci failure.
        let Some((scipy_positive_infinity, rust_v)) = ledger.both(
            "branch_cut",
            &case.case_id,
            Some(oracle.positive_infinity),
            fsci_eval(case.a, case.b, case.c, case.z),
        ) else {
            continue;
        };
        let rust_positive_infinity = rust_v.is_infinite() && rust_v.is_sign_positive();
        let hardened_error = fsci_hardened_errors(case.a, case.b, case.c, case.z);
        let pass = scipy_positive_infinity && rust_positive_infinity && hardened_error;
        ledger.compared("branch_cut", &case.case_id, pass);
        branch_cut_diffs.push(BranchCutDiff {
            case_id: case.case_id.clone(),
            scipy_positive_infinity,
            rust_positive_infinity,
            hardened_error,
            pass,
        });
    }

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

    let log = DiffLog {
        test_id: "diff_special_hyp2f1".into(),
        category: "scipy.special.hyp2f1".into(),
        case_count: diffs.len() + branch_cut_diffs.len(),
        compared: ledger.counts().clone(),
        max_abs_diff: max_abs_overall,
        max_rel_diff: max_rel_overall,
        pass: all_pass,
        timestamp_ms: timestamp_ms(),
        duration_ns: start.elapsed().as_nanos(),
        cases: diffs.clone(),
        branch_cut_cases: branch_cut_diffs.clone(),
    };

    emit_log(&log);

    for d in &diffs {
        if !d.pass {
            eprintln!(
                "hyp2f1 mismatch: {} abs={} rel={}",
                d.case_id, d.abs_diff, d.rel_diff
            );
        }
    }
    for d in &branch_cut_diffs {
        if !d.pass {
            eprintln!(
                "hyp2f1 branch-cut mismatch: {} scipy_inf={} rust_inf={} hardened_error={}",
                d.case_id, d.scipy_positive_infinity, d.rust_positive_infinity, d.hardened_error
            );
        }
    }

    assert!(
        all_pass,
        "scipy.special.hyp2f1 conformance failed: {} cases, max_abs={} max_rel={}",
        diffs.len(),
        max_abs_overall,
        max_rel_overall
    );
    // The two arms have separate case lists (branch_cut has the fewest); each must compare all
    // of its own.
    ledger.finish(query.points.len().min(query.branch_cut_points.len()));
}