alkahest-cas 3.10.0

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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
//! The coefficient tower `q`-Zeilberger runs in: `Q(q) ⊂ Q(q)(x) ⊂ Q(q)(x)(y)`.
//!
//! With `x = qⁿ` and `y = q^k`, every shift quotient of a `q`-hypergeometric
//! term is a *rational function* of `x` and `y` over `Q(q)`, and the two shift
//! operators act on that field as the `Q(q)`-algebra maps
//!
//! ```text
//! n ↦ n+1   is   x ↦ q·x        k ↦ k+1   is   y ↦ q·y
//! ```
//!
//! which is what makes the classical Gosper/Zeilberger derivation carry over
//! verbatim with `k+1` read as `q·y` — see [`super::search`].
//!
//! # Reuse, deliberately
//!
//! The bottom two levels are **not** new code. [`super::super::qfield`]'s
//! [`Rn`] is `Q(v)` for one variable `v` and [`PolyK`]/[`RatK`] are `Rn[v₂]`
//! and `Rn(v₂)`; naming the outer variable `q` instead of `n` and the inner one
//! `x` instead of `k` gives exactly `Q(q)`, `Q(q)[x]` and `Q(q)(x)`, with the
//! same tested arithmetic (including the `Z[·][·]` gcd that keeps `normalize`
//! from blowing up). The aliases [`Qq`], [`PolyX`], [`RatX`] record that
//! reinterpretation.
//!
//! What is *not* reused is the shift: `PolyK::shift_k` / `shift_n` are additive
//! (`v ↦ v + j`), and the `q`-world needs the multiplicative `x ↦ q^i·x`, which
//! is [`polyx_qshift`] / [`ratx_qshift`] here. Only the third level, `y`, is
//! new: [`PolyY`] and [`RatY`] below.

use super::super::qfield::{
    rn_add, rn_inv, rn_is_zero, rn_mul, rn_one, rn_poly, rn_zero, PolyK, RatK, Rn,
};
use crate::matrix::normal_form::RatUniPoly;
use rug::Rational;

/// `Q(q)` — rational functions in the `q` of a `q`-analogue.
pub type Qq = Rn;
/// `Q(q)[x]` with `x = qⁿ`.
pub type PolyX = PolyK;
/// `Q(q)(x)` — where the recurrence coefficients `a_i` live.
pub type RatX = RatK;

// ---------------------------------------------------------------------------
// Cooperative refusal for the field arithmetic
// ---------------------------------------------------------------------------

/// Largest a single `Q(q)(x)` element may grow to inside this tower's
/// arithmetic, counted by [`ratx_terms`].
///
/// The Euclidean gcd used by [`RatY::normalize`] has no content removal, which
/// is the classical way for coefficients to explode while the *degrees* stay
/// small — and small degrees are exactly what the shape ceilings in
/// [`super::search`] can see. Sized well above anything the working cases in
/// this module's tests reach (the largest observed is under 100).
pub const MAX_FIELD_ELEMENT_TERMS: usize = 6_000;

/// Why the field arithmetic gave up part-way through, if it did.
#[derive(Clone, Copy, Debug)]
pub enum FieldRefusal {
    /// A coefficient grew past [`MAX_FIELD_ELEMENT_TERMS`]; the payload is the
    /// size it reached.
    SizeCeiling(usize),
    /// An active [`crate::budget`] stopped it.
    Budget(crate::budget::BudgetTrip),
}

thread_local! {
    static REFUSAL: std::cell::Cell<Option<FieldRefusal>> = const { std::cell::Cell::new(None) };
}

/// Record a refusal for the caller to pick up.
///
/// The arithmetic here is infallible by signature (`PolyY::gcd` returns a
/// `PolyY`, not a `Result`) and threading `Result` through every operator
/// would be a far larger change than the problem warrants, so a refusal
/// travels out of band and the operation returns a *correct but unhelpful*
/// answer — `div_rem` returns `None`, which `gcd` already handles by returning
/// `1`, which `normalize` already handles by not cancelling. Nothing becomes
/// wrong; the caller is expected to check [`take_field_refusal`] and stop.
fn note_refusal(r: FieldRefusal) {
    REFUSAL.with(|c| c.set(Some(r)));
}

/// Clear any recorded refusal. Call before each probe.
pub fn clear_field_refusal() {
    REFUSAL.with(|c| c.set(None));
}

