pub struct Poly<T: Scalar>(_);
Expand description
polynomial as a list of coefficients of terms of descending degree
Implementations§
source§impl<T: Scalar> Poly<T>
impl<T: Scalar> Poly<T>
sourcepub fn term(coeff: T, degree: usize) -> Self
pub fn term(coeff: T, degree: usize) -> Self
Creates a polynomial with a single term of degree n
.
Examples
use ndarray::prelude::*;
let t1 = Poly::term(1i32, 0);
let t2 = Poly::term(2i32, 1);
let t3 = Poly::term(3i32, 2);
assert_eq!(t1, Poly::new(array![1]));
assert_eq!(t2, Poly::new(array![2, 0]));
assert_eq!(t3, Poly::new(array![3, 0, 0]));
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Length of the polynomial
Note that this does not include leading zeros, as polynomials are stored in their normalized form internally.
sourcepub fn eval<D: Dimension>(&self, x: Array<T, D>) -> Array<T, D>
pub fn eval<D: Dimension>(&self, x: Array<T, D>) -> Array<T, D>
Evaluate a polynomial at a specific input value x
. This may be an
ndarray of any dimension
Examples
Evaluate a real polynomial at real points
use ndarray::prelude::*;
// x^2 + 2x + 1
let p = Poly::new(array![1, 2, 1]);
let x = array![-1, 0, 1];
let y = p.eval(x);
assert_eq!(y, array![0, 1, 4]);
Evaluate a complex polynomial at complex points
use ndarray::prelude::*;
use num_complex::Complex64;
// (2+i)x^2 + 2i
let p = Poly::new(array![
Complex64::new(2.0, 1.0),
Complex64::new(0.0, 0.0),
Complex64::new(0.0, 2.0),
]);
let x = array![Complex64::new(1.0, 0.0), Complex64::new(0.0, 1.0)];
let y = p.eval(x);
assert_eq!(y, array![Complex64::new(2.0, 3.0), Complex64::new(-2.0, 1.0)]);
sourcepub fn div_rem(&self, rhs: Self) -> (Self, Self)
pub fn div_rem(&self, rhs: Self) -> (Self, Self)
Computes the quotient and remainder of a polynomial division
Examples
Divide two real polynomials
use ndarray::prelude::*;
let p1 = Poly::new(array![3.0, 5.0, 2.0]);
let p2 = Poly::new(array![2.0, 1.0]);
let (q, r) = p1.div_rem(p2);
assert_eq!(q, Poly::new(array![1.5, 1.75]));
assert_eq!(r, Poly::new(array![0.25]));
Divide two complex polynomials
use ndarray::prelude::*;
use num_complex::Complex64;
let p1 = Poly::term(Complex64::new(1.0, 1.0), 2);
let p2 = Poly::term(Complex64::new(1.0, -1.0), 0);
let (q, r) = p1.div_rem(p2);
assert_eq!(q, Poly::term(Complex64::new(0.0, 1.0), 2));
assert_eq!(r, Poly::new(array![]));
Trait Implementations§
source§impl<T: Scalar> Add<Poly<T>> for Poly<T>
impl<T: Scalar> Add<Poly<T>> for Poly<T>
source§fn add(self, rhs: Self) -> Self::Output
fn add(self, rhs: Self) -> Self::Output
Add toghether two polynomials
Examples
Add polynomials of various lengths
use ndarray::prelude::*;
let p1 = Poly::new(array![1.0, 0.0]);
let p2 = Poly::new(array![1.0]);
assert_eq!(p1.clone() + p1.clone(), Poly::new(array![2.0, 0.0]));
assert_eq!(p2.clone() + p1.clone(), Poly::new(array![1.0, 1.0]));
assert_eq!(p1 + p2, Poly::new(array![1.0, 1.0]));
Add three terms to form a polynomial
use ndarray::prelude::*;
let t1 = Poly::term(1, 0);
let t2 = Poly::term(2, 1);
let t3 = Poly::term(3, 2);
let sum = t1 + t2 + t3;
assert_eq!(sum, Poly::new(array![3, 2, 1]));
source§impl<T: Scalar> Mul<Poly<T>> for Poly<T>
impl<T: Scalar> Mul<Poly<T>> for Poly<T>
source§fn mul(self, rhs: Self) -> Self::Output
fn mul(self, rhs: Self) -> Self::Output
Multiplies two polynomials together
Examples
Convolve two polynomials
use ndarray::prelude::*;
let p1 = Poly::new(array![1.0, 2.0, 3.0]);
let p2 = Poly::new(array![9.0, 5.0, 1.0]);
let prod = p1 * p2;
assert_eq!(prod, Poly::new(array![9.0, 23.0, 38.0, 17.0, 3.0]));
Scalar multiplication
use ndarray::prelude::*;
let p1 = Poly::term(3, 0);
let p2 = Poly::new(array![1, 1]);
let prod1 = p1.clone() * p2.clone();
let prod2 = p2.clone() * p1.clone();
assert_eq!(prod1, Poly::new(array![3, 3]));
assert_eq!(prod2, Poly::new(array![3, 3]));
source§impl<T: PartialEq + Scalar> PartialEq<Poly<T>> for Poly<T>
impl<T: PartialEq + Scalar> PartialEq<Poly<T>> for Poly<T>
source§impl<T: Scalar> Sub<Poly<T>> for Poly<T>
impl<T: Scalar> Sub<Poly<T>> for Poly<T>
source§fn sub(self, rhs: Self) -> Self::Output
fn sub(self, rhs: Self) -> Self::Output
Subtract one polynomial from another
Examples
Subtract polynomials of various lengths
use ndarray::prelude::*;
let p1 = Poly::new(array![1.0, 0.0]);
let p2 = Poly::new(array![1.0]);
assert_eq!(p1.clone() - p1.clone(), Poly::new(array![]));
assert_eq!(p2.clone() - p1.clone(), Poly::new(array![-1.0, 1.0]));
assert_eq!(p1 - p2, Poly::new(array![1.0, -1.0]));