Poly

Struct Poly 

Source
pub struct Poly<const N: usize> { /* private fields */ }
Expand description

A polynomial whose degree is known at compile-time.

Although this supports polynomials of arbitrary degree, it is intended for low-degree polynomials. For example, the coefficients are stored in an array, and so they will be stack-allocated (unless you Box the Poly, of course) tend to be copied around.

Polynomial multiplication is not yet implemented, because doing it “nicely” would require const generic expressions: ideally we’d do something like

impl<N, M> Mul<Poly<M>> for Poly<N> {
    type Output = Poly<{M + N - 1}>;
}

It’s possible to work around this with macros, but there are lots of possibilities and I didn’t feel like it was worth the trouble (and the hit to compilation time).

Implementations§

Source§

impl Poly<4>

Source

pub fn roots_between( self, lower: f64, upper: f64, x_error: f64, ) -> ArrayVec<f64, 3>

Computes all roots between lower and upper, to the desired accuracy.

We make no guarantees about multiplicity. In fact, if there’s a double-root that isn’t a triple-root (and therefore has no sign change nearby) then there’s a good chance we miss it altogether. This is fine if you’re using this root-finding to optimize a quartic, because double-roots of the derivative aren’t local extrema.

Source§

impl<const N: usize> Poly<N>

Source

pub const fn new(coeffs: [f64; N]) -> Poly<N>

Creates a new polynomial with the provided coefficients.

The constant coefficient comes first, then the linear coefficient, and so on. So if you pass [c, b, a] you’ll get the polynomial a x^2 + b x + c.

Source

pub fn coeffs(&self) -> &[f64; N]

The coefficients of this polynomial.

In the returned array, the coefficient of x^i is at index i.

Source

pub fn eval(&self, x: f64) -> f64

Evaluates this polynomial at a point.

Source

pub fn max_abs_coefficient(&self) -> f64

Returns the largest absolute value of any coefficient.

Always returns a non-negative number, or NaN if some coefficient is NaN.

Source

pub fn is_finite(&self) -> bool

Are all the coefficients finite?

Source§

impl Poly<3>

Source

pub fn deriv(&self) -> Poly<2>

Compute the derivative of this polynomial, as a polynomial with one less coefficient.

Source

pub fn deflate(&self, root: f64) -> Poly<2>

Divide this polynomial by the polynomial x - root, returning the quotient (as a polynomial with one less coefficient) and ignoring the remainder.

If root is actually a root of self (as the name suggests it should be, but this is not actually required), the remainder will be zero. In general, the remainder will be self.eval(root).

Source§

impl Poly<4>

Source

pub fn deriv(&self) -> Poly<3>

Compute the derivative of this polynomial, as a polynomial with one less coefficient.

Source

pub fn deflate(&self, root: f64) -> Poly<3>

Divide this polynomial by the polynomial x - root, returning the quotient (as a polynomial with one less coefficient) and ignoring the remainder.

If root is actually a root of self (as the name suggests it should be, but this is not actually required), the remainder will be zero. In general, the remainder will be self.eval(root).

Source§

impl Poly<5>

Source

pub fn deriv(&self) -> Poly<4>

Compute the derivative of this polynomial, as a polynomial with one less coefficient.

Source

pub fn deflate(&self, root: f64) -> Poly<4>

Divide this polynomial by the polynomial x - root, returning the quotient (as a polynomial with one less coefficient) and ignoring the remainder.

If root is actually a root of self (as the name suggests it should be, but this is not actually required), the remainder will be zero. In general, the remainder will be self.eval(root).

Source§

impl Poly<6>

Source

pub fn deriv(&self) -> Poly<5>

Compute the derivative of this polynomial, as a polynomial with one less coefficient.

Source

pub fn deflate(&self, root: f64) -> Poly<5>

Divide this polynomial by the polynomial x - root, returning the quotient (as a polynomial with one less coefficient) and ignoring the remainder.

If root is actually a root of self (as the name suggests it should be, but this is not actually required), the remainder will be zero. In general, the remainder will be self.eval(root).

Source§

impl Poly<7>

Source

pub fn deriv(&self) -> Poly<6>

Compute the derivative of this polynomial, as a polynomial with one less coefficient.

Source

pub fn deflate(&self, root: f64) -> Poly<6>

Divide this polynomial by the polynomial x - root, returning the quotient (as a polynomial with one less coefficient) and ignoring the remainder.

If root is actually a root of self (as the name suggests it should be, but this is not actually required), the remainder will be zero. In general, the remainder will be self.eval(root).