/// Take (and clear) the refusal recorded on this thread, if any.
pub fn take_field_refusal() -> Option<FieldRefusal> {
    REFUSAL.with(|c| c.take())
}

/// The largest coefficient of `p`, by [`ratx_terms`].
fn widest_coeff(p: &PolyY) -> usize {
    p.coeffs.iter().map(ratx_terms).max().unwrap_or(0)
}

/// How many rational numbers it takes to write `r` down — a cheap, exact
/// proxy for how big an element of `Q(q)(x)` has become.
///
/// The `q`-tower is three levels deep (`Q(q) ⊂ Q(q)(x) ⊂ Q(q)(x)(y)`), so a
/// single "coefficient" in the linear system is a quotient of polynomials in
/// `x` whose own coefficients are quotients of polynomials in `q`. Elimination
/// grows that nesting, and the growth is fragile in the *input* rather than in
/// `max_order`/`max_degree`: `Σ_k [n;k]_q` decides in half a second where
/// `Σ_k [2n;k]_q` does not return at the cheapest bounds the engine accepts.
/// A ceiling on the shape of the system cannot see that; this can.
///
/// Counts coefficient slots rather than bit-lengths: it is `O(size)` with no
/// allocation and no GMP calls, which is what makes it affordable in the
/// inner elimination loop.
pub fn ratx_terms(r: &RatX) -> usize {
    fn polyx_terms(p: &PolyX) -> usize {
        p.coeffs
            .iter()
            .map(|c| c.num.coeffs.len() + c.den.coeffs.len())
            .sum()
    }
    polyx_terms(&r.num) + polyx_terms(&r.den)
}

/// `q^i ∈ Q(q)`, for any sign of `i`.
pub fn qq_pow(i: i64) -> Qq {
    if i == 0 {
        return rn_one();
    }
    let mut coeffs = vec![Rational::from(0); i.unsigned_abs() as usize + 1];
    coeffs[i.unsigned_abs() as usize] = Rational::from(1);
    let mono = rn_poly(RatUniPoly { coeffs }.trim());
    if i > 0 {
        mono
    } else {
        // `mono` is the monomial `q^{|i|} ≠ 0`, so the inverse exists.
        rn_inv(&mono).unwrap_or_else(rn_one)
    }
}

/// `x^a ∈ Q(q)(x)`, for any sign of `a`.
pub fn ratx_x_pow(a: i64) -> RatX {
    let mut coeffs = vec![rn_zero(); a.unsigned_abs() as usize + 1];
    coeffs[a.unsigned_abs() as usize] = rn_one();
    let mono = RatX::from_poly(PolyX::from_coeffs(coeffs));
    if a >= 0 {
        mono
    } else {
        mono.inv().unwrap_or_else(RatX::one)
    }
}

/// `p(x)` with `x ↦ q^i·x` — the action of `n ↦ n+i` on `Q(q)[x]`.
pub fn polyx_qshift(p: &PolyX, i: i64) -> PolyX {
    if i == 0 {
        return p.clone();
    }
    PolyX::from_coeffs(
        p.coeffs
            .iter()
            .enumerate()
            .map(|(d, c)| rn_mul(c, &qq_pow(i * d as i64)))
            .collect(),
    )
}

/// `r(x)` with `x ↦ q^i·x` — the action of `n ↦ n+i` on `Q(q)(x)`.
pub fn ratx_qshift(r: &RatX, i: i64) -> RatX {
    if i == 0 {
        return r.clone();
    }
    RatX {
        num: polyx_qshift(&r.num, i),
        den: polyx_qshift(&r.den, i),
    }
    .normalize()
}

// ---------------------------------------------------------------------------
// Q(q)(x)[y]
// ---------------------------------------------------------------------------

/// A polynomial in `y = q^k` with coefficients in `Q(q)(x)` (ascending order).
///
/// The same dense representation as [`PolyK`], one level up the tower; the
/// operations are the field-generic ones, so the coefficient arithmetic is
/// `RatX`'s and every reduction below is exact.
#[derive(Clone, Debug)]
pub struct PolyY {
    pub coeffs: Vec<RatX>,
}

impl PolyY {
    pub fn zero() -> Self {
        PolyY { coeffs: vec![] }
    }

