oxiz-math 0.2.2

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
//! Type definitions for polynomial arithmetic.
//!
//! This module contains the core types used in polynomial representation:
//! - VarPower: Variable with exponent
//! - Monomial: Product of variables with exponents
//! - Term: Coefficient multiplied by a monomial
//! - MonomialOrder: Ordering for polynomial canonicalization

#[allow(unused_imports)]
use crate::prelude::*;
use core::cmp::Ordering;
use core::fmt;
use core::hash::{Hash, Hasher};
use num_rational::BigRational;
use num_traits::{One, Zero};
use smallvec::SmallVec;

/// Variable identifier for polynomials.
pub type Var = u32;

/// Null variable constant (indicates no variable).
pub const NULL_VAR: Var = u32::MAX;

/// Power of a variable (variable, exponent).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct VarPower {
    /// The variable identifier.
    pub var: Var,
    /// The exponent (power) of the variable.
    pub power: u32,
}

impl VarPower {
    /// Create a new variable power.
    #[inline]
    pub fn new(var: Var, power: u32) -> Self {
        Self { var, power }
    }
}

impl PartialOrd for VarPower {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for VarPower {
    fn cmp(&self, other: &Self) -> Ordering {
        self.var.cmp(&other.var)
    }
}

/// A monomial is a product of variables with exponents.
/// Represented as a sorted list of (variable, power) pairs.
/// The unit monomial (1) is represented as an empty list.
#[derive(Clone, PartialEq, Eq)]
pub struct Monomial {
    /// Variables with their exponents, sorted by variable index.
    pub(crate) vars: SmallVec<[VarPower; 4]>,
    /// Cached total degree.
    pub(crate) total_degree: u32,
    /// Cached hash value.
    pub(crate) hash: u64,
}

impl Monomial {
    /// Create the unit monomial (1).
    #[inline]
    pub fn unit() -> Self {
        Self {
            vars: SmallVec::new(),
            total_degree: 0,
            hash: 0,
        }
    }

    /// Create a monomial from a single variable with power 1.
    #[inline]
    pub fn from_var(var: Var) -> Self {
        Self::from_var_power(var, 1)
    }

    /// Create a monomial from a single variable with a given power.
    pub fn from_var_power(var: Var, power: u32) -> Self {
        if power == 0 {
            return Self::unit();
        }
        let mut vars = SmallVec::new();
        vars.push(VarPower::new(var, power));
        Self {
            total_degree: power,
            hash: compute_monomial_hash(&vars),
            vars,
        }
    }

    /// Create a monomial from a list of (variable, power) pairs.
    /// The input doesn't need to be sorted or normalized.
    pub fn from_powers(powers: impl IntoIterator<Item = (Var, u32)>) -> Self {
        let mut var_powers: FxHashMap<Var, u32> = FxHashMap::default();
        for (var, power) in powers {
            if power > 0 {
                *var_powers.entry(var).or_insert(0) += power;
            }
        }

        let mut vars: SmallVec<[VarPower; 4]> = var_powers
            .into_iter()
            .filter(|(_, p)| *p > 0)
            .map(|(v, p)| VarPower::new(v, p))
            .collect();
        vars.sort_by_key(|vp| vp.var);

        let total_degree = vars.iter().map(|vp| vp.power).sum();
        let hash = compute_monomial_hash(&vars);

        Self {
            vars,
            total_degree,
            hash,
        }
    }

    /// Returns true if this is the unit monomial.
    #[inline]
    pub fn is_unit(&self) -> bool {
        self.vars.is_empty()
    }

    /// Returns the total degree of the monomial.
    #[inline]
    pub fn total_degree(&self) -> u32 {
        self.total_degree
    }

    /// Returns the number of variables in the monomial.
    #[inline]
    pub fn num_vars(&self) -> usize {
        self.vars.len()
    }

    /// Returns the variable-power pairs.
    #[inline]
    pub fn vars(&self) -> &[VarPower] {
        &self.vars
    }

    /// Returns the degree of a specific variable in this monomial.
    pub fn degree(&self, var: Var) -> u32 {
        self.vars
            .iter()
            .find(|vp| vp.var == var)
            .map(|vp| vp.power)
            .unwrap_or(0)
    }

    /// Returns the maximum variable in this monomial, or NULL_VAR if unit.
    pub fn max_var(&self) -> Var {
        self.vars.last().map(|vp| vp.var).unwrap_or(NULL_VAR)
    }

    /// Check if this monomial is univariate (contains at most one variable).
    #[inline]
    pub fn is_univariate(&self) -> bool {
        self.vars.len() <= 1
    }

