alkahest-cas 3.5.1

High-performance computer algebra kernel: symbolic expressions, polynomials, Gröbner bases, JIT, and Arb ball arithmetic.
Documentation
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
//! Tests for the Z-transform and its inverse.
//!
//! Forward transforms are checked against the analytic table; inverses are
//! checked by re-substituting the partial-fraction term back; the tie-in test
//! cross-checks against [`crate::sum::rsolve::rsolve`] for the Fibonacci
//! recurrence.

use super::*;
use crate::jit::eval_interp;
use crate::kernel::{Domain, ExprPool};
use std::collections::HashMap;

fn setup() -> (ExprPool, ExprId, ExprId) {
    let pool = ExprPool::new();
    let n = pool.symbol("n", Domain::Real);
    let z = pool.symbol("z", Domain::Real);
    (pool, n, z)
}

/// Numeric evaluation of an expression in a single variable at `var = val`.
fn eval_at(expr: ExprId, var: ExprId, val: f64, pool: &ExprPool) -> Option<f64> {
    let mut env = HashMap::new();
    env.insert(var, val);
    eval_interp(expr, &env, pool)
}

/// Assert two single-variable expressions agree numerically at a set of
/// sample points (avoids brittle structural comparison after simplification).
fn assert_numeric_eq(a: ExprId, b: ExprId, var: ExprId, samples: &[f64], pool: &ExprPool) {
    for &x in samples {
        let va = eval_at(a, var, x, pool);
        let vb = eval_at(b, var, x, pool);
        match (va, vb) {
            (Some(va), Some(vb)) => assert!(
                (va - vb).abs() < 1e-6 * (1.0 + va.abs() + vb.abs()),
                "mismatch at {x}: {} = {va} vs {} = {vb}",
                pool.display(a),
                pool.display(b),
            ),
            _ => panic!(
                "could not numerically evaluate at {x}: {} / {}",
                pool.display(a),
                pool.display(b)
            ),
        }
    }
}

/// Like [`assert_numeric_eq`], but also binds an extra free symbol (e.g. a
/// placeholder transform `X`/`A`) to a fixed numeric value at every sample.
fn assert_numeric_eq_with(
    a: ExprId,
    b: ExprId,
    var: ExprId,
    samples: &[f64],
    extra: ExprId,
    extra_val: f64,
    pool: &ExprPool,
) {
    for &x in samples {
        let mut env = HashMap::new();
        env.insert(var, x);
        env.insert(extra, extra_val);
        let va = eval_interp(a, &env, pool);
        let vb = eval_interp(b, &env, pool);
        match (va, vb) {
            (Some(va), Some(vb)) => assert!(
                (va - vb).abs() < 1e-6 * (1.0 + va.abs() + vb.abs()),
                "mismatch at {x}: {} = {va} vs {} = {vb}",
                pool.display(a),
                pool.display(b),
            ),
            _ => panic!(
                "could not numerically evaluate at {x}: {} / {}",
                pool.display(a),
                pool.display(b)
            ),
        }
    }
}

// ── forward table ───────────────────────────────────────────────────────────

#[test]
fn forward_constant() {
    let (pool, n, z) = setup();
    // Z{1}(z) = z/(z-1)
    let f = pool.integer(1_i32);
    let got = z_transform(f, n, z, &pool).unwrap();
    let want = pool.mul(vec![
        z,
        pool.pow(
            pool.add(vec![z, pool.integer(-1_i32)]),
            pool.integer(-1_i32),
        ),
    ]);
    assert_numeric_eq(got, want, z, &[2.0, 3.0, 5.0], &pool);
}

#[test]
fn forward_constant_scaled() {
    let (pool, n, z) = setup();
    // Z{5}(z) = 5z/(z-1)
    let f = pool.integer(5_i32);
    let got = z_transform(f, n, z, &pool).unwrap();
    let want = pool.mul(vec![
        pool.integer(5_i32),
        z,
        pool.pow(
            pool.add(vec![z, pool.integer(-1_i32)]),
            pool.integer(-1_i32),
        ),
    ]);
    assert_numeric_eq(got, want, z, &[2.0, 3.0, 5.0], &pool);
}