    pub fn one() -> Self {
        PolyY {
            coeffs: vec![RatX::one()],
        }
    }

    pub fn constant(c: RatX) -> Self {
        PolyY { coeffs: vec![c] }.trim()
    }

    /// The polynomial `y`.
    pub fn y() -> Self {
        PolyY {
            coeffs: vec![RatX::zero(), RatX::one()],
        }
    }

    pub fn from_coeffs(coeffs: Vec<RatX>) -> Self {
        PolyY { coeffs }.trim()
    }

    pub fn trim(mut self) -> Self {
        while self.coeffs.last().map(RatX::is_zero).unwrap_or(false) {
            self.coeffs.pop();
        }
        self
    }

    pub fn is_zero(&self) -> bool {
        self.coeffs.iter().all(RatX::is_zero)
    }

    /// Degree, or `-1` for the zero polynomial.
    pub fn degree(&self) -> i32 {
        let mut d = self.coeffs.len() as i32 - 1;
        while d >= 0 && self.coeffs[d as usize].is_zero() {
            d -= 1;
        }
        d
    }

    pub fn coeff(&self, i: usize) -> RatX {
        self.coeffs.get(i).cloned().unwrap_or_else(RatX::zero)
    }

    pub fn leading_coeff(&self) -> RatX {
        let d = self.degree();
        if d < 0 {
            RatX::zero()
        } else {
            self.coeff(d as usize)
        }
    }

    pub fn add(&self, other: &PolyY) -> PolyY {
        let n = self.coeffs.len().max(other.coeffs.len());
        PolyY {
            coeffs: (0..n).map(|i| self.coeff(i).add(&other.coeff(i))).collect(),
        }
        .trim()
    }

    pub fn neg(&self) -> PolyY {
        PolyY {
            coeffs: self.coeffs.iter().map(RatX::neg).collect(),
        }
    }

    pub fn sub(&self, other: &PolyY) -> PolyY {
        self.add(&other.neg())
    }

    pub fn mul(&self, other: &PolyY) -> PolyY {
        if self.is_zero() || other.is_zero() {
            return PolyY::zero();
        }
        let mut out = vec![RatX::zero(); self.coeffs.len() + other.coeffs.len() - 1];
        for (i, a) in self.coeffs.iter().enumerate() {
            if a.is_zero() {
                continue;
            }
            for (j, b) in other.coeffs.iter().enumerate() {
                if b.is_zero() {
                    continue;
                }
                out[i + j] = out[i + j].add(&a.mul(b));
            }
        }
        PolyY { coeffs: out }.trim()
    }

    pub fn scale(&self, c: &RatX) -> PolyY {
        if c.is_zero() {
            return PolyY::zero();
        }
        PolyY {
            coeffs: self.coeffs.iter().map(|a| a.mul(c)).collect(),
        }
        .trim()
    }

    /// Euclidean division over the field `Q(q)(x)`.
    pub fn div_rem(a: &PolyY, b: &PolyY) -> Option<(PolyY, PolyY)> {
        if b.is_zero() {
            return None;
        }
        let db = b.degree();
        let lb_inv = b.leading_coeff().inv()?;
        let mut rem = a.clone().trim();
        let mut quot: Vec<RatX> = Vec::new();
        while !rem.is_zero() && rem.degree() >= db {
            // The hot loop of `gcd`, and the one place in this tower where a
            // small system can run for minutes: the remainder's *degree* falls
            // every step while its coefficients grow without bound.
            if let Err(t) = crate::budget::check_all() {
                note_refusal(FieldRefusal::Budget(t));
                return None;
            }
            // The `Z[q][x]` gcd one level down has given up, so every `RatX`
            // operation from here on runs on unreduced (and therefore growing)
            // representations. Stop now rather than finish this division more
            // slowly than it would have run with cancellation.
            if super::super::qfield::gcd_stop_pending() {
                note_refusal(FieldRefusal::SizeCeiling(widest_coeff(&rem)));
                return None;
            }
            let widest = widest_coeff(&rem);
            if widest > MAX_FIELD_ELEMENT_TERMS {
                note_refusal(FieldRefusal::SizeCeiling(widest));
                return None;
            }
            let shift = (rem.degree() - db) as usize;
            let t = rem.leading_coeff().mul(&lb_inv);
            if shift >= quot.len() {
                quot.resize(shift + 1, RatX::zero());
            }
            quot[shift] = quot[shift].add(&t);
            let mut sub_coeffs = vec![RatX::zero(); shift];
            sub_coeffs.extend(b.coeffs.iter().map(|c| c.mul(&t)));
            rem = rem.sub(&PolyY { coeffs: sub_coeffs });
        }
        Some((PolyY { coeffs: quot }.trim(), rem.trim()))
    }

