Skip to main content

ginger/
lib.rs

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