#[test]
fn forward_ramp_n() {
    let (pool, n, z) = setup();
    // Z{n}(z) = z/(z-1)^2
    let got = z_transform(n, n, z, &pool).unwrap();
    let want = pool.mul(vec![
        z,
        pool.pow(
            pool.add(vec![z, pool.integer(-1_i32)]),
            pool.integer(-2_i32),
        ),
    ]);
    assert_numeric_eq(got, want, z, &[2.0, 3.0, 5.0], &pool);
}

#[test]
fn forward_n_squared() {
    let (pool, n, z) = setup();
    // Z{n^2}(z) = z(z+1)/(z-1)^3
    let f = pool.pow(n, pool.integer(2_i32));
    let got = z_transform(f, n, z, &pool).unwrap();
    let want = pool.mul(vec![
        z,
        pool.add(vec![z, pool.integer(1_i32)]),
        pool.pow(
            pool.add(vec![z, pool.integer(-1_i32)]),
            pool.integer(-3_i32),
        ),
    ]);
    assert_numeric_eq(got, want, z, &[2.0, 3.0, 5.0], &pool);
}

#[test]
fn forward_geometric() {
    let (pool, n, z) = setup();
    // Z{a^n}(z) = z/(z-a), a = 1/2
    let a = pool.rational(1_i32, 2_i32);
    let f = pool.pow(a, n);
    let got = z_transform(f, n, z, &pool).unwrap();
    let want = pool.mul(vec![
        z,
        pool.pow(pool.add(vec![z, neg(a, &pool)]), pool.integer(-1_i32)),
    ]);
    assert_numeric_eq(got, want, z, &[3.0, 4.0, 5.0], &pool);
}

#[test]
fn forward_n_times_geometric() {
    let (pool, n, z) = setup();
    // Z{n a^n}(z) = a z / (z-a)^2, a = 1/2
    let a = pool.rational(1_i32, 2_i32);
    let f = pool.mul(vec![n, pool.pow(a, n)]);
    let got = z_transform(f, n, z, &pool).unwrap();
    let want = pool.mul(vec![
        a,
        z,
        pool.pow(pool.add(vec![z, neg(a, &pool)]), pool.integer(-2_i32)),
    ]);
    assert_numeric_eq(got, want, z, &[3.0, 4.0, 5.0], &pool);
}

#[test]
fn forward_sin_cos() {
    let (pool, n, z) = setup();
    let omega = pool.rational(1_i32, 3_i32);

    // Z{sin(omega n)}(z) = z sin(omega) / (z^2 - 2z cos(omega) + 1)
    let sin_on = pool.func("sin", vec![pool.mul(vec![omega, n])]);
    let got_sin = z_transform(sin_on, n, z, &pool).unwrap();
    let cos_w = pool.func("cos", vec![omega]);
    let sin_w = pool.func("sin", vec![omega]);
    let z2 = pool.pow(z, pool.integer(2_i32));
    let two_z_cos = pool.mul(vec![pool.integer(2_i32), z, cos_w]);
    let denom = pool.add(vec![z2, neg(two_z_cos, &pool), pool.integer(1_i32)]);
    let want_sin = pool.mul(vec![z, sin_w, pool.pow(denom, pool.integer(-1_i32))]);
    assert_numeric_eq(got_sin, want_sin, z, &[3.0, 5.0, 7.0], &pool);

    // Z{cos(omega n)}(z) = z(z - cos(omega)) / (z^2 - 2z cos(omega) + 1)
    let cos_on = pool.func("cos", vec![pool.mul(vec![omega, n])]);
    let got_cos = z_transform(cos_on, n, z, &pool).unwrap();
    let z_minus_cos = pool.add(vec![z, neg(cos_w, &pool)]);
    let want_cos = pool.mul(vec![z, z_minus_cos, pool.pow(denom, pool.integer(-1_i32))]);
    assert_numeric_eq(got_cos, want_cos, z, &[3.0, 5.0, 7.0], &pool);
}

// ── theorems ────────────────────────────────────────────────────────────────

