Struct ohsl::polynomial::Polynomial
source · pub struct Polynomial<T> { /* private fields */ }
Implementations§
source§impl<T: Copy + Clone + Number + Signed + Debug> Polynomial<T>
impl<T: Copy + Clone + Number + Signed + Debug> Polynomial<T>
sourcepub fn polydiv(
&self,
v: &Polynomial<T>
) -> Result<(Polynomial<T>, Polynomial<T>), &'static str>
pub fn polydiv( &self, v: &Polynomial<T> ) -> Result<(Polynomial<T>, Polynomial<T>), &'static str>
Divide the polynomial by another polynomial to get a quotient and remainder
Examples found in repository?
examples/polynomial.rs (line 32)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
fn main() {
println!("----- Polynomials -----");
// Create a new polynomial
let p = Polynomial::<f64>::new( vec![ 1.0, 2.0, 3.0 ] );
println!( " * p(x) = {}", p );
println!( " * p[0] = {}", p[0] );
println!( " * p[1] = {}", p[1] );
println!( " * p[2] = {}", p[2] );
println!( " * p is of degree {}", p.degree().unwrap() );
// Evaluate the polynomial at a given point
println!( " * p(0.5) = {}", p.eval( 0.5 ) );
println!( " * p(1.0) = {}", p.eval( 1.0 ) );
// Determine the roots of the polynomial
let roots = p.roots( true );
println!( " * p(x) = {} = 0 has {} roots", p, roots.size() );
println!( " * x0 = {:.6} {:.6} i", roots[0].real, roots[0].imag );
println!( " * x1 = {:.6} +{:.6} i", roots[1].real, roots[1].imag );
// Arithmetic
let q = Polynomial::cubic( 3.0, 5.0, 4.0, 0.0 );
println!( " * q(x) = {}", q );
println!( " * p(x) + q(x) = {}", p.clone() + q.clone() );
println!( " * p(x) - q(x) = {}", p.clone() - q.clone() );
println!( " * -p(x) = {}", -p.clone() );
println!( " * p(x) * q(x) = {}", p.clone() * q.clone() );
println!( " * p(x) * 3 = {}", p.clone() * 3.0 );
let result = q.polydiv( &p );
match result {
Ok( ( q, r ) ) => println!( " * q(x) / p(x) = {} remainder {}", q, r ),
Err( e ) => println!( " * q(x) / p(x) = {}", e )
}
// Derivatives
println!( " * p'(x) = {}", p.derivative() );
println!( " * p''(x) = {}", p.derivative_n( 2 ) );
println!( " * p'(0.5) = {}", p.derivative_at( 0.5, 1 ) );
println!( " * p''(0.5) = {}", p.derivative_at( 0.5, 2 ) );
println!( "--- FINISHED ---" );
}
source§impl<T> Polynomial<T>
impl<T> Polynomial<T>
sourcepub fn new(coeffs: Vec<T>) -> Self
pub fn new(coeffs: Vec<T>) -> Self
Create a new polynomial
Examples found in repository?
examples/polynomial.rs (line 7)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
fn main() {
println!("----- Polynomials -----");
// Create a new polynomial
let p = Polynomial::<f64>::new( vec![ 1.0, 2.0, 3.0 ] );
println!( " * p(x) = {}", p );
println!( " * p[0] = {}", p[0] );
println!( " * p[1] = {}", p[1] );
println!( " * p[2] = {}", p[2] );
println!( " * p is of degree {}", p.degree().unwrap() );
// Evaluate the polynomial at a given point
println!( " * p(0.5) = {}", p.eval( 0.5 ) );
println!( " * p(1.0) = {}", p.eval( 1.0 ) );
// Determine the roots of the polynomial
let roots = p.roots( true );
println!( " * p(x) = {} = 0 has {} roots", p, roots.size() );
println!( " * x0 = {:.6} {:.6} i", roots[0].real, roots[0].imag );
println!( " * x1 = {:.6} +{:.6} i", roots[1].real, roots[1].imag );
// Arithmetic
let q = Polynomial::cubic( 3.0, 5.0, 4.0, 0.0 );
println!( " * q(x) = {}", q );
println!( " * p(x) + q(x) = {}", p.clone() + q.clone() );
println!( " * p(x) - q(x) = {}", p.clone() - q.clone() );
println!( " * -p(x) = {}", -p.clone() );
println!( " * p(x) * q(x) = {}", p.clone() * q.clone() );
println!( " * p(x) * 3 = {}", p.clone() * 3.0 );
let result = q.polydiv( &p );
match result {
Ok( ( q, r ) ) => println!( " * q(x) / p(x) = {} remainder {}", q, r ),
Err( e ) => println!( " * q(x) / p(x) = {}", e )
}
// Derivatives
println!( " * p'(x) = {}", p.derivative() );
println!( " * p''(x) = {}", p.derivative_n( 2 ) );
println!( " * p'(0.5) = {}", p.derivative_at( 0.5, 1 ) );
println!( " * p''(0.5) = {}", p.derivative_at( 0.5, 2 ) );
println!( "--- FINISHED ---" );
}
sourcepub fn quadratic(a: T, b: T, c: T) -> Self
pub fn quadratic(a: T, b: T, c: T) -> Self
Create a new quadratic polynomial of the form ax^2 + bx + c
sourcepub fn cubic(a: T, b: T, c: T, d: T) -> Self
pub fn cubic(a: T, b: T, c: T, d: T) -> Self
Create a new cubic polynomial of the form ax^3 + bx^2 + cx + d
Examples found in repository?
examples/polynomial.rs (line 25)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
fn main() {
println!("----- Polynomials -----");
// Create a new polynomial
let p = Polynomial::<f64>::new( vec![ 1.0, 2.0, 3.0 ] );
println!( " * p(x) = {}", p );
println!( " * p[0] = {}", p[0] );
println!( " * p[1] = {}", p[1] );
println!( " * p[2] = {}", p[2] );
println!( " * p is of degree {}", p.degree().unwrap() );
// Evaluate the polynomial at a given point
println!( " * p(0.5) = {}", p.eval( 0.5 ) );
println!( " * p(1.0) = {}", p.eval( 1.0 ) );
// Determine the roots of the polynomial
let roots = p.roots( true );
println!( " * p(x) = {} = 0 has {} roots", p, roots.size() );
println!( " * x0 = {:.6} {:.6} i", roots[0].real, roots[0].imag );
println!( " * x1 = {:.6} +{:.6} i", roots[1].real, roots[1].imag );
// Arithmetic
let q = Polynomial::cubic( 3.0, 5.0, 4.0, 0.0 );
println!( " * q(x) = {}", q );
println!( " * p(x) + q(x) = {}", p.clone() + q.clone() );
println!( " * p(x) - q(x) = {}", p.clone() - q.clone() );
println!( " * -p(x) = {}", -p.clone() );
println!( " * p(x) * q(x) = {}", p.clone() * q.clone() );
println!( " * p(x) * 3 = {}", p.clone() * 3.0 );
let result = q.polydiv( &p );
match result {
Ok( ( q, r ) ) => println!( " * q(x) / p(x) = {} remainder {}", q, r ),
Err( e ) => println!( " * q(x) / p(x) = {}", e )
}
// Derivatives
println!( " * p'(x) = {}", p.derivative() );
println!( " * p''(x) = {}", p.derivative_n( 2 ) );
println!( " * p'(0.5) = {}", p.derivative_at( 0.5, 1 ) );
println!( " * p''(0.5) = {}", p.derivative_at( 0.5, 2 ) );
println!( "--- FINISHED ---" );
}
sourcepub fn degree(&self) -> Result<usize, &'static str>
pub fn degree(&self) -> Result<usize, &'static str>
Return the degree of the polynomial or an error if < 1.
Examples found in repository?
examples/polynomial.rs (line 12)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
fn main() {
println!("----- Polynomials -----");
// Create a new polynomial
let p = Polynomial::<f64>::new( vec![ 1.0, 2.0, 3.0 ] );
println!( " * p(x) = {}", p );
println!( " * p[0] = {}", p[0] );
println!( " * p[1] = {}", p[1] );
println!( " * p[2] = {}", p[2] );
println!( " * p is of degree {}", p.degree().unwrap() );
// Evaluate the polynomial at a given point
println!( " * p(0.5) = {}", p.eval( 0.5 ) );
println!( " * p(1.0) = {}", p.eval( 1.0 ) );
// Determine the roots of the polynomial
let roots = p.roots( true );
println!( " * p(x) = {} = 0 has {} roots", p, roots.size() );
println!( " * x0 = {:.6} {:.6} i", roots[0].real, roots[0].imag );
println!( " * x1 = {:.6} +{:.6} i", roots[1].real, roots[1].imag );
// Arithmetic
let q = Polynomial::cubic( 3.0, 5.0, 4.0, 0.0 );
println!( " * q(x) = {}", q );
println!( " * p(x) + q(x) = {}", p.clone() + q.clone() );
println!( " * p(x) - q(x) = {}", p.clone() - q.clone() );
println!( " * -p(x) = {}", -p.clone() );
println!( " * p(x) * q(x) = {}", p.clone() * q.clone() );
println!( " * p(x) * 3 = {}", p.clone() * 3.0 );
let result = q.polydiv( &p );
match result {
Ok( ( q, r ) ) => println!( " * q(x) / p(x) = {} remainder {}", q, r ),
Err( e ) => println!( " * q(x) / p(x) = {}", e )
}
// Derivatives
println!( " * p'(x) = {}", p.derivative() );
println!( " * p''(x) = {}", p.derivative_n( 2 ) );
println!( " * p'(0.5) = {}", p.derivative_at( 0.5, 1 ) );
println!( " * p''(0.5) = {}", p.derivative_at( 0.5, 2 ) );
println!( "--- FINISHED ---" );
}
sourcepub fn coeffs(&mut self) -> &mut Vec<T>
pub fn coeffs(&mut self) -> &mut Vec<T>
Return the coefficients of the polynomial as a mutable reference
sourcepub fn eval(&self, x: T) -> Twhere
T: Copy + Mul<Output = T> + Add<Output = T>,
pub fn eval(&self, x: T) -> Twhere T: Copy + Mul<Output = T> + Add<Output = T>,
Evaluate the polynomial at a given point
Examples found in repository?
examples/polynomial.rs (line 15)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
fn main() {
println!("----- Polynomials -----");
// Create a new polynomial
let p = Polynomial::<f64>::new( vec![ 1.0, 2.0, 3.0 ] );
println!( " * p(x) = {}", p );
println!( " * p[0] = {}", p[0] );
println!( " * p[1] = {}", p[1] );
println!( " * p[2] = {}", p[2] );
println!( " * p is of degree {}", p.degree().unwrap() );
// Evaluate the polynomial at a given point
println!( " * p(0.5) = {}", p.eval( 0.5 ) );
println!( " * p(1.0) = {}", p.eval( 1.0 ) );
// Determine the roots of the polynomial
let roots = p.roots( true );
println!( " * p(x) = {} = 0 has {} roots", p, roots.size() );
println!( " * x0 = {:.6} {:.6} i", roots[0].real, roots[0].imag );
println!( " * x1 = {:.6} +{:.6} i", roots[1].real, roots[1].imag );
// Arithmetic
let q = Polynomial::cubic( 3.0, 5.0, 4.0, 0.0 );
println!( " * q(x) = {}", q );
println!( " * p(x) + q(x) = {}", p.clone() + q.clone() );
println!( " * p(x) - q(x) = {}", p.clone() - q.clone() );
println!( " * -p(x) = {}", -p.clone() );
println!( " * p(x) * q(x) = {}", p.clone() * q.clone() );
println!( " * p(x) * 3 = {}", p.clone() * 3.0 );
let result = q.polydiv( &p );
match result {
Ok( ( q, r ) ) => println!( " * q(x) / p(x) = {} remainder {}", q, r ),
Err( e ) => println!( " * q(x) / p(x) = {}", e )
}
// Derivatives
println!( " * p'(x) = {}", p.derivative() );
println!( " * p''(x) = {}", p.derivative_n( 2 ) );
println!( " * p'(0.5) = {}", p.derivative_at( 0.5, 1 ) );
println!( " * p''(0.5) = {}", p.derivative_at( 0.5, 2 ) );
println!( "--- FINISHED ---" );
}
source§impl<T: Clone + Copy + Zero + Mul<Output = T> + Add<Output = T>> Polynomial<T>
impl<T: Clone + Copy + Zero + Mul<Output = T> + Add<Output = T>> Polynomial<T>
sourcepub fn derivative(&self) -> Polynomial<T>
pub fn derivative(&self) -> Polynomial<T>
Return the derivative of the polynomial
Examples found in repository?
examples/polynomial.rs (line 39)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
fn main() {
println!("----- Polynomials -----");
// Create a new polynomial
let p = Polynomial::<f64>::new( vec![ 1.0, 2.0, 3.0 ] );
println!( " * p(x) = {}", p );
println!( " * p[0] = {}", p[0] );
println!( " * p[1] = {}", p[1] );
println!( " * p[2] = {}", p[2] );
println!( " * p is of degree {}", p.degree().unwrap() );
// Evaluate the polynomial at a given point
println!( " * p(0.5) = {}", p.eval( 0.5 ) );
println!( " * p(1.0) = {}", p.eval( 1.0 ) );
// Determine the roots of the polynomial
let roots = p.roots( true );
println!( " * p(x) = {} = 0 has {} roots", p, roots.size() );
println!( " * x0 = {:.6} {:.6} i", roots[0].real, roots[0].imag );
println!( " * x1 = {:.6} +{:.6} i", roots[1].real, roots[1].imag );
// Arithmetic
let q = Polynomial::cubic( 3.0, 5.0, 4.0, 0.0 );
println!( " * q(x) = {}", q );
println!( " * p(x) + q(x) = {}", p.clone() + q.clone() );
println!( " * p(x) - q(x) = {}", p.clone() - q.clone() );
println!( " * -p(x) = {}", -p.clone() );
println!( " * p(x) * q(x) = {}", p.clone() * q.clone() );
println!( " * p(x) * 3 = {}", p.clone() * 3.0 );
let result = q.polydiv( &p );
match result {
Ok( ( q, r ) ) => println!( " * q(x) / p(x) = {} remainder {}", q, r ),
Err( e ) => println!( " * q(x) / p(x) = {}", e )
}
// Derivatives
println!( " * p'(x) = {}", p.derivative() );
println!( " * p''(x) = {}", p.derivative_n( 2 ) );
println!( " * p'(0.5) = {}", p.derivative_at( 0.5, 1 ) );
println!( " * p''(0.5) = {}", p.derivative_at( 0.5, 2 ) );
println!( "--- FINISHED ---" );
}
sourcepub fn derivative_n(&self, n: usize) -> Polynomial<T>
pub fn derivative_n(&self, n: usize) -> Polynomial<T>
Return the nth derivative of the polynomial
Examples found in repository?
examples/polynomial.rs (line 40)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
fn main() {
println!("----- Polynomials -----");
// Create a new polynomial
let p = Polynomial::<f64>::new( vec![ 1.0, 2.0, 3.0 ] );
println!( " * p(x) = {}", p );
println!( " * p[0] = {}", p[0] );
println!( " * p[1] = {}", p[1] );
println!( " * p[2] = {}", p[2] );
println!( " * p is of degree {}", p.degree().unwrap() );
// Evaluate the polynomial at a given point
println!( " * p(0.5) = {}", p.eval( 0.5 ) );
println!( " * p(1.0) = {}", p.eval( 1.0 ) );
// Determine the roots of the polynomial
let roots = p.roots( true );
println!( " * p(x) = {} = 0 has {} roots", p, roots.size() );
println!( " * x0 = {:.6} {:.6} i", roots[0].real, roots[0].imag );
println!( " * x1 = {:.6} +{:.6} i", roots[1].real, roots[1].imag );
// Arithmetic
let q = Polynomial::cubic( 3.0, 5.0, 4.0, 0.0 );
println!( " * q(x) = {}", q );
println!( " * p(x) + q(x) = {}", p.clone() + q.clone() );
println!( " * p(x) - q(x) = {}", p.clone() - q.clone() );
println!( " * -p(x) = {}", -p.clone() );
println!( " * p(x) * q(x) = {}", p.clone() * q.clone() );
println!( " * p(x) * 3 = {}", p.clone() * 3.0 );
let result = q.polydiv( &p );
match result {
Ok( ( q, r ) ) => println!( " * q(x) / p(x) = {} remainder {}", q, r ),
Err( e ) => println!( " * q(x) / p(x) = {}", e )
}
// Derivatives
println!( " * p'(x) = {}", p.derivative() );
println!( " * p''(x) = {}", p.derivative_n( 2 ) );
println!( " * p'(0.5) = {}", p.derivative_at( 0.5, 1 ) );
println!( " * p''(0.5) = {}", p.derivative_at( 0.5, 2 ) );
println!( "--- FINISHED ---" );
}
sourcepub fn derivative_at(&self, x: T, n: usize) -> T
pub fn derivative_at(&self, x: T, n: usize) -> T
Return the nth derivative of the polynomial at a given point
Examples found in repository?
examples/polynomial.rs (line 41)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
fn main() {
println!("----- Polynomials -----");
// Create a new polynomial
let p = Polynomial::<f64>::new( vec![ 1.0, 2.0, 3.0 ] );
println!( " * p(x) = {}", p );
println!( " * p[0] = {}", p[0] );
println!( " * p[1] = {}", p[1] );
println!( " * p[2] = {}", p[2] );
println!( " * p is of degree {}", p.degree().unwrap() );
// Evaluate the polynomial at a given point
println!( " * p(0.5) = {}", p.eval( 0.5 ) );
println!( " * p(1.0) = {}", p.eval( 1.0 ) );
// Determine the roots of the polynomial
let roots = p.roots( true );
println!( " * p(x) = {} = 0 has {} roots", p, roots.size() );
println!( " * x0 = {:.6} {:.6} i", roots[0].real, roots[0].imag );
println!( " * x1 = {:.6} +{:.6} i", roots[1].real, roots[1].imag );
// Arithmetic
let q = Polynomial::cubic( 3.0, 5.0, 4.0, 0.0 );
println!( " * q(x) = {}", q );
println!( " * p(x) + q(x) = {}", p.clone() + q.clone() );
println!( " * p(x) - q(x) = {}", p.clone() - q.clone() );
println!( " * -p(x) = {}", -p.clone() );
println!( " * p(x) * q(x) = {}", p.clone() * q.clone() );
println!( " * p(x) * 3 = {}", p.clone() * 3.0 );
let result = q.polydiv( &p );
match result {
Ok( ( q, r ) ) => println!( " * q(x) / p(x) = {} remainder {}", q, r ),
Err( e ) => println!( " * q(x) / p(x) = {}", e )
}
// Derivatives
println!( " * p'(x) = {}", p.derivative() );
println!( " * p''(x) = {}", p.derivative_n( 2 ) );
println!( " * p'(0.5) = {}", p.derivative_at( 0.5, 1 ) );
println!( " * p''(0.5) = {}", p.derivative_at( 0.5, 2 ) );
println!( "--- FINISHED ---" );
}
source§impl Polynomial<f64>
impl Polynomial<f64>
sourcepub fn roots(&self, refine: bool) -> Vector<Cmplx>
pub fn roots(&self, refine: bool) -> Vector<Cmplx>
Find all the roots of the polynomial with f64 coefficients refine = true will refine the roots using Laguerre’s method
Examples found in repository?
examples/polynomial.rs (line 19)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
fn main() {
println!("----- Polynomials -----");
// Create a new polynomial
let p = Polynomial::<f64>::new( vec![ 1.0, 2.0, 3.0 ] );
println!( " * p(x) = {}", p );
println!( " * p[0] = {}", p[0] );
println!( " * p[1] = {}", p[1] );
println!( " * p[2] = {}", p[2] );
println!( " * p is of degree {}", p.degree().unwrap() );
// Evaluate the polynomial at a given point
println!( " * p(0.5) = {}", p.eval( 0.5 ) );
println!( " * p(1.0) = {}", p.eval( 1.0 ) );
// Determine the roots of the polynomial
let roots = p.roots( true );
println!( " * p(x) = {} = 0 has {} roots", p, roots.size() );
println!( " * x0 = {:.6} {:.6} i", roots[0].real, roots[0].imag );
println!( " * x1 = {:.6} +{:.6} i", roots[1].real, roots[1].imag );
// Arithmetic
let q = Polynomial::cubic( 3.0, 5.0, 4.0, 0.0 );
println!( " * q(x) = {}", q );
println!( " * p(x) + q(x) = {}", p.clone() + q.clone() );
println!( " * p(x) - q(x) = {}", p.clone() - q.clone() );
println!( " * -p(x) = {}", -p.clone() );
println!( " * p(x) * q(x) = {}", p.clone() * q.clone() );
println!( " * p(x) * 3 = {}", p.clone() * 3.0 );
let result = q.polydiv( &p );
match result {
Ok( ( q, r ) ) => println!( " * q(x) / p(x) = {} remainder {}", q, r ),
Err( e ) => println!( " * q(x) / p(x) = {}", e )
}
// Derivatives
println!( " * p'(x) = {}", p.derivative() );
println!( " * p''(x) = {}", p.derivative_n( 2 ) );
println!( " * p'(0.5) = {}", p.derivative_at( 0.5, 1 ) );
println!( " * p''(0.5) = {}", p.derivative_at( 0.5, 2 ) );
println!( "--- FINISHED ---" );
}
Trait Implementations§
source§impl<T: Clone> Clone for Polynomial<T>
impl<T: Clone> Clone for Polynomial<T>
source§impl<T: Debug> Debug for Polynomial<T>
impl<T: Debug> Debug for Polynomial<T>
source§impl<T: Display + Zero + PartialOrd + Signed> Display for Polynomial<T>
impl<T: Display + Zero + PartialOrd + Signed> Display for Polynomial<T>
source§impl<T> Index<usize> for Polynomial<T>
impl<T> Index<usize> for Polynomial<T>
source§impl<T> IndexMut<usize> for Polynomial<T>
impl<T> IndexMut<usize> for Polynomial<T>
Auto Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more