pub struct Poly<T: Scalar>(_);
Implementations§
source§impl<T: Scalar> Poly<T>
impl<T: Scalar> Poly<T>
pub fn new(coeffs: &[Complex<T>]) -> Self
pub const fn from_dvector(value: DVector<Complex<T>>) -> Self
sourcepub fn from_complex_slice(value: &[Complex<T>]) -> Self
pub fn from_complex_slice(value: &[Complex<T>]) -> Self
The same as Poly::new()
pub fn from_complex_vec(value: Vec<Complex<T>>) -> Self
pub fn from_real_slice(value: &[T]) -> Self
pub fn from_real_vec(value: Vec<T>) -> Self
sourcepub fn from_roots(roots: &[Complex<T>]) -> Self
pub fn from_roots(roots: &[Complex<T>]) -> Self
Monic polynomial from its complex roots.
Examples
use rust_poly::Poly;
use num_complex::Complex;
use num_traits::{Zero, One};
let p = Poly::from_roots(&[Complex::new(-1.0, 0.0), Complex::zero(), Complex::one()]);
assert_eq!(p, Poly::new(&[Complex::zero(), Complex::new(-1.0, 0.0), Complex::zero(), Complex::one()]))
sourcepub fn line(offset: Complex<T>, slope: Complex<T>) -> Self
pub fn line(offset: Complex<T>, slope: Complex<T>) -> Self
Linear function as a polynomial.
Examples
use rust_poly::Poly;
use num_complex::Complex;
use num_traits::{One, Zero};
assert_eq!(Poly::line(Complex::one(), Complex::new(-1.0, 0.0)).eval_point(Complex::one()), Complex::zero());
sourcepub fn line_from_points(
p1: (Complex<T>, Complex<T>),
p2: (Complex<T>, Complex<T>)
) -> Self
pub fn line_from_points( p1: (Complex<T>, Complex<T>), p2: (Complex<T>, Complex<T>) ) -> Self
Line between two points with complex coordinates.
Note that the points are determined by two complex numbers, so they are in a four dimensional space. Leave the imaginary component as zero for lines in a 2D plane.
Examples
use rust_poly::Poly;
use num_complex::Complex;
use num_traits::{One, Zero};
let p1 = (Complex::new(-1.0, 0.0), Complex::new(2.0, 0.0));
let p2 = (Complex::new(2.0, 0.0), Complex::new(-1.0, 0.0));
assert_eq!(Poly::line_from_points(p1, p2).eval_point(Complex::one()), Complex::zero());
sourcepub fn term(coeff: Complex<T>, degree: u32) -> Self
pub fn term(coeff: Complex<T>, degree: u32) -> Self
Create a polynomial from a single term (coefficient + degree)
Examples
use rust_poly::{poly, Poly};
use num_complex::Complex;
use num_traits::One;
assert_eq!(Poly::term(Complex::one(), 3), poly![0.0, 0.0, 0.0, 1.0]);
sourcepub fn get_term(&self, degree: u32) -> Option<Self>
pub fn get_term(&self, degree: u32) -> Option<Self>
Get the nth term of the polynomial as a new polynomial
Will return None if out of bounds.
Examples
use rust_poly::{poly, Poly};
use num_complex::Complex;
use num_traits::One;
let p = poly![1.0, 2.0, 3.0];
assert_eq!(p.get_term(1).unwrap(), poly![0.0, 2.0]);
sourcepub fn cheby(n: usize) -> Self
pub fn cheby(n: usize) -> Self
Get the nth chebyshev polynomial
use rust_poly::{poly, Poly};
assert_eq!(Poly::cheby(2), poly![-1.0, 0.0, 2.0]);
assert_eq!(Poly::cheby(3), poly![0.0, -3.0, 0.0, 4.0]);
assert_eq!(Poly::cheby(4), poly![1.0, 0.0, -8.0, 0.0, 8.0])
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
sourcepub fn eval_point(&self, x: Complex<T>) -> Complex<T>
pub fn eval_point(&self, x: Complex<T>) -> Complex<T>
Evaluate the polynomial at a single value of x
.
use rust_poly::Poly;
use num_complex::Complex;
let p = Poly::new(&[Complex::new(1.0, 0.0), Complex::new(2.0, 0.0), Complex::new(3.0, 0.0)]);
let x = Complex::new(1.0, 0.0);
assert_eq!(p.eval_point(x), Complex::new(6.0, 0.0));
sourcepub fn eval(&self, x: &DMatrix<Complex<T>>) -> DMatrix<Complex<T>>
pub fn eval(&self, x: &DMatrix<Complex<T>>) -> DMatrix<Complex<T>>
Evaluate the polynomial for each entry of a matrix.
sourcepub fn pow(&self, pow: u32) -> Self
pub fn pow(&self, pow: u32) -> Self
Raises a polynomial to an integer power.
use rust_poly::{poly, Poly};
use num_complex::Complex;
assert_eq!(poly![1.0, 2.0, 3.0].pow(2), poly![1.0, 4.0, 10.0, 12.0, 9.0]);
pub fn pow_usize(&self, pow: usize) -> Self
sourcepub fn roots(&self) -> Vec<Complex<T>>
pub fn roots(&self) -> Vec<Complex<T>>
Find the roots of a polynomial numerically.
Examples
use rust_poly::{poly, Poly};
use rust_poly::num_complex::Complex;
let p: Poly<f64> = poly![-6.0, 11.0, -6.0, 1.0];
let expected_roots = &[Complex::new(1.0, 0.0), Complex::new(2.0, 0.0), Complex::new(3.0, 0.0)];
let temp = p.roots(); // Rust is really annoying sometimes...
let calculated_roots = temp.as_slice();
// assert almost equal
assert!((expected_roots[0] - calculated_roots[0]).re.abs() < 0.000001);
assert!((expected_roots[1] - calculated_roots[1]).re.abs() < 0.000001);
assert!((expected_roots[2] - calculated_roots[2]).re.abs() < 0.000001);
sourcepub fn compose(&self, x: Self) -> Self
pub fn compose(&self, x: Self) -> Self
Compose two polynomials, returning a new polynomial.
Substitute the given polynomial x
into self
and expand the
result into a new polynomial.
Examples
use rust_poly::Poly;
use num_complex::Complex;
use num_traits::One;
let f = Poly::new(&[Complex::new(1.0, 0.0), Complex::new(2.0, 0.0)]);
let g = Poly::one();
assert_eq!(f.compose(g), f);
sourcepub fn div_rem(self, rhs: &Self) -> (Self, Self)
pub fn div_rem(self, rhs: &Self) -> (Self, Self)
Calculate the quotient and remainder uwing long division. More efficient than calculating them separately.
Panics
Panics if a division by zero is attempted
Examples
use rust_poly::Poly;
use num_complex::Complex;
use num_traits::identities::One;
let c1 = Poly::new(&[Complex::new(1.0, 0.0), Complex::new(2.0, 0.0), Complex::new(3.0, 0.0)]);
let c2 = Poly::new(&[Complex::new(3.0, 0.0), Complex::new(2.0, 0.0), Complex::new(1.0, 0.0)]);
let expected1 = (Poly::new(&[Complex::new(3.0, 0.0)]), Poly::new(&[Complex::new(-8.0, 0.0), Complex::new(-4.0, 0.0)]));
assert_eq!(c1.clone().div_rem(&c2), expected1);
pub fn as_slice(&self) -> &[Complex<T>]
pub fn as_mut_slice(&mut self) -> &mut [Complex<T>]
pub fn as_ptr(&self) -> *const Complex<T>
pub fn as_mut_ptr(&mut self) -> *mut Complex<T>
pub fn as_view(&self) -> DMatrixView<'_, Complex<T>>
pub fn as_view_mut(&mut self) -> DMatrixViewMut<'_, Complex<T>>
pub fn to_vec(&self) -> Vec<Complex<T>>
pub fn to_dvector(&self) -> DVector<Complex<T>>
Trait Implementations§
source§impl<T: Scalar> From<Matrix<Complex<T>, Dyn, Const<1>, VecStorage<Complex<T>, Dyn, Const<1>>>> for Poly<T>
impl<T: Scalar> From<Matrix<Complex<T>, Dyn, Const<1>, VecStorage<Complex<T>, Dyn, Const<1>>>> for Poly<T>
source§impl<T: Scalar> Mul<Poly<T>> for Poly<T>
impl<T: Scalar> Mul<Poly<T>> for Poly<T>
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 two polynomials.
Examples
use rust_poly::{poly, Poly};
use num_complex::Complex;
let c1 = poly![1.0, 2.0, 3.0];
let c2 = poly![3.0, 2.0, 1.0];
assert_eq!(c1.clone() - c2.clone(), poly![-2.0, 0.0, 2.0]);
assert_eq!(c2 - c1, poly![2.0, 0.0, -2.0]);
impl<T: Eq + Scalar> Eq for Poly<T>
impl<T: Scalar> StructuralEq for Poly<T>
impl<T: Scalar> StructuralPartialEq for Poly<T>
Auto Trait Implementations§
impl<T> RefUnwindSafe for Poly<T>where T: RefUnwindSafe,
impl<T> Send for Poly<T>
impl<T> Sync for Poly<T>
impl<T> Unpin for Poly<T>where T: Unpin,
impl<T> UnwindSafe for Poly<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
§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.