#[test]
fn linearity() {
    let (pool, n, z) = setup();
    // Z{2 + 3n}(z) = 2*Z{1} + 3*Z{n}
    let f = pool.add(vec![
        pool.integer(2_i32),
        pool.mul(vec![pool.integer(3_i32), n]),
    ]);
    let got = z_transform(f, n, z, &pool).unwrap();

    let z1 = z_transform(pool.integer(1_i32), n, z, &pool).unwrap();
    let zn = z_transform(n, n, z, &pool).unwrap();
    let want = pool.add(vec![
        pool.mul(vec![pool.integer(2_i32), z1]),
        pool.mul(vec![pool.integer(3_i32), zn]),
    ]);
    assert_numeric_eq(got, want, z, &[3.0, 5.0, 7.0], &pool);
}

#[test]
fn scaling_theorem() {
    let (pool, n, z) = setup();
    // Z{a^n * n}(z) -- exercised already via forward_n_times_geometric, but
    // also check the direct scaling form Z{a^n * 1}(z) = X(z/a) with X = Z{1}
    let a = pool.rational(2_i32, 1_i32);
    let f = pool.mul(vec![pool.pow(a, n), pool.integer(1_i32)]);
    // Note: pool.mul(vec![a^n, 1]) simplifies to a^n; just confirm forward
    // matches the geometric table entry.
    let got = z_transform(f, n, z, &pool).unwrap();
    let want = pool.mul(vec![
        z,
        pool.pow(pool.add(vec![z, neg(a, &pool)]), pool.integer(-1_i32)),
    ]);
    assert_numeric_eq(got, want, z, &[5.0, 7.0], &pool);
}

#[test]
fn shift_delay_theorem() {
    let (pool, _n, z) = setup();
    let big_x = pool.symbol("X", Domain::Real);
    // Z{x[n-2]} = z^{-2} X(z)
    let got = z_shift_delay(big_x, z, 2, &pool);
    let want = pool.mul(vec![pool.pow(z, pool.integer(-2_i32)), big_x]);
    assert_numeric_eq_with(got, want, z, &[3.0, 5.0], big_x, 11.0, &pool);
}

#[test]
fn shift_advance_theorem() {
    let (pool, _n, z) = setup();
    let big_x = pool.symbol("X", Domain::Real);
    let x0 = pool.integer(7_i32);
    // Z{x[n+1]} = z X(z) - z x[0]
    let got = z_shift_advance(big_x, z, 1, &[x0], &pool);
    let want = pool.add(vec![
        pool.mul(vec![z, big_x]),
        pool.mul(vec![pool.integer(-1_i32), z, x0]),
    ]);
    assert_numeric_eq_with(got, want, z, &[3.0, 5.0], big_x, 11.0, &pool);
}

#[test]
fn differentiation_theorem() {
    let (pool, n, z) = setup();
    // Z{n}(z) via differentiation: -z d/dz[Z{1}] = -z d/dz[z/(z-1)]
    let got = z_transform(n, n, z, &pool).unwrap();

    let z1 = z_transform(pool.integer(1_i32), n, z, &pool).unwrap();
    let dz1 = crate::diff::diff(z1, z, &pool).unwrap().value;
    let want = simp(pool.mul(vec![pool.integer(-1_i32), z, dz1]), &pool);
    assert_numeric_eq(got, want, z, &[3.0, 5.0], &pool);
}

// ── inverse table ───────────────────────────────────────────────────────────

#[test]
fn inverse_geometric() {
    let (pool, n, z) = setup();
    // X(z) = z/(z-1/2)  ->  x[n] = (1/2)^n
    let a = pool.rational(1_i32, 2_i32);
    let big_x = pool.mul(vec![
        z,
        pool.pow(pool.add(vec![z, neg(a, &pool)]), pool.integer(-1_i32)),
    ]);
    let got = inverse_z_transform(big_x, z, n, &pool).unwrap();
    let want = pool.pow(a, n);
    assert_numeric_eq(got, want, n, &[0.0, 1.0, 2.0, 5.0, 10.0], &pool);
}

