rust_poly/poly/roots/
many_roots.rs

1//! Methods that find multiple roots at the same time (but not all roots)
2
3use super::{halley, naive, newton, single_root::NextRootFun};
4use crate::{
5    num::Complex,
6    util::doc_macros::{errors_no_converge, panic_t_from_f64},
7    Poly, RealScalar,
8};
9
10/// Run multiple single-root root finders in parallel.
11///
12/// # Errors
13/// Passes through whichever errors the passed closure `next_root_fun` returned.
14///
15/// # Panics
16#[doc = panic_t_from_f64!()]
17pub fn parallel<T: RealScalar>(
18    next_root_fun: NextRootFun<T>,
19    poly: &mut Poly<T>,
20    epsilon: Option<T>,
21    max_iter: Option<usize>,
22    initial_guesses: &[Complex<T>],
23) -> super::Result<T> {
24    poly.make_monic();
25
26    let mut roots = vec![];
27    for z in initial_guesses {
28        log::trace!("next root");
29        roots.extend(
30            next_root_fun(
31                poly,
32                epsilon.clone().unwrap_or(T::zero()),
33                max_iter,
34                Some(z.clone()),
35            )?
36            .0,
37        );
38    }
39    Ok(roots)
40}
41
42/// Use Naive Newton's method in parallel for multiple initial guesses.
43///
44/// # Errors
45#[doc = errors_no_converge!()]
46#[inline]
47pub fn naive_parallel<T: RealScalar>(
48    poly: &mut Poly<T>,
49    epsilon: Option<T>,
50    max_iter: Option<usize>,
51    initial_guesses: &[Complex<T>],
52) -> super::Result<T> {
53    parallel(naive, poly, epsilon, max_iter, initial_guesses)
54}
55
56/// Use Newton's method in parallel for multiple initial guesses.
57///
58/// # Errors
59#[doc = errors_no_converge!()]
60#[inline]
61pub fn newton_parallel<T: RealScalar>(
62    poly: &mut Poly<T>,
63    epsilon: Option<T>,
64    max_iter: Option<usize>,
65    initial_guesses: &[Complex<T>],
66) -> super::Result<T> {
67    parallel(newton, poly, epsilon, max_iter, initial_guesses)
68}
69
70/// Use Halley's method in parallel for multiple initial guesses.
71///
72/// # Errors
73#[doc = errors_no_converge!()]
74#[inline]
75pub fn halley_parallel<T: RealScalar>(
76    poly: &mut Poly<T>,
77    epsilon: Option<T>,
78    max_iter: Option<usize>,
79    initial_guesses: &[Complex<T>],
80) -> super::Result<T> {
81    parallel(halley, poly, epsilon, max_iter, initial_guesses)
82}