    pub fn exact_div(a: &PolyY, b: &PolyY) -> Option<PolyY> {
        let (q, r) = PolyY::div_rem(a, b)?;
        r.is_zero().then_some(q)
    }

    /// Monic gcd over `Q(q)(x)`, by the Euclidean algorithm.
    pub fn gcd(a: &PolyY, b: &PolyY) -> PolyY {
        let mut x = a.clone().trim();
        let mut y = b.clone().trim();
        if x.is_zero() && y.is_zero() {
            return PolyY::zero();
        }
        while !y.is_zero() {
            let Some((_, r)) = PolyY::div_rem(&x, &y) else {
                return PolyY::one();
            };
            x = y;
            y = r;
        }
        x.monic()
    }

    pub fn monic(&self) -> PolyY {
        match self.leading_coeff().inv() {
            Some(inv) => self.scale(&inv),
            None => self.clone(),
        }
    }

    /// `lcm` via `a·b/gcd`.
    pub fn lcm(a: &PolyY, b: &PolyY) -> PolyY {
        if a.is_zero() || b.is_zero() {
            return PolyY::zero();
        }
        let g = PolyY::gcd(a, b);
        let prod = a.mul(b);
        PolyY::exact_div(&prod, &g).unwrap_or(prod)
    }

    /// `p` with `y ↦ q^j·y` — the action of `k ↦ k+j`.
    pub fn qshift_y(&self, j: i64) -> PolyY {
        if j == 0 {
            return self.clone();
        }
        PolyY::from_coeffs(
            self.coeffs
                .iter()
                .enumerate()
                .map(|(d, c)| c.mul(&RatX::from_rn(qq_pow(j * d as i64))))
                .collect(),
        )
    }

    /// `p` with `x ↦ q^i·x` — the action of `n ↦ n+i`.
    pub fn qshift_x(&self, i: i64) -> PolyY {
        if i == 0 {
            return self.clone();
        }
        PolyY::from_coeffs(self.coeffs.iter().map(|c| ratx_qshift(c, i)).collect())
    }

    pub fn eq_poly(&self, other: &PolyY) -> bool {
        self.sub(other).is_zero()
    }
}

// ---------------------------------------------------------------------------
// Q(q)(x)(y)
// ---------------------------------------------------------------------------

/// A rational function in `y` over `Q(q)(x)` — where shift quotients and the
/// certificate live, and where the final identity is checked.
#[derive(Clone, Debug)]
pub struct RatY {
    pub num: PolyY,
    pub den: PolyY,
}

impl RatY {
    pub fn zero() -> Self {
        RatY {
            num: PolyY::zero(),
            den: PolyY::one(),
        }
    }

    pub fn one() -> Self {
        RatY {
            num: PolyY::one(),
            den: PolyY::one(),
        }
    }

    pub fn from_poly(p: PolyY) -> Self {
        RatY {
            num: p,
            den: PolyY::one(),
        }
        .normalize()
    }

    pub fn from_ratx(c: RatX) -> Self {
        RatY::from_poly(PolyY::constant(c))
    }

    pub fn y() -> Self {
        RatY::from_poly(PolyY::y())
    }

    pub fn is_zero(&self) -> bool {
        self.num.is_zero()
    }

    pub fn normalize(mut self) -> Self {
        if self.num.is_zero() {
            return RatY::zero();
        }
        if self.den.is_zero() {
            return self;
        }
        if self.num.degree() > 0 && self.den.degree() > 0 {
            let g = PolyY::gcd(&self.num, &self.den);
            if g.degree() > 0 {
                if let (Some(u), Some(v)) = (
                    PolyY::exact_div(&self.num, &g),
                    PolyY::exact_div(&self.den, &g),
                ) {
                    self.num = u;
                    self.den = v;
                }
            }
        }
        if let Some(inv) = self.den.leading_coeff().inv() {
            self.num = self.num.scale(&inv);
            self.den = self.den.scale(&inv);
        }
        self
    }

