Skip to main content

oxiz_math/
lib.rs

1//! # oxiz-math
2//!
3//! Mathematical foundations for the OxiZ SMT solver.
4//!
5//! This crate provides Pure Rust implementations of mathematical algorithms
6//! required for SMT solving, including:
7//!
8//! ## Linear Arithmetic
9//! - **Simplex**: Dual simplex algorithm for linear programming (LRA theory)
10//! - **Interior Point**: Primal-dual interior point method for large-scale LP
11//! - **Matrix**: Dense and sparse matrix operations with Gaussian elimination
12//! - **Interval**: Interval arithmetic for bound propagation
13//! - **Delta Rational**: Support for strict inequalities in simplex
14//! - **BLAS**: High-performance BLAS operations for large-scale LP (1000+ variables)
15//!
16//! ## Non-Linear Arithmetic
17//! - **Polynomial**: Multivariate polynomial arithmetic with GCD and factorization
18//! - **Rational Function**: Arithmetic on quotients of polynomials (p/q operations)
19//! - **Gröbner**: Gröbner basis computation (Buchberger, F4, and F5 algorithms)
20//! - **Real Closure**: Algebraic number representation and root isolation
21//! - **Hilbert**: Hilbert basis computation for integer cones
22//!
23//! ## Decision Diagrams
24//! - **BDD**: Reduced Ordered Binary Decision Diagrams
25//! - **ZDD**: Zero-suppressed BDDs for sparse set representation
26//! - **ADD**: Algebraic Decision Diagrams for rational-valued functions
27//!
28//! ## Numerical Utilities
29//! - **Rational**: Arbitrary precision rational arithmetic utilities
30//! - **MPFR**: Arbitrary precision floating-point arithmetic (MPFR-like)
31//!
32//! # Examples
33//!
34//! ## Polynomial Arithmetic
35//!
36//! ```
37//! use oxiz_math::polynomial::{Polynomial, Var};
38//!
39//! // Create polynomial for variable x (index 0)
40//! let x: Var = 0;
41//!
42//! // Create polynomial representing just x
43//! let p = Polynomial::from_var(x);
44//!
45//! // Compute x * x = x^2
46//! let p_squared = p.clone() * p.clone();
47//! ```
48//!
49//! ## BDD Operations
50//!
51//! ```
52//! use oxiz_math::bdd::BddManager;
53//!
54//! let mut mgr = BddManager::new();
55//!
56//! // Create variables (VarId is u32)
57//! let x = mgr.variable(0);
58//! let y = mgr.variable(1);
59//!
60//! // Compute x AND y
61//! let and_xy = mgr.and(x, y);
62//!
63//! // Compute x OR y
64//! let or_xy = mgr.or(x, y);
65//! ```
66//!
67//! ## BLAS Operations
68//!
69//! ```
70//! use oxiz_math::blas::{ddot, dgemv, Transpose};
71//!
72//! // Vector dot product
73//! let x = vec![1.0, 2.0, 3.0];
74//! let y = vec![4.0, 5.0, 6.0];
75//! let dot = ddot(&x, &y);
76//! assert_eq!(dot, 32.0);
77//! ```
78//!
79//! ## Arbitrary Precision Floats
80//!
81//! ```
82//! use oxiz_math::mpfr::{ArbitraryFloat, Precision, RoundingMode};
83//!
84//! let prec = Precision::new(128);
85//! let a = ArbitraryFloat::from_f64(3.14159, prec);
86//! let b = ArbitraryFloat::from_f64(2.71828, prec);
87//! let sum = a.add(&b, RoundingMode::RoundNearest);
88//! ```
89
90#![cfg_attr(not(feature = "std"), no_std)]
91#![warn(missing_docs)]
92
93#[cfg(not(feature = "std"))]
94extern crate alloc;
95
96mod prelude;
97
98// TODO: algebraic module needs refactoring to match Polynomial API
99// pub mod algebraic;
100pub mod algebraic_number;
101pub mod bdd;
102#[cfg(feature = "std")]
103pub mod blas;
104#[cfg(feature = "std")]
105pub mod blas_ops;
106pub mod delta_rational;
107pub mod fast_rational;
108pub mod grobner;
109pub mod hilbert;
110pub mod interior_point;
111pub mod interval;
112pub mod lp;
113pub mod lp_core;
114pub mod matrix;
115#[cfg(feature = "std")]
116pub mod mpfr;
117pub mod polynomial;
118pub mod rational;
119pub mod rational_function;
120pub mod realclosure;
121pub mod realclosure_advanced;
122#[cfg(feature = "std")]
123pub mod simd;
124pub mod simplex;
125pub mod simplex_parametric;
126
127#[cfg(test)]
128mod integration_tests {
129    use super::*;
130    use num_bigint::BigInt;
131    use num_rational::BigRational;
132
133    fn rat(n: i64) -> BigRational {
134        BigRational::from_integer(BigInt::from(n))
135    }
136
137    #[test]
138    fn test_grobner_with_root_isolation() {
139        // Integration test: Use Gröbner basis to simplify, then isolate roots
140        // System: x^2 - 2 = 0, y - x = 0
141        // Should reduce to y^2 - 2 = 0
142
143        let x_squared_minus_2 = polynomial::Polynomial::from_coeffs_int(&[
144            (1, &[(0, 2)]), // x^2
145            (-2, &[]),      // -2
146        ]);
147
148        let y_minus_x = polynomial::Polynomial::from_coeffs_int(&[
149            (1, &[(1, 1)]),  // y
150            (-1, &[(0, 1)]), // -x
151        ]);
152
153        let gb = grobner::grobner_basis(&[x_squared_minus_2.clone(), y_minus_x]);
154
155        // The Gröbner basis should contain polynomials
156        assert!(!gb.is_empty());
157
158        // One of the polynomials should be univariate
159        let has_univariate = gb.iter().any(|p| p.is_univariate());
160        assert!(has_univariate || gb.len() == 1);
161    }
162
163    #[test]
164    fn test_nra_solver_with_algebraic_numbers() {
165        // Integration test: NRA solver with algebraic number evaluation
166        // Solve x^2 - 2 = 0
167
168        let mut solver = grobner::NraSolver::new();
169
170        let x_squared_minus_2 = polynomial::Polynomial::from_coeffs_int(&[
171            (1, &[(0, 2)]), // x^2
172            (-2, &[]),      // -2
173        ]);
174
175        solver.add_equality(x_squared_minus_2.clone());
176
177        // Should be satisfiable
178        assert_eq!(solver.check_sat(), grobner::SatResult::Sat);
179
180        // Create algebraic number for sqrt(2)
181        // AlgebraicNumber::new(poly, var, lower, upper)
182        let sqrt_2 = realclosure::AlgebraicNumber::new(
183            x_squared_minus_2,
184            0, // variable 0
185            rat(1),
186            rat(2),
187        );
188
189        // Algebraic number should be valid
190        let _ = sqrt_2;
191    }
192
193    #[test]
194    fn test_interval_with_polynomial_bounds() {
195        // Integration test: Use interval arithmetic with polynomial evaluation
196        // Evaluate x^2 over [1, 2] should give [1, 4]
197
198        let x_squared = polynomial::Polynomial::from_coeffs_int(&[(1, &[(0, 2)])]);
199
200        // Evaluate at x = 1
201        let mut assignment1 = crate::prelude::FxHashMap::default();
202        assignment1.insert(0, rat(1));
203        let val1 = x_squared.eval(&assignment1);
204        assert_eq!(val1, rat(1));
205
206        // Evaluate at x = 2
207        let mut assignment2 = crate::prelude::FxHashMap::default();
208        assignment2.insert(0, rat(2));
209        let val2 = x_squared.eval(&assignment2);
210        assert_eq!(val2, rat(4));
211
212        // Create interval [1, 4]
213        let interval = interval::Interval::closed(rat(1), rat(4));
214        assert!(interval.contains(&val1));
215        assert!(interval.contains(&val2));
216    }
217
218    #[test]
219    fn test_delta_rationals_ordering() {
220        // Integration test: Delta rationals for strict inequalities
221        let delta_zero = delta_rational::DeltaRational::from_rational(rat(0));
222        let delta_small = delta_rational::DeltaRational::new(rat(0), 1); // delta_coeff is i64
223
224        // 0 + delta > 0
225        assert!(delta_small > delta_zero);
226
227        // Delta rationals maintain ordering
228        let delta_one = delta_rational::DeltaRational::from_rational(rat(1));
229        assert!(delta_one > delta_small);
230    }
231
232    #[test]
233    fn test_matrix_operations() {
234        // Integration test: Matrix operations (used in F4 algorithm)
235        use matrix::Matrix;
236        use num_rational::Rational64;
237
238        // Create a simple 2x2 matrix
239        let m = Matrix::from_vec(
240            2,
241            2,
242            vec![
243                Rational64::new(2, 1),
244                Rational64::new(1, 1),
245                Rational64::new(1, 1),
246                Rational64::new(1, 1),
247            ],
248        );
249
250        // Check matrix values
251        assert_eq!(m.get(0, 0), Rational64::new(2, 1));
252        assert_eq!(m.get(0, 1), Rational64::new(1, 1));
253    }
254
255    #[test]
256    fn test_polynomial_factorization_with_grobner() {
257        // Integration test: Factorization helps with Gröbner basis computation
258        // x^2 - y^2 can be analyzed via Gröbner basis
259
260        let x_sq_minus_y_sq = polynomial::Polynomial::from_coeffs_int(&[
261            (1, &[(0, 2)]),  // x^2
262            (-1, &[(1, 2)]), // -y^2
263        ]);
264
265        // Compute Gröbner basis of {x^2 - y^2}
266        let gb = grobner::grobner_basis(&[x_sq_minus_y_sq]);
267
268        assert!(!gb.is_empty());
269    }
270
271    #[test]
272    fn test_real_closure_root_isolation_integration() {
273        // Integration test: Real closure and root isolation
274        // Find roots of x^3 - 2 = 0
275
276        let poly = polynomial::Polynomial::from_coeffs_int(&[
277            (1, &[(0, 3)]), // x^3
278            (-2, &[]),      // -2
279        ]);
280
281        // Isolate roots (for variable 0)
282        let roots = poly.isolate_roots(0);
283
284        // Should find at least one real root (cube root of 2)
285        assert!(!roots.is_empty());
286    }
287
288    #[test]
289    fn test_polynomial_gcd_univariate() {
290        // Integration test: GCD computation for univariate polynomials
291        // gcd(x^2 - 1, x - 1) = x - 1
292
293        let p1 = polynomial::Polynomial::from_coeffs_int(&[
294            (1, &[(0, 2)]), // x^2
295            (-1, &[]),      // -1
296        ]);
297
298        let p2 = polynomial::Polynomial::from_coeffs_int(&[
299            (1, &[(0, 1)]), // x
300            (-1, &[]),      // -1
301        ]);
302
303        let gcd = p1.gcd_univariate(&p2);
304
305        // GCD should be x - 1 (or a scalar multiple)
306        assert_eq!(gcd.total_degree(), 1);
307    }
308}