    /// Check if this monomial is linear (degree 0 or 1).
    #[inline]
    pub fn is_linear(&self) -> bool {
        self.total_degree <= 1
    }

    /// Multiply two monomials.
    pub fn mul(&self, other: &Monomial) -> Monomial {
        if self.is_unit() {
            return other.clone();
        }
        if other.is_unit() {
            return self.clone();
        }

        let mut vars: SmallVec<[VarPower; 4]> = SmallVec::new();
        let mut i = 0;
        let mut j = 0;

        while i < self.vars.len() && j < other.vars.len() {
            match self.vars[i].var.cmp(&other.vars[j].var) {
                Ordering::Less => {
                    vars.push(self.vars[i]);
                    i += 1;
                }
                Ordering::Greater => {
                    vars.push(other.vars[j]);
                    j += 1;
                }
                Ordering::Equal => {
                    vars.push(VarPower::new(
                        self.vars[i].var,
                        self.vars[i].power + other.vars[j].power,
                    ));
                    i += 1;
                    j += 1;
                }
            }
        }
        vars.extend_from_slice(&self.vars[i..]);
        vars.extend_from_slice(&other.vars[j..]);

        let total_degree = self.total_degree + other.total_degree;
        let hash = compute_monomial_hash(&vars);

        Monomial {
            vars,
            total_degree,
            hash,
        }
    }

    /// Check if other divides self. Returns the quotient if it does.
    pub fn div(&self, other: &Monomial) -> Option<Monomial> {
        if other.is_unit() {
            return Some(self.clone());
        }

        let mut vars: SmallVec<[VarPower; 4]> = SmallVec::new();
        let mut j = 0;

        for vp in &self.vars {
            if j < other.vars.len() && other.vars[j].var == vp.var {
                if vp.power < other.vars[j].power {
                    return None;
                }
                let new_power = vp.power - other.vars[j].power;
                if new_power > 0 {
                    vars.push(VarPower::new(vp.var, new_power));
                }
                j += 1;
            } else if j < other.vars.len() && other.vars[j].var < vp.var {
                return None;
            } else {
                vars.push(*vp);
            }
        }

        if j < other.vars.len() {
            return None;
        }

        let total_degree = vars.iter().map(|vp| vp.power).sum();
        let hash = compute_monomial_hash(&vars);

        Some(Monomial {
            vars,
            total_degree,
            hash,
        })
    }

    /// Compute the GCD of two monomials.
    pub fn gcd(&self, other: &Monomial) -> Monomial {
        if self.is_unit() || other.is_unit() {
            return Monomial::unit();
        }

        let mut vars: SmallVec<[VarPower; 4]> = SmallVec::new();
        let mut i = 0;
        let mut j = 0;

        while i < self.vars.len() && j < other.vars.len() {
            match self.vars[i].var.cmp(&other.vars[j].var) {
                Ordering::Less => {
                    i += 1;
                }
                Ordering::Greater => {
                    j += 1;
                }
                Ordering::Equal => {
                    let min_power = self.vars[i].power.min(other.vars[j].power);
                    if min_power > 0 {
                        vars.push(VarPower::new(self.vars[i].var, min_power));
                    }
                    i += 1;
                    j += 1;
                }
            }
        }

        let total_degree = vars.iter().map(|vp| vp.power).sum();
        let hash = compute_monomial_hash(&vars);

        Monomial {
            vars,
            total_degree,
            hash,
        }
    }

    /// Raise monomial to a power.
    pub fn pow(&self, n: u32) -> Monomial {
        if n == 0 {
            return Monomial::unit();
        }
        if n == 1 {
            return self.clone();
        }

        let vars: SmallVec<[VarPower; 4]> = self
            .vars
            .iter()
            .map(|vp| VarPower::new(vp.var, vp.power * n))
            .collect();
        let total_degree = self.total_degree * n;
        let hash = compute_monomial_hash(&vars);

        Monomial {
            vars,
            total_degree,
            hash,
        }
    }

    /// Lexicographic comparison of monomials.
    pub fn lex_cmp(&self, other: &Monomial) -> Ordering {
        let mut i = 0;
        let mut j = 0;

        while i < self.vars.len() && j < other.vars.len() {
            match self.vars[i].var.cmp(&other.vars[j].var) {
                Ordering::Less => return Ordering::Greater,
                Ordering::Greater => return Ordering::Less,
                Ordering::Equal => match self.vars[i].power.cmp(&other.vars[j].power) {
                    Ordering::Equal => {
                        i += 1;
                        j += 1;
                    }
                    ord => return ord,
                },
            }
        }

        if i < self.vars.len() {
            Ordering::Greater
        } else if j < other.vars.len() {
            Ordering::Less
        } else {
            Ordering::Equal
        }
    }

