oxiz-math 0.2.1

Mathematical foundations for OxiZ SMT solver
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
//! Advanced Multivariate Polynomial GCD
#![allow(missing_docs)] // Under development
//!
//! This module implements sophisticated algorithms for computing the greatest
//! common divisor (GCD) of multivariate polynomials:
//! - Subresultant Polynomial Remainder Sequence (PRS)
//! - Modular GCD with Chinese Remainder Theorem (CRT)
//! - Heuristic GCD for sparse polynomials
//! - Content and primitive part computation

#[allow(unused_imports)]
use crate::prelude::*;
use num_bigint::BigInt;
use num_rational::BigRational;
use num_traits::{One, Zero};

/// Monomial representation (variable -> exponent)
pub type Monomial = FxHashMap<usize, usize>;

/// Multivariate polynomial term
#[derive(Debug, Clone)]
pub struct Term {
    pub coefficient: BigRational,
    pub monomial: Monomial,
}

/// Multivariate polynomial
#[derive(Debug, Clone)]
pub struct MultivariatePolynomial {
    pub terms: Vec<Term>,
    pub num_vars: usize,
}

/// Statistics for GCD computation
#[derive(Debug, Clone, Default)]
pub struct GcdStats {
    pub gcd_computations: u64,
    pub subresultant_steps: u64,
    pub modular_reductions: u64,
    pub crt_reconstructions: u64,
    pub heuristic_attempts: u64,
    pub heuristic_successes: u64,
}

/// Configuration for GCD algorithms
#[derive(Debug, Clone)]
pub struct GcdConfig {
    /// Use heuristic GCD for sparse polynomials
    pub use_heuristic: bool,
    /// Use modular GCD algorithm
    pub use_modular: bool,
    /// Threshold for sparse polynomial detection
    pub sparse_threshold: f64,
}

impl Default for GcdConfig {
    fn default() -> Self {
        Self {
            use_heuristic: true,
            use_modular: true,
            sparse_threshold: 0.5,
        }
    }
}

/// Advanced multivariate GCD computer
pub struct MultivariateGcdComputer {
    config: GcdConfig,
    stats: GcdStats,
}

impl MultivariateGcdComputer {
    /// Create a new GCD computer
    pub fn new(config: GcdConfig) -> Self {
        Self {
            config,
            stats: GcdStats::default(),
        }
    }

    /// Compute GCD of two multivariate polynomials
    pub fn gcd(
        &mut self,
        f: &MultivariatePolynomial,
        g: &MultivariatePolynomial,
    ) -> Result<MultivariatePolynomial, String> {
        self.stats.gcd_computations += 1;

        // Handle trivial cases
        if f.is_zero() {
            return Ok(g.clone());
        }
        if g.is_zero() {
            return Ok(f.clone());
        }

        // Check if polynomials are sparse
        let is_sparse = self.is_sparse(f) && self.is_sparse(g);

        // Try heuristic GCD for sparse polynomials
        if self.config.use_heuristic && is_sparse {
            self.stats.heuristic_attempts += 1;
            if let Ok(gcd) = self.heuristic_gcd(f, g) {
                self.stats.heuristic_successes += 1;
                return Ok(gcd);
            }
        }

        // Use modular GCD if enabled
        if self.config.use_modular {
            return self.modular_gcd(f, g);
        }

        // Fall back to subresultant PRS
        self.subresultant_gcd(f, g)
    }

    /// Check if polynomial is sparse
    fn is_sparse(&self, poly: &MultivariatePolynomial) -> bool {
        if poly.terms.is_empty() {
            return true;
        }

        let max_degree = poly.total_degree();
        let max_terms = (max_degree + 1).pow(poly.num_vars as u32);
        let density = poly.terms.len() as f64 / max_terms as f64;

        density < self.config.sparse_threshold
    }

