pub struct PolyDyn { /* 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 PolyDyn
impl PolyDyn
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 coeffs(&self) -> &[f64]
pub fn coeffs(&self) -> &[f64]
The coefficients of this polynomial.
In the returned slice, the coefficient of x^i is at index i.
Sourcepub fn deriv(&self) -> PolyDyn
pub fn deriv(&self) -> PolyDyn
Returns the polynomial that’s the derivative of this polynomial.
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 roots_between(&self, lower: f64, upper: f64, x_error: f64) -> Vec<f64>
pub fn roots_between(&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.
Sourcepub fn roots_between_with_buffer(
&self,
lower: f64,
upper: f64,
x_error: f64,
out: &mut Vec<f64>,
scratch: &mut Vec<f64>,
)
pub fn roots_between_with_buffer( &self, lower: f64, upper: f64, x_error: f64, out: &mut Vec<f64>, scratch: &mut Vec<f64>, )
Finds all the roots in an interval, using Yuksel’s algorithm.
See PolyDyn::roots_between for more details. This method differs from that
one in that it performs fewer allocations: you provide an out buffer
for the result and a scratch buffer for intermediate computations.