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,
impl<T> Polynomial<T>where
T: ComplexFloat + Debug,
Sourcepub fn build(coef: &[T]) -> Result<Self>
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²Sourcepub fn to_trimmed(&self) -> Self
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]);Sourcepub fn to_depressed_cubic(&self) -> Result<Polynomial<f64>>
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();Sourcepub fn eval(&self, x: T) -> T
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);Sourcepub fn eval_derivs(&self, x: T, n: usize) -> Vec<T>
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]);Sourcepub fn solve_real_quadratic(&self) -> Result<Vec<f64>>
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:
- the Polynomial is not of order 2
- one of the coefficients is not real
- 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);Sourcepub fn solve_real_cubic(&self) -> Result<Vec<f64>>
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:
- the Polynomial is not of order 3
- one of the coefficients is not real
- 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>
impl<T> Clone for Polynomial<T>
Source§fn clone(&self) -> Polynomial<T>
fn clone(&self) -> Polynomial<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more