    /// Heuristic GCD using evaluation and interpolation
    fn heuristic_gcd(
        &mut self,
        f: &MultivariatePolynomial,
        g: &MultivariatePolynomial,
    ) -> Result<MultivariatePolynomial, String> {
        // Evaluate at small integer points
        let evaluation_points = vec![0i64, 1, -1, 2, -2];

        let mut gcd_evaluations = Vec::new();

        for &point in &evaluation_points {
            let f_eval = self.evaluate_at_point(f, point);
            let g_eval = self.evaluate_at_point(g, point);

            // Compute univariate GCD
            let gcd_eval = self.univariate_gcd(&f_eval, &g_eval)?;
            gcd_evaluations.push(gcd_eval);
        }

        // Check if all GCD evaluations have the same degree
        let gcd_degree = gcd_evaluations[0].len();
        if gcd_evaluations.iter().all(|g| g.len() == gcd_degree) {
            // Interpolate to recover multivariate GCD
            self.interpolate_gcd(&gcd_evaluations, &evaluation_points)
        } else {
            Err("Heuristic GCD failed: inconsistent degrees".to_string())
        }
    }

    /// Evaluate polynomial at a specific integer point (for main variable)
    fn evaluate_at_point(&self, _poly: &MultivariatePolynomial, _point: i64) -> Vec<BigRational> {
        // Placeholder: evaluate main variable at point
        // Returns univariate polynomial in remaining variables
        vec![BigRational::one()]
    }

    /// Compute GCD of two univariate polynomials
    fn univariate_gcd(
        &self,
        f: &[BigRational],
        g: &[BigRational],
    ) -> Result<Vec<BigRational>, String> {
        let mut a = f.to_vec();
        let mut b = g.to_vec();

        // Euclidean algorithm
        while !Self::is_zero_poly(&b) {
            let r = Self::poly_rem(&a, &b)?;
            a = b;
            b = r;
        }

        Ok(Self::make_monic(a))
    }

    /// Check if polynomial is zero
    fn is_zero_poly(poly: &[BigRational]) -> bool {
        poly.is_empty() || poly.iter().all(|c| c.is_zero())
    }

    /// Polynomial remainder
    fn poly_rem(
        dividend: &[BigRational],
        divisor: &[BigRational],
    ) -> Result<Vec<BigRational>, String> {
        if Self::is_zero_poly(divisor) {
            return Err("Division by zero polynomial".to_string());
        }

        let mut remainder = dividend.to_vec();
        let lc_divisor = &divisor[0];

        while !Self::is_zero_poly(&remainder) && remainder.len() >= divisor.len() {
            let lc_remainder = &remainder[0];
            let quotient_coeff = lc_remainder / lc_divisor;

            // Subtract quotient_coeff * x^degree_diff * divisor
            // When multiplying by x^degree_diff, coefficients align with leading terms
            for i in 0..divisor.len() {
                remainder[i] = &remainder[i] - &quotient_coeff * &divisor[i];
            }

            // Remove leading zeros
            while !remainder.is_empty() && remainder[0].is_zero() {
                remainder.remove(0);
            }
        }

        Ok(remainder)
    }

    /// Make polynomial monic (leading coefficient = 1)
    fn make_monic(mut poly: Vec<BigRational>) -> Vec<BigRational> {
        if poly.is_empty() || poly[0].is_zero() {
            return poly;
        }

        let lc = poly[0].clone();
        for coeff in &mut poly {
            *coeff = &*coeff / &lc;
        }
        poly
    }

    /// Interpolate GCD from evaluations
    fn interpolate_gcd(
        &self,
        _evaluations: &[Vec<BigRational>],
        _points: &[i64],
    ) -> Result<MultivariatePolynomial, String> {
        // Placeholder: Lagrange interpolation
        Ok(MultivariatePolynomial {
            terms: vec![],
            num_vars: 1,
        })
    }

