ocas-calc 0.23.0

Calculus and equation solving for oCAS
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
//! Elements of the differential fields in an extension tower.
//!
//! A tower field `ℚ(x, t₁, …, tₙ)` is represented in flat multivariate
//! form: an element of the field is a [`KElem`] (a numerator/denominator
//! pair of sparse polynomials over `ℚ`), and a univariate polynomial over
//! the top generator is a [`KPoly`] (dense coefficient vector in the top
//! variable). [`KRat`] is a rational function in the top generator.
//!
//! No multivariate GCD reduction is performed on [`KElem`]; only zero
//! detection (via the numerator) and cross-multiplied equality are
//! reliable. [`KPoly`] carries a genuine Euclidean algorithm because the
//! coefficient field is exact.

use ocas_domain::{Domain, Rational, RationalDomain};
use ocas_poly::{Lex, SparseMultivariatePolynomial};

pub(crate) type Sparse = SparseMultivariatePolynomial<RationalDomain, Lex>;

fn sparse_one(n: usize) -> Sparse {
    Sparse::new(RationalDomain, n).one()
}

/// If `num = c·den` as polynomials for a rational `c`, return `c`.
///
/// Works on unreduced fractions by comparing supports and per-term
/// coefficient ratios.
pub(crate) fn ratio_constant(num: &Sparse, den: &Sparse) -> Option<Rational> {
    let dom = RationalDomain;
    if num.is_zero() {
        return Some(dom.zero());
    }
    let nt = num.terms_ref();
    let dt = den.terms_ref();
    if nt.len() != dt.len() {
        return None;
    }
    let mut out = None;
    for (exp, nc) in nt {
        let dc = dt.get(exp)?;
        let ratio = dom.div(nc, dc)?;
        match &out {
            None => out = Some(ratio),
            Some(c) if *c == ratio => {}
            _ => return None,
        }
    }
    out
}

/// Cancel a shared monomial factor between `num` and `den`
/// (`a·tᵏ / (b·tᵏ) → a/b` for monomial-free leftovers).
///
/// Only handles the case where `den` is a single term; returns
/// `(num/den, one)` when every num term dominates the den exponent.
pub(crate) fn cancel_monomial(num: &Sparse, den: &Sparse) -> Option<(Sparse, Sparse)> {
    let dom = RationalDomain;
    if den.terms_ref().len() != 1 {
        return None;
    }
    let (d_exp, d_coeff) = den.terms_ref().iter().next().unwrap();
    let mut out_terms = Vec::new();
    for (n_exp, n_coeff) in num.terms_ref() {
        if n_exp.iter().zip(d_exp.iter()).any(|(a, b)| a < b) {
            return None;
        }
        let new_exp: Vec<usize> = n_exp.iter().zip(d_exp.iter()).map(|(a, b)| a - b).collect();
        out_terms.push((new_exp, dom.div(n_coeff, d_coeff)?));
    }
    let n = Sparse::from_terms(RationalDomain, num.n_vars(), out_terms);
    let one = num.one();
    Some((n, one))
}

/// Formal partial derivative of a sparse polynomial w.r.t. variable `var`.
pub(crate) fn sparse_partial_deriv(p: &Sparse, var: usize) -> Sparse {
    let dom = RationalDomain;
    let mut terms = Vec::new();
    for (exp, c) in p.terms_ref() {
        let e = exp[var];
        if e > 0 {
            let mut ne = exp.to_vec();
            ne[var] -= 1;
            terms.push((ne, dom.mul(c, &Rational::new(e as i64, 1))));
        }
    }
    Sparse::from_terms(RationalDomain, p.n_vars(), terms)
}

// ------------------------------------------------------------------
//  KElem: element of k = ℚ(x, t₁, …, t_{n-1})
// ------------------------------------------------------------------

/// An element of the coefficient field as a multivariate rational function.
#[derive(Clone, Debug)]
pub(crate) struct KElem {
    /// Numerator.
    pub num: Sparse,
    /// Denominator (never zero).
    pub den: Sparse,
}

impl KElem {
    /// Create without reduction. `den` must be non-zero.
    pub fn new(num: Sparse, den: Sparse) -> Self {
        debug_assert!(!den.is_zero());
        Self { num, den }
    }

