Polynomial

Struct Polynomial 

Source
pub struct Polynomial<T>
where T: Debug,
{ pub coef: Vec<T>, }
Expand description

Representation of a polynomial.

A polynomial of degree n, represented with a Vec of length n+1 containing the coefficients c[i]:

P(x) = c[0] + c[1]x + c[2]x² + … + c[n−1]xⁿ⁻¹ + c[n]xⁿ

Fields§

§coef: Vec<T>

The polynomial’s coefficients, from constant to leading term.

Implementations§

Source§

impl<T> Polynomial<T>
where T: ComplexFloat + Debug,

Source

pub fn new() -> Self

Creates a new Polynomial with no terms (zero polynomial).

Source

pub fn build(coef: &[T]) -> Result<Self>

Creates a new Polynomial from the given coefficients.

§Example
let poly = Polynomial::build(&[1.0, 4.0, 3.0])?; // 1+4x+3x²
Source

pub fn to_trimmed(&self) -> Self

Trims the higher order terms with 0 coefficient.

§Example
// 0+x+0+2x³+0 −> x+2x³
let poly = Polynomial::build(&[0.0, 1.0, 0.0, 2.0, 0.0])?.to_trimmed();

assert_eq!(poly.coef, &[0.0, 1.0, 0.0, 2.0]);
Source

pub fn to_monic(&self) -> Self

Converts a general polynomial to a monic polynomial: ax³ + bx² + cx + d −> x³ + a’x² + b’x + c’

§Example
let poly = Polynomial::build(&[30.0, 6.0, 3.0])?.to_monic();

assert_eq!(poly.coef, &[10.0, 2.0, 1.0]);
Source

pub fn to_depressed_cubic(&self) -> Result<Polynomial<f64>>

Converts a general cubic polynomial to a depressed cubic polynomial: ax³ + bx³ + cx + d −> t³ + pt + q, where t = x − b/3a

§Example
let poly = Polynomial::build(&[30.0, 6.0, 3.0])?.to_depressed_cubic();
Source

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

Evaluates the polynomial for the value x.

§Example
let poly = Polynomial::build(&[1.0, 2.0, 3.0])?;

assert_eq!(poly.eval(1.0), 6.0);
assert_eq!(poly.eval(-1.0), 2.0);
Source

pub fn eval_derivs(&self, x: T, n: usize) -> Vec<T>

Evaluates the polynomials first n derivatives (including the 0-th derivative, i.e. the polynomial’s value) for the value x.

The result is a vector holding the calculated derivatives:

[d⁰/dx⁰, d¹/dx¹, d²/dx², …, dⁿ/dxⁿ]

§Example
let poly = Polynomial::build(&[1.0, 2.0, 3.0])?;

assert_eq!(poly.eval_derivs(1.0, 4), &[6.0, 8.0, 6.0, 0.0]);
Source

pub fn solve_real_quadratic(&self) -> Result<Vec<f64>>

Calculates the real roots af a quadratic equation ax²+bx+c.

§Error

Returns an error in 3 cases:

  1. the Polynomial is not of order 2
  2. one of the coefficients is not real
  3. the Polynomial is constant, i.e. a=b=0
§Example
let poly = Polynomial::build(&[-20.0, 0.0, 5.0])?; // 5x²-20
let y = poly.solve_real_quadratic()?;
let expected = [2.0, -2.0];

assert_eq!(y, expected);
Source

pub fn solve_real_cubic(&self) -> Result<Vec<f64>>

Calculates the real roots af a quadratic equation ax³+bx²+cx+d.

The roots are returned in increasing order.

§Note

Due to finite precision, some double roots may be missed, and considered to be a pair of complex roots z = x ± i*EPSILON close to the real axis.

§Error

Returns an error in 3 cases:

  1. the Polynomial is not of order 3
  2. one of the coefficients is not real
  3. the Polynomial is constant, i.e. a=b=c=0
§Example
let poly = Polynomial::build(&[-6.0, 11.0, -6.0, 1.0])?; // x³-6x²+11x-6
let y = poly.solve_real_cubic()?;
let expected = [1.0, 2.0, 3.0];

assert_eq!(y, expected);

Trait Implementations§

Source§

impl<T> Clone for Polynomial<T>
where T: Debug + Clone,

Source§

fn clone(&self) -> Polynomial<T>

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<T> Debug for Polynomial<T>
where T: Debug,

Source§

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

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

impl<T> Default for Polynomial<T>
where T: ComplexFloat + Debug,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Polynomial<T>

§

impl<T> RefUnwindSafe for Polynomial<T>
where T: RefUnwindSafe,

§

impl<T> Send for Polynomial<T>
where T: Send,

§

impl<T> Sync for Polynomial<T>
where T: Sync,

§

impl<T> Unpin for Polynomial<T>
where T: Unpin,

§

impl<T> UnwindSafe for Polynomial<T>
where T: UnwindSafe,

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.