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