    /// Create and reduce by cancelling a constant-support ratio
    /// (`num = c·den`) or a shared monomial factor (`a·tᵏ / b·tᵏ`).
    ///
    /// This is a best-effort normalizer: without a multivariate GCD the
    /// fraction is not fully reduced, but the common cases produced by
    /// tower arithmetic (quotients like `t/t`, cross-multiplied sums)
    /// collapse to their minimal form.
    pub fn reduced(num: Sparse, den: Sparse) -> Self {
        debug_assert!(!den.is_zero());
        if let Some(c) = ratio_constant(&num, &den) {
            let one = den.one();
            let const_poly = Sparse::from_terms(
                RationalDomain,
                den.n_vars(),
                vec![(vec![0; den.n_vars()], c)],
            );
            return Self {
                num: const_poly,
                den: one,
            };
        }
        if let Some((n2, d2)) = cancel_monomial(&num, &den) {
            return Self { num: n2, den: d2 };
        }
        Self { num, den }
    }

    pub fn zero(n: usize) -> Self {
        Self::new(Sparse::new(RationalDomain, n), sparse_one(n))
    }

    pub fn one(n: usize) -> Self {
        Self::new(sparse_one(n), sparse_one(n))
    }

    pub fn from_poly(p: Sparse) -> Self {
        let one = p.one();
        Self::new(p, one)
    }

    pub fn from_rational(r: &Rational, n: usize) -> Self {
        Self::from_poly(Sparse::from_terms(
            RationalDomain,
            n,
            vec![(vec![0; n], r.clone())],
        ))
    }

    /// The variable with the given index as a field element.
    pub fn var(idx: usize, n: usize) -> Self {
        let mut e = vec![0; n];
        e[idx] = 1;
        Self::from_poly(Sparse::from_terms(
            RationalDomain,
            n,
            vec![(e, RationalDomain.one())],
        ))
    }

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

    pub fn n_vars(&self) -> usize {
        self.num.n_vars()
    }

    /// Cross-multiplied equality (reliable without GCD reduction).
    pub fn eq_cross(&self, other: &Self) -> bool {
        self.num.mul(&other.den) == other.num.mul(&self.den)
    }

    /// If this element equals a rational constant `c` (i.e. `num = c·den`
    /// as polynomials), return it. Works on unreduced fractions by
    /// comparing supports and per-term ratios.
    pub fn as_rational(&self) -> Option<Rational> {
        ratio_constant(&self.num, &self.den)
    }

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

    pub fn add(&self, o: &Self) -> Self {
        if self.is_zero() {
            return o.clone();
        }
        if o.is_zero() {
            return self.clone();
        }
        if self.den == o.den {
            return Self::reduced(self.num.add(&o.num), self.den.clone());
        }
        Self::reduced(
            self.num.mul(&o.den).add(&o.num.mul(&self.den)),
            self.den.mul(&o.den),
        )
    }

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

    pub fn mul(&self, o: &Self) -> Self {
        if self.is_zero() || o.is_zero() {
            return Self::zero(self.n_vars());
        }
        Self::reduced(self.num.mul(&o.num), self.den.mul(&o.den))
    }

    pub fn inv(&self) -> Option<Self> {
        if self.is_zero() {
            None
        } else {
            Some(Self::new(self.den.clone(), self.num.clone()))
        }
    }

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

    pub fn mul_rational(&self, r: &Rational) -> Self {
        Self::new(self.num.mul_scalar(r), self.den.clone())
    }

    #[allow(dead_code)] // used by unit tests
    pub fn pow(&self, mut k: u64) -> Self {
        let mut result = Self::one(self.n_vars());
        let mut base = self.clone();
        while k > 0 {
            if k & 1 == 1 {
                result = result.mul(&base);
            }
            base = base.mul(&base);
            k >>= 1;
        }
        result
    }

    /// Formal partial derivative w.r.t. variable `var` (quotient rule).
    pub fn partial_deriv(&self, var: usize) -> Self {
        let a = &self.num;
        let b = &self.den;
        let av = sparse_partial_deriv(a, var);
        let bv = sparse_partial_deriv(b, var);
        Self::new(av.mul(b).add(&a.mul(&bv).neg()), b.mul(b))
    }
}

