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