Source§

impl Poly<8>

Source

pub fn deriv(&self) -> Poly<7>

Compute the derivative of this polynomial, as a polynomial with one less coefficient.

Source

pub fn deflate(&self, root: f64) -> Poly<7>

Divide this polynomial by the polynomial x - root, returning the quotient (as a polynomial with one less coefficient) and ignoring the remainder.

If root is actually a root of self (as the name suggests it should be, but this is not actually required), the remainder will be zero. In general, the remainder will be self.eval(root).

Source§

impl Poly<9>

Source

pub fn deriv(&self) -> Poly<8>

Compute the derivative of this polynomial, as a polynomial with one less coefficient.

Source

pub fn deflate(&self, root: f64) -> Poly<8>

Divide this polynomial by the polynomial x - root, returning the quotient (as a polynomial with one less coefficient) and ignoring the remainder.

If root is actually a root of self (as the name suggests it should be, but this is not actually required), the remainder will be zero. In general, the remainder will be self.eval(root).

Source§

impl Poly<10>

Source

pub fn deriv(&self) -> Poly<9>

Compute the derivative of this polynomial, as a polynomial with one less coefficient.

Source

pub fn deflate(&self, root: f64) -> Poly<9>

Divide this polynomial by the polynomial x - root, returning the quotient (as a polynomial with one less coefficient) and ignoring the remainder.

If root is actually a root of self (as the name suggests it should be, but this is not actually required), the remainder will be zero. In general, the remainder will be self.eval(root).

Source§

impl Poly<5>

Source

pub fn roots_between( self, lower: f64, upper: f64, x_error: f64, ) -> ArrayVec<f64, 4>

Computes all roots between lower and upper, to the desired accuracy.

We make no guarantees about multiplicity. For example, if there’s a double-root that isn’t a triple-root (and therefore has no sign change nearby) then there’s a good chance we miss it altogether. This is fine if you’re using this root-finding to find critical points for optimizing a polynomial, because roots that don’t come with a sign change aren’t local extrema.

Source§

impl Poly<6>

Source

pub fn roots_between( self, lower: f64, upper: f64, x_error: f64, ) -> ArrayVec<f64, 5>

Computes all roots between lower and upper, to the desired accuracy.

We make no guarantees about multiplicity. For example, if there’s a double-root that isn’t a triple-root (and therefore has no sign change nearby) then there’s a good chance we miss it altogether. This is fine if you’re using this root-finding to find critical points for optimizing a polynomial, because roots that don’t come with a sign change aren’t local extrema.

Source§

impl Poly<7>

Source

pub fn roots_between( self, lower: f64, upper: f64, x_error: f64, ) -> ArrayVec<f64, 6>

Computes all roots between lower and upper, to the desired accuracy.

We make no guarantees about multiplicity. For example, if there’s a double-root that isn’t a triple-root (and therefore has no sign change nearby) then there’s a good chance we miss it altogether. This is fine if you’re using this root-finding to find critical points for optimizing a polynomial, because roots that don’t come with a sign change aren’t local extrema.

Source§

impl Poly<8>

Source

pub fn roots_between( self, lower: f64, upper: f64, x_error: f64, ) -> ArrayVec<f64, 7>

Computes all roots between lower and upper, to the desired accuracy.

We make no guarantees about multiplicity. For example, if there’s a double-root that isn’t a triple-root (and therefore has no sign change nearby) then there’s a good chance we miss it altogether. This is fine if you’re using this root-finding to find critical points for optimizing a polynomial, because roots that don’t come with a sign change aren’t local extrema.

Source§

impl Poly<9>

Source

pub fn roots_between( self, lower: f64, upper: f64, x_error: f64, ) -> ArrayVec<f64, 8>

Computes all roots between lower and upper, to the desired accuracy.

We make no guarantees about multiplicity. For example, if there’s a double-root that isn’t a triple-root (and therefore has no sign change nearby) then there’s a good chance we miss it altogether. This is fine if you’re using this root-finding to find critical points for optimizing a polynomial, because roots that don’t come with a sign change aren’t local extrema.

Source§

impl Poly<10>

Source

pub fn roots_between( self, lower: f64, upper: f64, x_error: f64, ) -> ArrayVec<f64, 9>

Computes all roots between lower and upper, to the desired accuracy.

We make no guarantees about multiplicity. For example, if there’s a double-root that isn’t a triple-root (and therefore has no sign change nearby) then there’s a good chance we miss it altogether. This is fine if you’re using this root-finding to find critical points for optimizing a polynomial, because roots that don’t come with a sign change aren’t local extrema.