// ------------------------------------------------------------------
//  KPoly: univariate polynomial over k in the top variable
// ------------------------------------------------------------------

/// A dense univariate polynomial over the coefficient field `k`.
#[derive(Clone, Debug)]
pub(crate) struct KPoly {
    /// Index of the polynomial variable (the top generator of the level).
    pub top: usize,
    /// Coefficients by ascending degree; trailing zeros are trimmed.
    pub coeffs: Vec<KElem>,
    /// Total number of variables of the underlying multivariate ring.
    pub n_vars: usize,
}

impl KPoly {
    fn trim(&mut self) {
        while let Some(last) = self.coeffs.last() {
            if last.is_zero() {
                self.coeffs.pop();
            } else {
                break;
            }
        }
    }

    pub fn zero(top: usize, n: usize) -> Self {
        Self {
            top,
            coeffs: Vec::new(),
            n_vars: n,
        }
    }

    pub fn one(top: usize, n: usize) -> Self {
        Self {
            top,
            coeffs: vec![KElem::one(n)],
            n_vars: n,
        }
    }

    /// A constant polynomial (degree 0 in the top variable).
    pub fn from_kelem(c: KElem, top: usize) -> Self {
        let n = c.n_vars();
        if c.is_zero() {
            return Self::zero(top, n);
        }
        Self {
            top,
            coeffs: vec![c],
            n_vars: n,
        }
    }

    /// View a sparse polynomial as a univariate polynomial in `top`.
    pub fn from_sparse(p: &Sparse, top: usize) -> Self {
        let n = p.n_vars();
        let deg = p.degree_in(top);
        let mut buckets: Vec<Vec<(Vec<usize>, Rational)>> = vec![Vec::new(); deg + 1];
        for (exp, c) in p.terms_ref() {
            let mut e = exp.to_vec();
            let d = e[top];
            e[top] = 0;
            buckets[d].push((e, c.clone()));
        }
        let coeffs = buckets
            .into_iter()
            .map(|terms| KElem::from_poly(Sparse::from_terms(RationalDomain, n, terms)))
            .collect();
        let mut r = Self {
            top,
            coeffs,
            n_vars: n,
        };
        r.trim();
        r
    }

    /// Collapse to a single field element via Horner-style summation.
    pub fn kelem(&self) -> KElem {
        let mut acc = KElem::zero(self.n_vars);
        let t = KElem::var(self.top, self.n_vars);
        let mut tp = KElem::one(self.n_vars);
        for c in &self.coeffs {
            acc = acc.add(&c.mul(&tp));
            tp = tp.mul(&t);
        }
        acc
    }

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

    pub fn degree(&self) -> Option<usize> {
        self.coeffs.len().checked_sub(1)
    }

    pub fn lc(&self) -> KElem {
        self.coeffs
            .last()
            .cloned()
            .unwrap_or_else(|| KElem::zero(self.n_vars))
    }

    pub fn is_one(&self) -> bool {
        self.degree() == Some(0) && self.coeffs[0].eq_cross(&KElem::one(self.n_vars))
    }

    /// Coefficient of `t^i`, or zero.
    pub fn coeff_at(&self, i: usize) -> KElem {
        self.coeffs
            .get(i)
            .cloned()
            .unwrap_or_else(|| KElem::zero(self.n_vars))
    }

    pub fn neg(&self) -> Self {
        Self {
            top: self.top,
            coeffs: self.coeffs.iter().map(|c| c.neg()).collect(),
            n_vars: self.n_vars,
        }
    }

    pub fn add(&self, o: &Self) -> Self {
        debug_assert_eq!(self.top, o.top);
        let len = self.coeffs.len().max(o.coeffs.len());
        let mut coeffs = Vec::with_capacity(len);
        for i in 0..len {
            coeffs.push(self.coeff_at(i).add(&o.coeff_at(i)));
        }
        let mut r = Self {
            top: self.top,
            coeffs,
            n_vars: self.n_vars,
        };
        r.trim();
        r
    }

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

