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 the Leja ordering.
31pub mod leja_order;
32
33/// This module implements a simple 2D vector.
34pub mod vector2;
35
36/// This module implements a simple 2D vector reference.
37pub mod vector2_ref;
38
39pub use crate::aberth::{
40    aberth, aberth_autocorr, aberth_mt, initial_aberth, initial_aberth_autocorr,
41    poly_from_autocorr_roots, poly_from_roots,
42};
43pub use crate::horner::{horner_eval_c, horner_eval_f};
44pub use crate::matrix2::Matrix2;
45pub use crate::rootfinding::{
46    extract_autocorr, initial_autocorr, initial_guess, pbairstow_autocorr, pbairstow_autocorr_mt,
47    pbairstow_even, pbairstow_even_mt, poly_from_autocorr_factors, poly_from_quadratic_factors,
48    Options,
49};
50pub use crate::vector2::Vector2;
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn it_works() {
58        let a = Vector2::<f64>::new(1.2, 2.3);
59        a.scale(3.4);
60        a.unscale(3.4);
61        println!("{:?}", a.norm_sqr());
62        println!("{:?}", a.l1_norm());
63
64        let b = Vector2::<f64>::new(3.4, 4.5);
65        println!("{:?}", a + b);
66        println!("{:?}", a - b);
67
68        let mut a = Vector2::<f64>::new(4.2, 5.3);
69        a += b;
70        a -= b;
71        a *= 3.4;
72        a /= 3.4;
73        println!("{:?}", -a);
74        println!("{:?}", a * 3.4);
75        println!("{:?}", 3.4 * a);
76        println!("{:?}", a / 3.4);
77
78        let mm = Vector2::<Vector2<f64>>::new(a, b);
79        println!("{:?}", mm);
80
81        let mm = Matrix2::<f64>::new(a, b);
82        println!("{:?}", mm);
83
84        let b = Vector2::<i32>::new(42, 53);
85        println!("{:?}", b % 3);
86
87        let options = Options {
88            max_iters: 2000,
89            tolerance: 1e-12,
90            tol_ind: 1e-15,
91        };
92
93        let coeffs = vec![10.0, 34.0, 75.0, 94.0, 150.0, 94.0, 75.0, 34.0, 10.0];
94
95        let mut vrs = initial_guess(&coeffs);
96        let (niter, _found) = pbairstow_even(&coeffs, &mut vrs, &options);
97        println!("{niter}");
98
99        let mut vrs = initial_guess(&coeffs);
100        let (niter, _found) = pbairstow_even_mt(&coeffs, &mut vrs, &options);
101        println!("{niter}");
102
103        let mut vrs = initial_autocorr(&coeffs);
104        let (niter, _found) = pbairstow_autocorr(&coeffs, &mut vrs, &options);
105        println!("{niter}");
106
107        let mut vrs = initial_autocorr(&coeffs);
108        let (niter, _found) = pbairstow_autocorr_mt(&coeffs, &mut vrs, &options);
109        println!("{niter}");
110
111        let mut zs = initial_aberth(&coeffs);
112        let (niter, _found) = aberth(&coeffs, &mut zs, &options);
113        println!("{niter}");
114
115        let mut zs = initial_aberth(&coeffs);
116        let (niter, _found) = aberth_mt(&coeffs, &mut zs, &options);
117        println!("{niter}");
118    }
119}