    pub fn add(&self, other: &RatY) -> RatY {
        RatY {
            num: self.num.mul(&other.den).add(&other.num.mul(&self.den)),
            den: self.den.mul(&other.den),
        }
        .normalize()
    }

    pub fn neg(&self) -> RatY {
        RatY {
            num: self.num.neg(),
            den: self.den.clone(),
        }
    }

    pub fn sub(&self, other: &RatY) -> RatY {
        self.add(&other.neg())
    }

    pub fn mul(&self, other: &RatY) -> RatY {
        RatY {
            num: self.num.mul(&other.num),
            den: self.den.mul(&other.den),
        }
        .normalize()
    }

    pub fn inv(&self) -> Option<RatY> {
        if self.num.is_zero() {
            return None;
        }
        Some(
            RatY {
                num: self.den.clone(),
                den: self.num.clone(),
            }
            .normalize(),
        )
    }

    pub fn div(&self, other: &RatY) -> Option<RatY> {
        Some(self.mul(&other.inv()?))
    }

    pub fn pow_i32(&self, e: i32) -> Option<RatY> {
        if e == 0 {
            return Some(RatY::one());
        }
        let base = if e < 0 { self.inv()? } else { self.clone() };
        let mut acc = RatY::one();
        for _ in 0..e.unsigned_abs() {
            acc = acc.mul(&base);
        }
        Some(acc)
    }

    /// `r` with `y ↦ q^j·y` — the action of `k ↦ k+j`.
    pub fn qshift_y(&self, j: i64) -> RatY {
        RatY {
            num: self.num.qshift_y(j),
            den: self.den.qshift_y(j),
        }
        .normalize()
    }

    /// `r` with `x ↦ q^i·x` — the action of `n ↦ n+i`.
    pub fn qshift_x(&self, i: i64) -> RatY {
        RatY {
            num: self.num.qshift_x(i),
            den: self.den.qshift_x(i),
        }
        .normalize()
    }

    pub fn eq_raty(&self, other: &RatY) -> bool {
        self.sub(other).is_zero()
    }
}

/// `x^a·y^b·q^c` as an element of `Q(q)(x)(y)` — the image of `q^{a·n + b·k + c}`.
pub fn q_monomial(a: i64, b: i64, c: i64) -> RatY {
    let scalar = RatX::from_rn(qq_pow(c)).mul(&ratx_x_pow(a));
    let mut out = RatY::from_ratx(scalar);
    if b != 0 {
        let mut coeffs = vec![RatX::zero(); b.unsigned_abs() as usize + 1];
        coeffs[b.unsigned_abs() as usize] = RatX::one();
        let mono = RatY::from_poly(PolyY::from_coeffs(coeffs));
        let mono = if b > 0 {
            mono
        } else {
            // A monomial is never zero, so the inverse exists.
            mono.inv().unwrap_or_else(RatY::one)
        };
        out = out.mul(&mono);
    }
    out
}

// ---------------------------------------------------------------------------
// Evaluation at integer (n, k)
// ---------------------------------------------------------------------------

/// `p` at `x = q^{n₀}`.
pub fn polyx_at_qn(p: &PolyX, n0: i64) -> Qq {
    let mut acc = rn_zero();
    for (deg, c) in p.coeffs.iter().enumerate() {
        if rn_is_zero(c) {
            continue;
        }
        acc = rn_add(&acc, &rn_mul(c, &qq_pow(n0 * deg as i64)));
    }
    acc
}

/// `r` at `x = q^{n₀}`, or `None` at a pole.
pub fn ratx_at_qn(r: &RatX, n0: i64) -> Option<Qq> {
    let den = polyx_at_qn(&r.den, n0);
    if rn_is_zero(&den) {
        return None;
    }
    Some(rn_mul(&polyx_at_qn(&r.num, n0), &rn_inv(&den)?))
}

/// `p` at `x = q^{n₀}`, `y = q^{k₀}`, or `None` at a coefficient pole.
pub fn polyy_at(p: &PolyY, n0: i64, k0: i64) -> Option<Qq> {
    let mut acc = rn_zero();
    for (deg, c) in p.coeffs.iter().enumerate() {
        if c.is_zero() {
            continue;
        }
        let cv = ratx_at_qn(c, n0)?;
        acc = rn_add(&acc, &rn_mul(&cv, &qq_pow(k0 * deg as i64)));
    }
    Some(acc)
}