    pub fn mul(&self, o: &Self) -> Self {
        debug_assert_eq!(self.top, o.top);
        if self.is_zero() || o.is_zero() {
            return Self::zero(self.top, self.n_vars);
        }
        let mut coeffs = vec![KElem::zero(self.n_vars); self.coeffs.len() + o.coeffs.len() - 1];
        for (i, a) in self.coeffs.iter().enumerate() {
            for (j, b) in o.coeffs.iter().enumerate() {
                coeffs[i + j] = coeffs[i + j].add(&a.mul(b));
            }
        }
        let mut r = Self {
            top: self.top,
            coeffs,
            n_vars: self.n_vars,
        };
        r.trim();
        r
    }

    pub fn mul_kelem(&self, c: &KElem) -> Self {
        let mut r = Self {
            top: self.top,
            coeffs: self.coeffs.iter().map(|a| a.mul(c)).collect(),
            n_vars: self.n_vars,
        };
        r.trim();
        r
    }

    /// `c · t^k · self`.
    pub fn monomial_shift(&self, c: &KElem, k: usize) -> Self {
        if self.is_zero() || c.is_zero() {
            return Self::zero(self.top, self.n_vars);
        }
        let mut coeffs = vec![KElem::zero(self.n_vars); k];
        coeffs.extend(self.coeffs.iter().map(|a| a.mul(c)));
        Self {
            top: self.top,
            coeffs,
            n_vars: self.n_vars,
        }
    }

    /// Formal derivative w.r.t. the polynomial variable `t`.
    pub fn derivative_dt(&self) -> Self {
        if self.coeffs.len() <= 1 {
            return Self::zero(self.top, self.n_vars);
        }
        let coeffs = self
            .coeffs
            .iter()
            .enumerate()
            .skip(1)
            .map(|(i, c)| c.mul_rational(&Rational::new(i as i64, 1)))
            .collect();
        Self {
            top: self.top,
            coeffs,
            n_vars: self.n_vars,
        }
    }

    /// Exact division over the field `k`: returns `(quotient, remainder)`.
    pub fn div_rem(&self, d: &Self) -> (Self, Self) {
        assert!(!d.is_zero(), "KPoly::div_rem: division by zero");
        let mut q = Self::zero(self.top, self.n_vars);
        let mut r = self.clone();
        let d_lc = d.lc();
        let d_deg = match d.degree() {
            Some(v) => v,
            None => unreachable!("non-zero polynomial has a degree"),
        };
        while let Some(r_deg) = r.degree() {
            if r_deg < d_deg {
                break;
            }
            let k = r_deg - d_deg;
            let c = r.lc().div(&d_lc).expect("field division");
            // q += c·t^k
            if q.coeffs.len() <= k {
                q.coeffs.resize(k + 1, KElem::zero(self.n_vars));
            }
            q.coeffs[k] = q.coeffs[k].add(&c);
            r = r.sub(&d.monomial_shift(&c, k));
        }
        q.trim();
        (q, r)
    }

    /// Monic GCD over `k[t]` (Euclidean algorithm).
    pub fn gcd(&self, o: &Self) -> Self {
        let mut a = self.clone();
        let mut b = o.clone();
        while !b.is_zero() {
            let (_, r) = a.div_rem(&b);
            a = b;
            b = r;
        }
        if a.is_zero() {
            return a;
        }
        a.monic()
    }

    /// Divide by the leading coefficient (no-op for zero).
    pub fn monic(&self) -> Self {
        if self.is_zero() {
            return self.clone();
        }
        let inv = self.lc().inv().expect("non-zero leading coefficient");
        self.mul_kelem(&inv)
    }

    /// Extended GCD: returns `(g, s, t)` with `s·self + t·other = g` monic.
    pub fn eea(&self, other: &Self) -> (Self, Self, Self) {
        let mut old_r = self.clone();
        let mut r = other.clone();
        let mut old_s = Self::one(self.top, self.n_vars);
        let mut s = Self::zero(self.top, self.n_vars);
        let mut old_t = Self::zero(self.top, self.n_vars);
        let mut t = Self::one(self.top, self.n_vars);
        while !r.is_zero() {
            let (q, rem) = old_r.div_rem(&r);
            old_r = r;
            r = rem;
            let new_s = old_s.sub(&q.mul(&s));
            old_s = s;
            s = new_s;
            let new_t = old_t.sub(&q.mul(&t));
            old_t = t;
            t = new_t;
        }
        if !old_r.is_zero() {
            let inv = old_r.lc().inv().expect("non-zero leading coefficient");
            old_r = old_r.mul_kelem(&inv);
            old_s = old_s.mul_kelem(&inv);
            old_t = old_t.mul_kelem(&inv);
        }
        (old_r, old_s, old_t)
    }