    /// Graded lexicographic comparison (total degree first, then lex).
    pub fn grlex_cmp(&self, other: &Monomial) -> Ordering {
        match self.total_degree.cmp(&other.total_degree) {
            Ordering::Equal => self.lex_cmp(other),
            ord => ord,
        }
    }

    /// Graded reverse lexicographic comparison.
    pub fn grevlex_cmp(&self, other: &Monomial) -> Ordering {
        match self.total_degree.cmp(&other.total_degree) {
            Ordering::Equal => {
                // Reverse lex: compare from highest variable
                let mut i = self.vars.len();
                let mut j = other.vars.len();

                while i > 0 && j > 0 {
                    i -= 1;
                    j -= 1;

                    match self.vars[i].var.cmp(&other.vars[j].var) {
                        Ordering::Less => return Ordering::Less,
                        Ordering::Greater => return Ordering::Greater,
                        Ordering::Equal => match self.vars[i].power.cmp(&other.vars[j].power) {
                            Ordering::Equal => {}
                            Ordering::Less => return Ordering::Greater,
                            Ordering::Greater => return Ordering::Less,
                        },
                    }
                }

                if i > 0 {
                    Ordering::Less
                } else if j > 0 {
                    Ordering::Greater
                } else {
                    Ordering::Equal
                }
            }
            ord => ord,
        }
    }
}

fn compute_monomial_hash(vars: &[VarPower]) -> u64 {
    use core::hash::{BuildHasher, BuildHasherDefault};
    use rustc_hash::FxHasher;
    let mut hasher = BuildHasherDefault::<FxHasher>::default().build_hasher();
    for vp in vars {
        vp.hash(&mut hasher);
    }
    hasher.finish()
}

impl Hash for Monomial {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.hash.hash(state);
    }
}

impl fmt::Debug for Monomial {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.is_unit() {
            write!(f, "1")
        } else {
            for (i, vp) in self.vars.iter().enumerate() {
                if i > 0 {
                    write!(f, "*")?;
                }
                if vp.power == 1 {
                    write!(f, "x{}", vp.var)?;
                } else {
                    write!(f, "x{}^{}", vp.var, vp.power)?;
                }
            }
            Ok(())
        }
    }
}

impl fmt::Display for Monomial {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

/// A term is a coefficient multiplied by a monomial.
#[derive(Clone, PartialEq, Eq)]
pub struct Term {
    /// The coefficient of the term.
    pub coeff: BigRational,
    /// The monomial part of the term.
    pub monomial: Monomial,
}

impl Term {
    /// Create a new term.
    #[inline]
    pub fn new(coeff: BigRational, monomial: Monomial) -> Self {
        Self { coeff, monomial }
    }

    /// Create a constant term.
    #[inline]
    pub fn constant(c: BigRational) -> Self {
        Self::new(c, Monomial::unit())
    }

    /// Create a term from a single variable.
    #[inline]
    pub fn from_var(var: Var) -> Self {
        Self::new(BigRational::one(), Monomial::from_var(var))
    }

    /// Check if this term is zero.
    #[inline]
    pub fn is_zero(&self) -> bool {
        self.coeff.is_zero()
    }

    /// Check if this is a constant term.
    #[inline]
    pub fn is_constant(&self) -> bool {
        self.monomial.is_unit()
    }
}

impl fmt::Debug for Term {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.monomial.is_unit() {
            write!(f, "{}", self.coeff)
        } else if self.coeff.is_one() {
            write!(f, "{:?}", self.monomial)
        } else if self.coeff == -BigRational::one() {
            write!(f, "-{:?}", self.monomial)
        } else {
            write!(f, "{}*{:?}", self.coeff, self.monomial)
        }
    }
}

/// Monomial ordering for polynomial canonicalization.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum MonomialOrder {
    /// Lexicographic order.
    Lex,
    /// Graded lexicographic order.
    #[default]
    GrLex,
    /// Graded reverse lexicographic order.
    GRevLex,
}

impl MonomialOrder {
    /// Compare two monomials using this ordering.
    pub fn compare(&self, a: &Monomial, b: &Monomial) -> Ordering {
        match self {
            MonomialOrder::Lex => a.lex_cmp(b),
            MonomialOrder::GrLex => a.grlex_cmp(b),
            MonomialOrder::GRevLex => a.grevlex_cmp(b),
        }
    }
}