#[test]
fn inverse_constant() {
    let (pool, n, z) = setup();
    // X(z) = 5z/(z-1) -> x[n] = 5
    let big_x = pool.mul(vec![
        pool.integer(5_i32),
        z,
        pool.pow(
            pool.add(vec![z, pool.integer(-1_i32)]),
            pool.integer(-1_i32),
        ),
    ]);
    let got = inverse_z_transform(big_x, z, n, &pool).unwrap();
    let want = pool.integer(5_i32);
    assert_numeric_eq(got, want, n, &[0.0, 1.0, 2.0, 5.0], &pool);
}

#[test]
fn inverse_repeated_geometric() {
    let (pool, n, z) = setup();
    // X(z) = a z / (z-a)^2 -> x[n] = n a^n, a = 1/2
    let a = pool.rational(1_i32, 2_i32);
    let big_x = pool.mul(vec![
        a,
        z,
        pool.pow(pool.add(vec![z, neg(a, &pool)]), pool.integer(-2_i32)),
    ]);
    let got = inverse_z_transform(big_x, z, n, &pool).unwrap();
    let want = pool.mul(vec![n, pool.pow(a, n)]);
    assert_numeric_eq(got, want, n, &[0.0, 1.0, 2.0, 5.0, 8.0], &pool);
}

#[test]
fn inverse_round_trip_sum() {
    let (pool, n, z) = setup();
    // X(z) = Z{2 + 3 (1/2)^n}(z); recover x[n] = 2 + 3 (1/2)^n.
    let half = pool.rational(1_i32, 2_i32);
    let f = pool.add(vec![
        pool.integer(2_i32),
        pool.mul(vec![pool.integer(3_i32), pool.pow(half, n)]),
    ]);
    let big_x = z_transform(f, n, z, &pool).unwrap();
    let got = inverse_z_transform(big_x, z, n, &pool).unwrap();
    assert_numeric_eq(got, f, n, &[0.0, 1.0, 2.0, 4.0, 7.0], &pool);
}

// ── declines ────────────────────────────────────────────────────────────────

#[test]
fn decline_non_table_function() {
    let (pool, n, z) = setup();
    // tan(n) has no rule.
    let f = pool.func("tan", vec![n]);
    let err = z_transform(f, n, z, &pool).unwrap_err();
    assert!(matches!(err, ZTransformError::NoRule(_)));
}

#[test]
fn decline_same_variable() {
    let (pool, n, _z) = setup();
    let err = z_transform(pool.integer(1_i32), n, n, &pool).unwrap_err();
    assert_eq!(err, ZTransformError::SameVariable);

    let err2 = inverse_z_transform(pool.integer(1_i32), n, n, &pool).unwrap_err();
    assert_eq!(err2, ZTransformError::SameVariable);
}

#[test]
fn decline_high_order_pole() {
    let (pool, n, z) = setup();
    // X(z) = z/(z-1)^3 -- repeated pole order 3, not in the inverse table.
    let big_x = pool.mul(vec![
        z,
        pool.pow(
            pool.add(vec![z, pool.integer(-1_i32)]),
            pool.integer(-3_i32),
        ),
    ]);
    let err = inverse_z_transform(big_x, z, n, &pool).unwrap_err();
    assert!(matches!(err, ZTransformError::NotInvertible(_)));
}

#[test]
fn decline_real_surd_quadratic_inverse() {
    let (pool, n, z) = setup();
    // X(z) = z / (z^2 - z - 1) -- the Fibonacci denominator: real (golden-ratio)
    // surd roots, discriminant 5 > 0. Not a complex-conjugate pair, so the
    // damped-sinusoid inverse does not apply -> declined (documented).
    let z2 = pool.pow(z, pool.integer(2_i32));
    let denom = pool.add(vec![z2, neg(z, &pool), pool.integer(-1_i32)]);
    let big_x = pool.mul(vec![z, pool.pow(denom, pool.integer(-1_i32))]);
    let err = inverse_z_transform(big_x, z, n, &pool).unwrap_err();
    assert!(matches!(err, ZTransformError::NotInvertible(_)));
}

