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#![warn(missing_docs)]
91
92pub mod bdd;
93pub mod blas;
94pub mod blas_ops;
95pub mod delta_rational;
96pub mod grobner;
97pub mod hilbert;
98pub mod interior_point;
99pub mod interval;
100pub mod lp;
101pub mod matrix;
102pub mod mpfr;
103pub mod polynomial;
104pub mod rational;
105pub mod rational_function;
106pub mod realclosure;
107pub mod simplex;
108
109#[cfg(test)]
110mod integration_tests {
111 use super::*;
112 use num_bigint::BigInt;
113 use num_rational::BigRational;
114
115 fn rat(n: i64) -> BigRational {
116 BigRational::from_integer(BigInt::from(n))
117 }
118
119 #[test]
120 fn test_grobner_with_root_isolation() {
121 // Integration test: Use Gröbner basis to simplify, then isolate roots
122 // System: x^2 - 2 = 0, y - x = 0
123 // Should reduce to y^2 - 2 = 0
124
125 let x_squared_minus_2 = polynomial::Polynomial::from_coeffs_int(&[
126 (1, &[(0, 2)]), // x^2
127 (-2, &[]), // -2
128 ]);
129
130 let y_minus_x = polynomial::Polynomial::from_coeffs_int(&[
131 (1, &[(1, 1)]), // y
132 (-1, &[(0, 1)]), // -x
133 ]);
134
135 let gb = grobner::grobner_basis(&[x_squared_minus_2.clone(), y_minus_x]);
136
137 // The Gröbner basis should contain polynomials
138 assert!(!gb.is_empty());
139
140 // One of the polynomials should be univariate
141 let has_univariate = gb.iter().any(|p| p.is_univariate());
142 assert!(has_univariate || gb.len() == 1);
143 }
144
145 #[test]
146 fn test_nra_solver_with_algebraic_numbers() {
147 // Integration test: NRA solver with algebraic number evaluation
148 // Solve x^2 - 2 = 0
149
150 let mut solver = grobner::NraSolver::new();
151
152 let x_squared_minus_2 = polynomial::Polynomial::from_coeffs_int(&[
153 (1, &[(0, 2)]), // x^2
154 (-2, &[]), // -2
155 ]);
156
157 solver.add_equality(x_squared_minus_2.clone());
158
159 // Should be satisfiable
160 assert_eq!(solver.check_sat(), grobner::SatResult::Sat);
161
162 // Create algebraic number for sqrt(2)
163 // AlgebraicNumber::new(poly, var, lower, upper)
164 let sqrt_2 = realclosure::AlgebraicNumber::new(
165 x_squared_minus_2,
166 0, // variable 0
167 rat(1),
168 rat(2),
169 );
170
171 // Algebraic number should be valid
172 let _ = sqrt_2;
173 }
174
175 #[test]
176 fn test_interval_with_polynomial_bounds() {
177 // Integration test: Use interval arithmetic with polynomial evaluation
178 // Evaluate x^2 over [1, 2] should give [1, 4]
179
180 let x_squared = polynomial::Polynomial::from_coeffs_int(&[(1, &[(0, 2)])]);
181
182 // Evaluate at x = 1
183 let mut assignment1 = rustc_hash::FxHashMap::default();
184 assignment1.insert(0, rat(1));
185 let val1 = x_squared.eval(&assignment1);
186 assert_eq!(val1, rat(1));
187
188 // Evaluate at x = 2
189 let mut assignment2 = rustc_hash::FxHashMap::default();
190 assignment2.insert(0, rat(2));
191 let val2 = x_squared.eval(&assignment2);
192 assert_eq!(val2, rat(4));
193
194 // Create interval [1, 4]
195 let interval = interval::Interval::closed(rat(1), rat(4));
196 assert!(interval.contains(&val1));
197 assert!(interval.contains(&val2));
198 }
199
200 #[test]
201 fn test_delta_rationals_ordering() {
202 // Integration test: Delta rationals for strict inequalities
203 let delta_zero = delta_rational::DeltaRational::from_rational(rat(0));
204 let delta_small = delta_rational::DeltaRational::new(rat(0), 1); // delta_coeff is i64
205
206 // 0 + delta > 0
207 assert!(delta_small > delta_zero);
208
209 // Delta rationals maintain ordering
210 let delta_one = delta_rational::DeltaRational::from_rational(rat(1));
211 assert!(delta_one > delta_small);
212 }
213
214 #[test]
215 fn test_matrix_operations() {
216 // Integration test: Matrix operations (used in F4 algorithm)
217 use matrix::Matrix;
218 use num_rational::Rational64;
219
220 // Create a simple 2x2 matrix
221 let m = Matrix::from_vec(
222 2,
223 2,
224 vec![
225 Rational64::new(2, 1),
226 Rational64::new(1, 1),
227 Rational64::new(1, 1),
228 Rational64::new(1, 1),
229 ],
230 );
231
232 // Check matrix values
233 assert_eq!(m.get(0, 0), Rational64::new(2, 1));
234 assert_eq!(m.get(0, 1), Rational64::new(1, 1));
235 }
236
237 #[test]
238 fn test_polynomial_factorization_with_grobner() {
239 // Integration test: Factorization helps with Gröbner basis computation
240 // x^2 - y^2 can be analyzed via Gröbner basis
241
242 let x_sq_minus_y_sq = polynomial::Polynomial::from_coeffs_int(&[
243 (1, &[(0, 2)]), // x^2
244 (-1, &[(1, 2)]), // -y^2
245 ]);
246
247 // Compute Gröbner basis of {x^2 - y^2}
248 let gb = grobner::grobner_basis(&[x_sq_minus_y_sq]);
249
250 assert!(!gb.is_empty());
251 }
252
253 #[test]
254 fn test_real_closure_root_isolation_integration() {
255 // Integration test: Real closure and root isolation
256 // Find roots of x^3 - 2 = 0
257
258 let poly = polynomial::Polynomial::from_coeffs_int(&[
259 (1, &[(0, 3)]), // x^3
260 (-2, &[]), // -2
261 ]);
262
263 // Isolate roots (for variable 0)
264 let roots = poly.isolate_roots(0);
265
266 // Should find at least one real root (cube root of 2)
267 assert!(!roots.is_empty());
268 }
269
270 #[test]
271 fn test_polynomial_gcd_univariate() {
272 // Integration test: GCD computation for univariate polynomials
273 // gcd(x^2 - 1, x - 1) = x - 1
274
275 let p1 = polynomial::Polynomial::from_coeffs_int(&[
276 (1, &[(0, 2)]), // x^2
277 (-1, &[]), // -1
278 ]);
279
280 let p2 = polynomial::Polynomial::from_coeffs_int(&[
281 (1, &[(0, 1)]), // x
282 (-1, &[]), // -1
283 ]);
284
285 let gcd = p1.gcd_univariate(&p2);
286
287 // GCD should be x - 1 (or a scalar multiple)
288 assert_eq!(gcd.total_degree(), 1);
289 }
290}