Source§

impl Poly<3>

Source

pub fn roots(&self) -> ArrayVec<f64, 2>

Returns the roots of this quadratic, in increasing order.

Double-roots are only counted once.

Source

pub fn positive_discriminant_roots(&self) -> Option<(f64, f64)>

Returns the two distinct roots of this quadratic, but only if the discriminant is positive.

Trait Implementations§

Source§

impl<const N: usize> Add<&Poly<N>> for Poly<N>

Source§

type Output = Poly<N>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &Poly<N>) -> Poly<N>

Performs the + operation. Read more
Source§

impl<const N: usize> Add<Poly<N>> for &Poly<N>

Source§

type Output = Poly<N>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Poly<N>) -> Poly<N>

Performs the + operation. Read more
Source§

impl<const N: usize> Add for Poly<N>

Source§

type Output = Poly<N>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Poly<N>) -> Poly<N>

Performs the + operation. Read more
Source§

impl<const N: usize> AddAssign<&Poly<N>> for Poly<N>

Source§

fn add_assign(&mut self, rhs: &Poly<N>)

Performs the += operation. Read more
Source§

impl<const N: usize> AddAssign for Poly<N>

Source§

fn add_assign(&mut self, rhs: Poly<N>)

Performs the += operation. Read more
Source§

impl<const N: usize> Clone for Poly<N>

Source§

fn clone(&self) -> Poly<N>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const N: usize> Debug for Poly<N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const N: usize> Div<f64> for &Poly<N>

Source§

type Output = Poly<N>

The resulting type after applying the / operator.
Source§

fn div(self, scale: f64) -> Poly<N>

Performs the / operation. Read more
Source§

impl<const N: usize> Div<f64> for Poly<N>

Source§

type Output = Poly<N>

The resulting type after applying the / operator.
Source§

fn div(self, scale: f64) -> Poly<N>

Performs the / operation. Read more
Source§

impl<const N: usize> DivAssign<f64> for Poly<N>

Source§

fn div_assign(&mut self, scale: f64)

Performs the /= operation. Read more
Source§

impl<const N: usize> Mul<f64> for &Poly<N>

Source§

type Output = Poly<N>

The resulting type after applying the * operator.
Source§

fn mul(self, scale: f64) -> Poly<N>

Performs the * operation. Read more
Source§

impl<const N: usize> Mul<f64> for Poly<N>

Source§

type Output = Poly<N>

The resulting type after applying the * operator.
Source§

fn mul(self, scale: f64) -> Poly<N>

Performs the * operation. Read more
Source§

impl<const N: usize> MulAssign<f64> for Poly<N>

Source§

fn mul_assign(&mut self, scale: f64)

Performs the *= operation. Read more
Source§

impl<const N: usize> PartialEq for Poly<N>

Source§

fn eq(&self, other: &Poly<N>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<const N: usize> PartialOrd for Poly<N>

Source§

fn partial_cmp(&self, other: &Poly<N>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<const N: usize> Sub<&Poly<N>> for Poly<N>

Source§

type Output = Poly<N>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &Poly<N>) -> Poly<N>

Performs the - operation. Read more
Source§

impl<const N: usize> Sub<Poly<N>> for &Poly<N>

Source§

type Output = Poly<N>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Poly<N>) -> Poly<N>

Performs the - operation. Read more
Source§

impl<const N: usize> Sub for Poly<N>

Source§

type Output = Poly<N>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Poly<N>) -> Poly<N>

Performs the - operation. Read more
Source§

impl<const N: usize> SubAssign<&Poly<N>> for Poly<N>

Source§

fn sub_assign(&mut self, rhs: &Poly<N>)

Performs the -= operation. Read more
Source§

impl<const N: usize> SubAssign for Poly<N>

Source§

fn sub_assign(&mut self, rhs: Poly<N>)

Performs the -= operation. Read more
Source§

impl<const N: usize> Copy for Poly<N>

Source§

impl<const N: usize> StructuralPartialEq for Poly<N>

Auto Trait Implementations§

§

impl<const N: usize> Freeze for Poly<N>

§

impl<const N: usize> RefUnwindSafe for Poly<N>

§

impl<const N: usize> Send for Poly<N>

§

impl<const N: usize> Sync for Poly<N>

§

impl<const N: usize> Unpin for Poly<N>

§

impl<const N: usize> UnwindSafe for Poly<N>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.