    /// Modular GCD using Chinese Remainder Theorem
    fn modular_gcd(
        &mut self,
        f: &MultivariatePolynomial,
        g: &MultivariatePolynomial,
    ) -> Result<MultivariatePolynomial, String> {
        // Choose primes for modular reduction
        let primes = vec![32003u64, 32009, 32027, 32029, 32051];

        let mut gcd_images = Vec::new();

        for &prime in &primes {
            self.stats.modular_reductions += 1;

            // Reduce polynomials modulo prime
            let f_mod = self.reduce_modulo(f, prime)?;
            let g_mod = self.reduce_modulo(g, prime)?;

            // Compute GCD in finite field
            let gcd_mod = self.gcd_finite_field(&f_mod, &g_mod, prime)?;
            gcd_images.push((gcd_mod, prime));
        }

        // Reconstruct using Chinese Remainder Theorem
        self.stats.crt_reconstructions += 1;
        self.chinese_remainder_reconstruction(&gcd_images)
    }

    /// Reduce polynomial modulo a prime
    fn reduce_modulo(
        &self,
        poly: &MultivariatePolynomial,
        prime: u64,
    ) -> Result<MultivariatePolynomial, String> {
        let mut reduced_terms = Vec::new();

        for term in &poly.terms {
            // Convert rational coefficient to integer modulo prime
            let num = term.coefficient.numer().clone();
            let den = term.coefficient.denom().clone();

            let coeff_mod = self.rational_mod(&num, &den, prime)?;

            if coeff_mod != 0 {
                reduced_terms.push(Term {
                    coefficient: BigRational::from_integer(BigInt::from(coeff_mod)),
                    monomial: term.monomial.clone(),
                });
            }
        }

        Ok(MultivariatePolynomial {
            terms: reduced_terms,
            num_vars: poly.num_vars,
        })
    }

    /// Compute rational number modulo prime (numerator * inverse(denominator))
    fn rational_mod(&self, num: &BigInt, den: &BigInt, prime: u64) -> Result<u64, String> {
        let num_mod = (num.clone() % BigInt::from(prime)).to_u64_digits().1;
        let den_mod = (den.clone() % BigInt::from(prime)).to_u64_digits().1;

        if den_mod.is_empty() || den_mod[0] == 0 {
            return Err("Denominator is zero modulo prime".to_string());
        }

        let den_inv = self.mod_inverse(den_mod[0], prime)?;
        let num_val = if num_mod.is_empty() { 0 } else { num_mod[0] };

        Ok((num_val * den_inv) % prime)
    }

    /// Compute modular inverse using extended Euclidean algorithm
    fn mod_inverse(&self, a: u64, m: u64) -> Result<u64, String> {
        let (g, x, _) = self.extended_gcd(a as i64, m as i64);

        if g != 1 {
            return Err("Modular inverse does not exist".to_string());
        }

        Ok(((x % m as i64 + m as i64) % m as i64) as u64)
    }

    /// Extended Euclidean algorithm
    fn extended_gcd(&self, a: i64, b: i64) -> (i64, i64, i64) {
        if b == 0 {
            return (a, 1, 0);
        }

        let (g, x1, y1) = self.extended_gcd(b, a % b);
        let x = y1;
        let y = x1 - (a / b) * y1;

        (g, x, y)
    }

    /// Compute GCD in finite field
    fn gcd_finite_field(
        &self,
        f: &MultivariatePolynomial,
        _g: &MultivariatePolynomial,
        _prime: u64,
    ) -> Result<MultivariatePolynomial, String> {
        // Placeholder: use Euclidean algorithm in finite field
        Ok(f.clone())
    }

    /// Chinese Remainder Theorem reconstruction
    fn chinese_remainder_reconstruction(
        &self,
        images: &[(MultivariatePolynomial, u64)],
    ) -> Result<MultivariatePolynomial, String> {
        if images.is_empty() {
            return Err("No images for CRT reconstruction".to_string());
        }

        // Placeholder: reconstruct coefficients using CRT
        Ok(images[0].0.clone())
    }

