Skip to main content

ginger/
lib.rs

1//! # Ginger-rs
2//!
3//! This crate provides implementations of polynomial root finding algorithms.
4//!
5//! ## Modules
6//!
7//! * `aberth` - Implements the Aberth method for finding the roots of a polynomial.
8//! * `horner` - Implements Horner's method for polynomial evaluation.
9//! * `matrix2` - Implements a simple 2x2 matrix.
10//! * `rootfinding` - Implements the Bairstow's method for finding the roots of a polynomial.
11//! * `leja_order` - Implements the Leja ordering.
12//! * `vector2` - Implements a simple 2D vector.
13//! * `vector2_ref` - Implements a simple 2D vector reference.
14
15/// This module provides precomputed low-discrepancy sequence table lookups.
16pub mod tables;
17
18/// This module implements the Aberth method for finding the roots of a polynomial.
19pub mod aberth;
20
21/// This module implements Horner's method for polynomial evaluation.
22pub mod horner;
23
24/// This module implements a simple 2x2 matrix.
25pub mod matrix2;
26
27/// This module implements the Bairstow's method for finding the roots of a polynomial.
28pub mod rootfinding;
29
30/// This module implements Bairstow's method for auto-correlation (palindromic) polynomials.
31pub mod autocorr;
32
33/// This module provides the execution policies (sequential / Jacobi-MT / atomic).
34pub mod execution_policy;
35
36/// This module provides the facade entry points with auto policy selection.
37pub mod solve;
38
39/// This module implements the Leja ordering.
40pub mod leja_order;
41
42/// This module provides coefficient scaling for scale-invariant convergence.
43pub mod scaling;
44
45/// This module provides seqlock-based atomic pairs for the atomic multi-threading method.
46pub mod seqlock;
47
48/// This module implements a simple 2D vector.
49pub mod vector2;
50
51/// This module implements a simple 2D vector reference.
52pub mod vector2_ref;
53
54pub use crate::aberth::{
55    aberth, aberth_atomic, aberth_autocorr, aberth_mt, initial_aberth, initial_aberth_autocorr,
56    poly_from_autocorr_roots, poly_from_roots,
57};
58pub use crate::autocorr::{
59    extract_autocorr, initial_autocorr, pbairstow_autocorr, pbairstow_autocorr_atomic,
60    pbairstow_autocorr_mt, poly_from_autocorr_factors,
61};
62pub use crate::execution_policy::{should_parallelize, AtomicCell, PARALLEL_THRESHOLD};
63pub use crate::horner::{horner_eval_c, horner_eval_f};
64pub use crate::matrix2::Matrix2;
65pub use crate::rootfinding::{
66    initial_guess, pbairstow_even, pbairstow_even_atomic, pbairstow_even_mt,
67    poly_from_quadratic_factors, Options, Vec2,
68};
69pub use crate::scaling::{derivative_coeffs, residual_scale, scaled_coeffs};
70pub use crate::seqlock::{AtomicComplex, AtomicVec2};
71pub use crate::solve::{
72    solve_aberth, solve_aberth_autocorr, solve_pbairstow_autocorr, solve_pbairstow_even, SolveMode,
73};
74pub use crate::vector2::Vector2;
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn it_works() {
82        let a = Vector2::<f64>::new(1.2, 2.3);
83        a.scale(3.4);
84        a.unscale(3.4);
85        println!("{:?}", a.norm_sqr());
86        println!("{:?}", a.l1_norm());
87
88        let b = Vector2::<f64>::new(3.4, 4.5);
89        println!("{:?}", a + b);
90        println!("{:?}", a - b);
91
92        let mut a = Vector2::<f64>::new(4.2, 5.3);
93        a += b;
94        a -= b;
95        a *= 3.4;
96        a /= 3.4;
97        println!("{:?}", -a);
98        println!("{:?}", a * 3.4);
99        println!("{:?}", 3.4 * a);
100        println!("{:?}", a / 3.4);
101
102        let mm = Vector2::<Vector2<f64>>::new(a, b);
103        println!("{:?}", mm);
104
105        let mm = Matrix2::<f64>::new(a, b);
106        println!("{:?}", mm);
107
108        let b = Vector2::<i32>::new(42, 53);
109        println!("{:?}", b % 3);
110
111        let options = Options {
112            max_iters: 2000,
113            tolerance: 1e-12,
114            tol_ind: 1e-15,
115        };
116
117        let coeffs = vec![10.0, 34.0, 75.0, 94.0, 150.0, 94.0, 75.0, 34.0, 10.0];
118
119        let mut vrs = initial_guess(&coeffs);
120        let (niter, _found) = pbairstow_even(&coeffs, &mut vrs, &options);
121        println!("{niter}");
122
123        let mut vrs = initial_guess(&coeffs);
124        let (niter, _found) = pbairstow_even_mt(&coeffs, &mut vrs, &options);
125        println!("{niter}");
126
127        let mut vrs = initial_autocorr(&coeffs);
128        let (niter, _found) = pbairstow_autocorr(&coeffs, &mut vrs, &options);
129        println!("{niter}");
130
131        let mut vrs = initial_autocorr(&coeffs);
132        let (niter, _found) = pbairstow_autocorr_mt(&coeffs, &mut vrs, &options);
133        println!("{niter}");
134
135        let mut zs = initial_aberth(&coeffs);
136        let (niter, _found) = aberth(&coeffs, &mut zs, &options);
137        println!("{niter}");
138
139        let mut zs = initial_aberth(&coeffs);
140        let (niter, _found) = aberth_mt(&coeffs, &mut zs, &options);
141        println!("{niter}");
142    }
143}