// ── inverse: irreducible-quadratic (complex-conjugate) poles → real cos/sin ──

#[test]
fn inverse_complex_pole_unit_circle() {
    // X(z) = z/(z² − z + 1) → real damped sinusoid (r = 1, θ = π/3):
    //   x[n] = (2/√3)·sin(π n / 3).
    // Verified by round-tripping the forward transform of the recovered x[n].
    let (pool, n, z) = setup();
    let z2 = pool.pow(z, pool.integer(2_i32));
    let denom = pool.add(vec![z2, neg(z, &pool), pool.integer(1_i32)]);
    let big_x = pool.mul(vec![z, pool.pow(denom, pool.integer(-1_i32))]);

    let x_n = inverse_z_transform(big_x, z, n, &pool).unwrap();
    // The output must be real — no imaginary unit anywhere.
    assert!(
        !pool.display(x_n).to_string().contains('I'),
        "complex-pole inverse must be real (no I): {}",
        pool.display(x_n),
    );

    // Round-trip: Z{x[n]} must reproduce the original X(z) (numerically in z).
    let round = z_transform(x_n, n, z, &pool).unwrap();
    assert_numeric_eq(round, big_x, z, &[2.0, 3.0, 5.0, 7.0], &pool);
}

#[test]
fn inverse_complex_pole_pure_imaginary() {
    // X(z) = z/(z² + 1) → r = 1, θ = π/2: x[n] = sin(π n / 2).
    let (pool, n, z) = setup();
    let z2 = pool.pow(z, pool.integer(2_i32));
    let denom = pool.add(vec![z2, pool.integer(1_i32)]);
    let big_x = pool.mul(vec![z, pool.pow(denom, pool.integer(-1_i32))]);

    let x_n = inverse_z_transform(big_x, z, n, &pool).unwrap();
    assert!(
        !pool.display(x_n).to_string().contains('I'),
        "complex-pole inverse must be real (no I): {}",
        pool.display(x_n),
    );
    // x[n] = sin(π n / 2): 0, 1, 0, −1, 0, 1, … — check the first few samples.
    for (k, want) in [(0.0, 0.0), (1.0, 1.0), (2.0, 0.0), (3.0, -1.0), (4.0, 0.0)] {
        let got = eval_at(x_n, n, k, &pool).expect("evaluable");
        assert!(
            (got - want).abs() < 1e-9,
            "x[{k}] = {got}, want {want} for sin(πn/2)",
        );
    }

    // Round-trip through the forward transform.
    let round = z_transform(x_n, n, z, &pool).unwrap();
    assert_numeric_eq(round, big_x, z, &[2.0, 3.0, 5.0], &pool);
}

#[test]
fn inverse_complex_pole_damped() {
    // Damped: X(z) = z/(z² − z + 1/2) → r = 1/√2, θ = π/4.
    // (z² − 2r cosθ z + r²) with r² = 1/2, 2r cosθ = 1 ⇒ cosθ = 1/√2 ⇒ θ = π/4.)
    let (pool, n, z) = setup();
    let z2 = pool.pow(z, pool.integer(2_i32));
    let denom = pool.add(vec![z2, neg(z, &pool), pool.rational(1_i32, 2_i32)]);
    let big_x = pool.mul(vec![z, pool.pow(denom, pool.integer(-1_i32))]);

    let x_n = inverse_z_transform(big_x, z, n, &pool).unwrap();
    assert!(
        !pool.display(x_n).to_string().contains('I'),
        "complex-pole inverse must be real (no I): {}",
        pool.display(x_n),
    );
    let round = z_transform(x_n, n, z, &pool).unwrap();
    assert_numeric_eq(round, big_x, z, &[2.0, 3.0, 5.0, 8.0], &pool);
}

// ── tie-in: cross-check against rsolve (Fibonacci) ──────────────────────────