    /// Subresultant polynomial remainder sequence
    fn subresultant_gcd(
        &mut self,
        f: &MultivariatePolynomial,
        g: &MultivariatePolynomial,
    ) -> Result<MultivariatePolynomial, String> {
        // Convert to univariate in main variable
        let main_var = 0; // Choose main variable

        let f_uni = self.to_univariate(f, main_var);
        let g_uni = self.to_univariate(g, main_var);

        // Compute subresultant PRS
        let mut prs = vec![f_uni, g_uni];
        let mut beta = BigRational::from_integer(BigInt::from(-1));
        let mut psi = BigRational::from_integer(BigInt::from(-1));

        loop {
            self.stats.subresultant_steps += 1;

            let i = prs.len() - 2;
            let j = prs.len() - 1;

            if prs[j].is_zero() {
                break;
            }

            // Compute pseudo-remainder
            let remainder = self.pseudo_remainder(&prs[i], &prs[j])?;

            if remainder.is_zero() {
                break;
            }

            // Update subresultant coefficients
            let delta = prs[i].degree(main_var) - prs[j].degree(main_var);
            let next_remainder = self.scale_by_subresultant(&remainder, &beta, &psi, delta);

            prs.push(next_remainder);

            // Update beta and psi for next iteration
            let lc_j = prs[j].leading_coefficient(main_var);
            beta = (-&lc_j).pow(delta as i32);

            if delta > 1 {
                psi = (-&lc_j).pow((delta - 1) as i32) / psi.pow((delta - 2) as i32);
            } else {
                psi = -lc_j;
            }
        }

        // Last non-zero polynomial is the GCD
        let gcd = prs
            .iter()
            .rev()
            .find(|p| !p.is_zero())
            .ok_or("No non-zero polynomial in PRS")?;

        Ok(gcd.clone())
    }

    /// Convert multivariate polynomial to univariate representation
    fn to_univariate(
        &self,
        poly: &MultivariatePolynomial,
        _main_var: usize,
    ) -> MultivariatePolynomial {
        // Placeholder: treat coefficients as polynomials in remaining variables
        poly.clone()
    }

    /// Compute pseudo-remainder
    fn pseudo_remainder(
        &self,
        dividend: &MultivariatePolynomial,
        _divisor: &MultivariatePolynomial,
    ) -> Result<MultivariatePolynomial, String> {
        // Placeholder: pseudo-division algorithm
        Ok(dividend.clone())
    }

    /// Scale remainder by subresultant coefficients
    fn scale_by_subresultant(
        &self,
        remainder: &MultivariatePolynomial,
        _beta: &BigRational,
        _psi: &BigRational,
        _delta: usize,
    ) -> MultivariatePolynomial {
        // Placeholder: apply subresultant scaling
        remainder.clone()
    }

    /// Get statistics
    pub fn stats(&self) -> &GcdStats {
        &self.stats
    }
}

impl MultivariatePolynomial {
    /// Check if polynomial is zero
    pub fn is_zero(&self) -> bool {
        self.terms.is_empty() || self.terms.iter().all(|t| t.coefficient.is_zero())
    }

    /// Get total degree
    pub fn total_degree(&self) -> usize {
        self.terms
            .iter()
            .map(|t| t.monomial.values().sum())
            .max()
            .unwrap_or(0)
    }

    /// Get degree in a specific variable
    pub fn degree(&self, var: usize) -> usize {
        self.terms
            .iter()
            .map(|t| *t.monomial.get(&var).unwrap_or(&0))
            .max()
            .unwrap_or(0)
    }