/// `r` at `x = q^{n₀}`, `y = q^{k₀}`, or `None` at a pole.
pub fn raty_at(r: &RatY, n0: i64, k0: i64) -> Option<Qq> {
    let den = polyy_at(&r.den, n0, k0)?;
    if rn_is_zero(&den) {
        return None;
    }
    Some(rn_mul(&polyy_at(&r.num, n0, k0)?, &rn_inv(&den)?))
}

/// Clear the denominators of a `Q(q)(x)` family, returning the numerators over
/// one common `Q(q)[x]` scale.
///
/// The scale is `k`-free, so multiplying the certificate by the same factor
/// preserves the telescoping identity exactly — which is why the search may do
/// it *before* the verification step rather than after.
pub fn clear_denominators_x(items: &[RatX]) -> (Vec<PolyX>, RatX) {
    let mut den = PolyX::one();
    for it in items {
        if it.is_zero() {
            continue;
        }
        den = PolyX::lcm(&den, &it.den);
    }
    let scale = RatX::from_poly(den);
    let out = items
        .iter()
        .map(|it| {
            let prod = it.mul(&scale);
            // Exact by construction of the lcm; fall back to the numerator
            // rather than panicking if a degenerate denominator slipped in.
            if prod.den.degree() == 0 {
                let c = prod.den.coeff(0);
                match rn_inv(&c) {
                    Some(inv) => prod.num.scale(&inv),
                    None => prod.num.clone(),
                }
            } else {
                prod.num.clone()
            }
        })
        .collect();
    (out, scale)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn qq_pow_is_a_group_homomorphism() {
        for i in -3_i64..4 {
            for j in -3_i64..4 {
                let lhs = rn_mul(&qq_pow(i), &qq_pow(j));
                let rhs = qq_pow(i + j);
                assert!(
                    super::super::super::qfield::rn_eq(&lhs, &rhs),
                    "q^{i}·q^{j} must be q^{}",
                    i + j
                );
            }
        }
    }

    #[test]
    fn y_shift_is_multiplicative_on_monomials() {
        // (y²)|_{y ↦ q y} = q²·y².
        let y2 = PolyY::y().mul(&PolyY::y());
        let shifted = y2.qshift_y(1);
        let expect = y2.scale(&RatX::from_rn(qq_pow(2)));
        assert!(shifted.eq_poly(&expect));
    }

    #[test]
    fn x_shift_acts_on_x_powers() {
        // x|_{x ↦ q^3 x} = q³·x.
        let x = ratx_x_pow(1);
        let shifted = ratx_qshift(&x, 3);
        let expect = x.mul(&RatX::from_rn(qq_pow(3)));
        assert!(shifted.eq_ratk(&expect));
    }

    #[test]
    fn raty_is_a_field_on_a_small_sample() {
        let a = RatY::from_poly(PolyY::from_coeffs(vec![
            RatX::one(),
            ratx_x_pow(1),
            RatX::from_rn(qq_pow(2)),
        ]));
        let b = RatY::from_poly(PolyY::from_coeffs(vec![ratx_x_pow(-1), RatX::one()]));
        let q = a.div(&b).expect("b != 0");
        assert!(q.mul(&b).eq_raty(&a), "division must invert multiplication");
        assert!(a.sub(&a).is_zero());
    }

    #[test]
    fn q_monomial_matches_repeated_multiplication() {
        let m = q_monomial(2, -1, 3);
        let expect = RatY::from_ratx(ratx_x_pow(2).mul(&RatX::from_rn(qq_pow(3))))
            .div(&RatY::y())
            .expect("y != 0");
        assert!(m.eq_raty(&expect));
    }

    #[test]
    fn gcd_and_exact_div_agree() {
        let f = PolyY::from_coeffs(vec![RatX::one(), RatX::one()]); // y + 1
        let g = PolyY::from_coeffs(vec![ratx_x_pow(1), RatX::one()]); // y + x
        let prod = f.mul(&g);
        let d = PolyY::gcd(&prod, &f);
        assert_eq!(d.degree(), 1);
        assert!(PolyY::exact_div(&prod, &f).expect("divides").eq_poly(&g));
    }
}