    /// Yun's square-free factorization over `k[t]`.
    pub fn square_free(&self) -> Vec<(Self, usize)> {
        let mut out = Vec::new();
        if self.is_zero() {
            return out;
        }
        let f = self.monic();
        let df = f.derivative_dt();
        let mut g = f.gcd(&df);
        if g.is_zero() {
            return out;
        }
        let (mut w, _) = f.div_rem(&g);
        let mut k = 1;
        while !w.is_one() && !w.is_zero() {
            let h = w.gcd(&g);
            let (z, _) = w.div_rem(&h);
            if !z.is_one() && !z.is_zero() {
                out.push((z, k));
            }
            let (new_g, _) = g.div_rem(&h);
            g = new_g;
            w = h;
            k += 1;
        }
        out
    }
}

// ------------------------------------------------------------------
//  KRat: rational function in the top variable
// ------------------------------------------------------------------

/// A rational function `num / den` in the top generator, kept coprime with
/// a monic denominator.
#[derive(Clone, Debug)]
pub(crate) struct KRat {
    /// Numerator.
    pub num: KPoly,
    /// Denominator (monic, non-zero).
    pub den: KPoly,
}

impl KRat {
    /// Create and reduce to coprime numerator/denominator.
    pub fn new(num: KPoly, den: KPoly) -> Self {
        assert!(!den.is_zero(), "KRat: zero denominator");
        if num.is_zero() {
            let one = KPoly::one(den.top, den.n_vars);
            return Self { num, den: one };
        }
        let g = num.gcd(&den);
        let (n, _) = num.div_rem(&g);
        let (d, _) = den.div_rem(&g);
        let lc_inv = d.lc().inv().expect("non-zero denominator lc");
        Self {
            num: n.mul_kelem(&lc_inv),
            den: d.mul_kelem(&lc_inv),
        }
    }