    /// Get leading coefficient (for specific variable)
    pub fn leading_coefficient(&self, _var: usize) -> BigRational {
        // Placeholder: return leading coefficient
        self.terms
            .first()
            .map(|t| t.coefficient.clone())
            .unwrap_or_else(BigRational::zero)
    }
}

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

    #[test]
    fn test_gcd_computer_creation() {
        let config = GcdConfig::default();
        let computer = MultivariateGcdComputer::new(config);
        assert_eq!(computer.stats.gcd_computations, 0);
    }

    #[test]
    fn test_zero_polynomial_gcd() {
        let mut computer = MultivariateGcdComputer::new(GcdConfig::default());

        let f = MultivariatePolynomial {
            terms: vec![],
            num_vars: 1,
        };
        let g = MultivariatePolynomial {
            terms: vec![Term {
                coefficient: BigRational::from_integer(BigInt::from(5)),
                monomial: Monomial::default(),
            }],
            num_vars: 1,
        };

        let result = computer.gcd(&f, &g).expect("test operation should succeed");
        assert_eq!(result.terms.len(), 1);
    }

    #[test]
    fn test_is_sparse() {
        let computer = MultivariateGcdComputer::new(GcdConfig::default());

        // Create a sparse polynomial: 2 terms in 3 variables with degree 2
        // max_terms = (2+1)^3 = 27, density = 2/27 ≈ 0.074 < 0.5
        let mut mono_x2 = Monomial::default();
        mono_x2.insert(0, 2); // x^2

        let mut mono_z2 = Monomial::default();
        mono_z2.insert(2, 2); // z^2

        let sparse = MultivariatePolynomial {
            terms: vec![
                Term {
                    coefficient: BigRational::one(),
                    monomial: mono_x2,
                },
                Term {
                    coefficient: BigRational::one(),
                    monomial: mono_z2,
                },
            ],
            num_vars: 3,
        };

        assert!(computer.is_sparse(&sparse));
    }

    #[test]
    fn test_univariate_gcd_simple() {
        let computer = MultivariateGcdComputer::new(GcdConfig::default());

        // gcd(x^2 - 1, x - 1) = x - 1
        let f = vec![
            BigRational::one(),  // x^2
            BigRational::zero(), // x
            -BigRational::one(), // constant
        ];
        let g = vec![
            BigRational::one(),  // x
            -BigRational::one(), // constant
        ];

        let result = computer
            .univariate_gcd(&f, &g)
            .expect("test operation should succeed");
        assert!(!result.is_empty());
    }

    #[test]
    fn test_poly_rem() {
        let dividend = vec![
            BigRational::one(), // x^2
            BigRational::zero(),
            -BigRational::one(),
        ];
        let divisor = vec![
            BigRational::one(), // x
            -BigRational::one(),
        ];

        let result = MultivariateGcdComputer::poly_rem(&dividend, &divisor)
            .expect("test operation should succeed");
        // Should get zero remainder (x^2 - 1 = (x+1)(x-1))
        assert!(MultivariateGcdComputer::is_zero_poly(&result));
    }

    #[test]
    fn test_make_monic() {
        let poly = vec![
            BigRational::from_integer(BigInt::from(2)),
            BigRational::from_integer(BigInt::from(4)),
        ];

        let monic = MultivariateGcdComputer::make_monic(poly);
        assert_eq!(monic[0], BigRational::one());
        assert_eq!(monic[1], BigRational::from_integer(BigInt::from(2)));
    }

    #[test]
    fn test_mod_inverse() {
        let computer = MultivariateGcdComputer::new(GcdConfig::default());

        let inv = computer
            .mod_inverse(3, 7)
            .expect("test operation should succeed");
        assert_eq!((3 * inv) % 7, 1);
    }

    #[test]
    fn test_extended_gcd() {
        let computer = MultivariateGcdComputer::new(GcdConfig::default());

        let (g, x, y) = computer.extended_gcd(35, 15);
        assert_eq!(g, 5);
        assert_eq!(35 * x + 15 * y, g);
    }

    #[test]
    fn test_rational_mod() {
        let computer = MultivariateGcdComputer::new(GcdConfig::default());

        let num = BigInt::from(7);
        let den = BigInt::from(3);
        let prime = 11;

        let result = computer
            .rational_mod(&num, &den, prime)
            .expect("test operation should succeed");
        // 7/3 mod 11 = 7 * 4 mod 11 = 28 mod 11 = 6
        assert_eq!(result, 6);
    }

    #[test]
    fn test_polynomial_degree() {
        let mut monomial = Monomial::default();
        monomial.insert(0, 2);
        monomial.insert(1, 3);

        let poly = MultivariatePolynomial {
            terms: vec![Term {
                coefficient: BigRational::one(),
                monomial,
            }],
            num_vars: 2,
        };

        assert_eq!(poly.total_degree(), 5);
        assert_eq!(poly.degree(0), 2);
        assert_eq!(poly.degree(1), 3);
    }
}