#[test]
fn fibonacci_via_z_transform_matches_rsolve() {
    use crate::sum::rsolve;
    use std::collections::BTreeMap;

    let (pool, n, z) = setup();

    // Difference equation: a[n+2] = a[n+1] + a[n], a[0] = 0, a[1] = 1.
    //
    // Apply Z to both sides (unilateral advance theorem twice / shift once):
    //   Z{a[n+2]} = z^2 A(z) - z^2 a[0] - z a[1]
    //   Z{a[n+1]} = z A(z) - z a[0]
    //   Z{a[n]}   = A(z)
    //
    // z^2 A - z^2 a0 - z a1 = (z A - z a0) + A
    // A (z^2 - z - 1) = z^2 a0 + z a1 - z a0 = z a1   (since a0 = 0)
    // A(z) = z a1 / (z^2 - z - 1) = z / (z^2 - z - 1)
    let a0 = pool.integer(0_i32);
    let a1 = pool.integer(1_i32);
    let big_a = pool.symbol("A", Domain::Real);

    let lhs = z_shift_advance(big_a, z, 2, &[a0, a1], &pool);
    let rhs = pool.add(vec![z_shift_advance(big_a, z, 1, &[a0], &pool), big_a]);
    // Solve lhs == rhs for A linearly: A * (z^2 - z - 1) = z^2 a0 + z a1 - z a0
    // We just build A(z) directly via the closed-form algebraic solution
    // below, then *verify* lhs == rhs holds when A is substituted.
    let z2 = pool.pow(z, pool.integer(2_i32));
    let denom = pool.add(vec![z2, neg(z, &pool), pool.integer(-1_i32)]);
    let big_a_expr = pool.mul(vec![z, pool.pow(denom, pool.integer(-1_i32))]);

    // Verify the algebraic equation lhs(A) == rhs(A) holds for this A(z).
    let mut map = std::collections::HashMap::new();
    map.insert(big_a, big_a_expr);
    let lhs_sub = simp(crate::kernel::subs(lhs, &map, &pool), &pool);
    let rhs_sub = simp(crate::kernel::subs(rhs, &map, &pool), &pool);
    assert_numeric_eq(lhs_sub, rhs_sub, z, &[3.0, 5.0, 7.0], &pool);

    // Now invert A(z) = z/(z^2-z-1) -- the denominator factors over the
    // golden-ratio conjugates, which is *not* a single linear pole, so
    // `inverse_z_transform`'s linear-pole-only table declines it (documented
    // limitation). We still cross-check against `rsolve`'s closed form by
    // partial-fractioning over the algebraic numbers ourselves is out of
    // scope; instead verify the *forward* transform of the `rsolve` solution
    // reproduces A(z), and separately confirm the first terms of the
    // sequence match the well-known Fibonacci numbers.
    let inv_err = inverse_z_transform(big_a_expr, z, n, &pool).unwrap_err();
    assert!(matches!(inv_err, ZTransformError::NotInvertible(_)));

    // rsolve cross-check: a[n] = a[n-1] + a[n-2], a[0]=0, a[1]=1.
    let f = |args: Vec<ExprId>| pool.func("f", args);
    let eq = simp(
        pool.add(vec![
            f(vec![n]),
            pool.mul(vec![
                f(vec![pool.add(vec![n, pool.integer(-1_i32)])]),
                pool.integer(-1_i32),
            ]),
            pool.mul(vec![
                f(vec![pool.add(vec![n, pool.integer(-2_i32)])]),
                pool.integer(-1_i32),
            ]),
        ]),
        &pool,
    );
    let mut init = BTreeMap::new();
    init.insert(0, pool.integer(0));
    init.insert(1, pool.integer(1));
    let rsolve_sol = rsolve(&pool, eq, n, "f", Some(&init)).expect("rsolve");

    // Known Fibonacci numbers 0..=7.
    let expected = [0.0, 1.0, 1.0, 2.0, 3.0, 5.0, 8.0, 13.0];
    for (ni, &exp) in expected.iter().enumerate() {
        let mut env = HashMap::new();
        env.insert(n, ni as f64);
        let v = eval_interp(rsolve_sol, &env, &pool).expect("eval rsolve");
        assert!((v - exp).abs() < 1e-4, "n={ni}: rsolve={v} expected={exp}");
    }
}