    /// Collapse to a single coefficient-field element.
    pub fn kelem(&self) -> KElem {
        self.num
            .kelem()
            .div(&self.den.kelem())
            .expect("non-zero denominator")
    }
}

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

    fn rat(p: i64, q: i64) -> Rational {
        Rational::new(p, q)
    }

    /// (t - c) as a KPoly with top variable `top` and constant c.
    fn linear(top: usize, n: usize, c: i64) -> KPoly {
        KPoly {
            top,
            coeffs: vec![KElem::from_rational(&rat(-c, 1), n), KElem::one(n)],
            n_vars: n,
        }
    }

    #[test]
    fn sparse_mul_exp_add() {
        // (x + 1)(x + 2) = x^2 + 3x + 2 in n_vars=2 (y unused).
        let dom = RationalDomain;
        let p = Sparse::from_terms(
            dom,
            2,
            vec![(vec![1, 0], rat(1, 1)), (vec![0, 0], rat(1, 1))],
        );
        let q = Sparse::from_terms(
            dom,
            2,
            vec![(vec![1, 0], rat(1, 1)), (vec![0, 0], rat(2, 1))],
        );
        let r = p.mul(&q);
        assert_eq!(r.n_terms(), 3);
        assert_eq!(r.coeff(&[2, 0]), rat(1, 1));
        assert_eq!(r.coeff(&[1, 0]), rat(3, 1));
        assert_eq!(r.coeff(&[0, 0]), rat(2, 1));
    }

    #[test]
    fn kelem_arithmetic() {
        let n = 2;
        let x = KElem::var(0, n);
        let t = KElem::var(1, n);
        // (x/t) + (t/x) = (x^2 + t^2)/(x·t)
        let sum = x.div(&t).unwrap().add(&t.div(&x).unwrap());
        let expect_num = x.mul(&x).add(&t.mul(&t));
        let expect = expect_num.div(&x.mul(&t)).unwrap();
        assert!(sum.eq_cross(&expect));
        // inv
        assert!(
            x.div(&t)
                .unwrap()
                .mul(&t.div(&x).unwrap())
                .eq_cross(&KElem::one(n))
        );
    }

    #[test]
    fn kelem_partial_deriv() {
        let n = 2;
        // f = t^2 / x; df/dx = -t^2/x^2; df/dt = 2t/x
        let x = KElem::var(0, n);
        let t = KElem::var(1, n);
        let f = t.pow(2).div(&x).unwrap();
        let dfdx = f.partial_deriv(0);
        assert!(dfdx.eq_cross(&t.pow(2).div(&x.pow(2)).unwrap().neg()));
        let dfdt = f.partial_deriv(1);
        let two_t = t.mul_rational(&rat(2, 1));
        assert!(dfdt.eq_cross(&two_t.div(&x).unwrap()));
    }

    #[test]
    fn kpoly_gcd_clean_exponents() {
        // gcd(t + x, 1) over n_vars=2, top=1: monic 1, no garbage exponents.
        let n = 2;
        let a = KPoly {
            top: 1,
            coeffs: vec![KElem::var(0, n), KElem::one(n)],
            n_vars: n,
        };
        let b = KPoly::one(1, n);
        let g = a.gcd(&b);
        assert!(g.is_one());
        for c in &g.coeffs {
            for e in c.num.terms_ref().keys() {
                assert!(e.len() <= n, "garbage exponent {e:?} in num");
            }
        }
    }

    #[test]
    fn kpoly_div_rem() {
        // (t^2 - 1) / (t - 1) = (t + 1), remainder 0
        let n = 2;
        let a = linear(1, n, 1).mul(&linear(1, n, -1));
        let b = linear(1, n, 1);
        let (q, r) = a.div_rem(&b);
        assert!(r.is_zero());
        assert!(q.kelem().eq_cross(&linear(1, n, -1).kelem()));
    }

    #[test]
    fn kpoly_gcd() {
        // gcd(t^2 - 1, t^2 - 2t + 1) = t - 1
        let n = 2;
        let a = linear(1, n, 1).mul(&linear(1, n, -1));
        let b = linear(1, n, 1).mul(&linear(1, n, 1));
        let g = a.gcd(&b);
        assert!(g.kelem().eq_cross(&linear(1, n, 1).kelem()));
    }

    #[test]
    fn kpoly_square_free() {
        // (t-1)^2 (t-2) → [(t-1, 2), (t-2, 1)]
        let n = 2;
        let f = linear(1, n, 1).mul(&linear(1, n, 1)).mul(&linear(1, n, 2));
        let sf = f.square_free();
        assert_eq!(sf.len(), 2);
        let mut by_mult: Vec<(usize, KElem)> = sf.iter().map(|(p, k)| (*k, p.kelem())).collect();
        by_mult.sort_by_key(|(k, _)| *k);
        assert_eq!(by_mult[0].0, 1);
        assert!(by_mult[0].1.eq_cross(&linear(1, n, 2).kelem()));
        assert_eq!(by_mult[1].0, 2);
        assert!(by_mult[1].1.eq_cross(&linear(1, n, 1).kelem()));
    }

    #[test]
    fn kpoly_eea() {
        // s·(t-1) + t·(t+1) = 1
        let n = 2;
        let a = linear(1, n, 1);
        let b = linear(1, n, -1);
        let (g, s, tt) = a.eea(&b);
        assert!(g.is_one());
        let check = s.mul(&a).add(&tt.mul(&b));
        assert!(check.is_one());
    }

    #[test]
    fn from_sparse_roundtrip() {
        // p = x·t + t^2 over n=2, top=1
        let n = 2;
        let dom = RationalDomain;
        let p = Sparse::from_terms(
            dom,
            n,
            vec![(vec![1, 1], rat(1, 1)), (vec![0, 2], rat(3, 1))],
        );
        let kp = KPoly::from_sparse(&p, 1);
        assert_eq!(kp.degree(), Some(2));
        // coeffs: [0, x, 3]
        assert!(kp.coeffs[1].eq_cross(&KElem::var(0, n)));
        assert!(kp.coeffs[2].eq_cross(&KElem::from_rational(&rat(3, 1), n)));
    }
}