pub struct Poly { /* private fields */ }Expand description
A polynomial of dynamic degree.
It would be nice to have polynomials of type-level degree,
but that’s a bit awkward without const generic expressions
(e.g. to express the type of the derivative). It could be
done with typenum and generic_array…
Implementations§
Source§impl Poly
impl Poly
Sourcepub fn new(coeffs: impl IntoIterator<Item = f64>) -> Self
pub fn new(coeffs: impl IntoIterator<Item = f64>) -> Self
Constructs a new polynomial from coefficients.
The first coefficient provided will be the constant term, the second will be the linear term, and so on.
Sourcepub fn degree(&self) -> usize
pub fn degree(&self) -> usize
The degree of this polynomial.
This function only looks at the presence of coefficients, not their value. If you construct a polynomial with three coefficients, this method will say that it has degree 2 even if all of those coefficients are zero.
A polynomial with no coefficients will give zero as its degree, as will a polynomial with one coefficient.
Sourcepub fn to_cubic(&self) -> Option<Cubic>
pub fn to_cubic(&self) -> Option<Cubic>
If this polynomial has degree 3 or less, converts it to a cubic.
Sourcepub fn roots_in(&self, lower: f64, upper: f64, x_error: f64) -> Vec<f64>
pub fn roots_in(&self, lower: f64, upper: f64, x_error: f64) -> Vec<f64>
Finds all the roots in an interval, using Yuksel’s algorithm.
This is a numerical, iterative method. It first constructs critical
points to find bracketing intervals (intervals [x0, x1] where
self.eval(x0) and self.eval(x1) have different signs). Then it uses
a kind of modified Newton method to find a root on each bracketing
interval. It has a few limitations:
- if there is only a small interval where the polynomial changes sign, it can miss roots. For example, when two roots are very close together it can miss them both.
- run time is quadratic in the degree. However, it is often very fast
in practice for polynomials of low degree, especially if the interval
[lower, upper]contains few roots.