pub struct Poly<T: Scalar>(/* private fields */);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;
use num::{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;
use num::{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;
use num::{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;
use num::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;
use num::One;
let p = poly![1.0, 2.0, 3.0];
assert_eq!(p.get_term(1).unwrap(), poly![0.0, 2.0]);sourcepub fn iter(&self) -> Iter<'_, Complex<T>>
pub fn iter(&self) -> Iter<'_, Complex<T>>
Iterate over coefficients, from the least significant
sourcepub fn iter_mut(&mut self) -> IterMut<'_, Complex<T>>
pub fn iter_mut(&mut self) -> IterMut<'_, Complex<T>>
Iterate over coefficients, from the least significant
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])sourcepub fn bessel(n: usize) -> Option<Self>
pub fn bessel(n: usize) -> Option<Self>
Get the nth Bessel polynomial
pub fn reverse_bessel(n: usize) -> Option<Self>
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;
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;
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 num::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, poly};
use num::{One, Complex};
let f = poly![1.0, 2.0];
let g = Poly::one();
assert_eq!(f.clone().compose(g), f);sourcepub fn almost_zero(&self, tolerance: &T) -> bool
pub fn almost_zero(&self, tolerance: &T) -> bool
Returns true if every coefficient in the polynomial is smaller than the tolerance (using complex norm).
Examples
use rust_poly::{Poly, poly};
assert!(poly![0.01, -0.01].almost_zero(&0.1));sourcepub fn div_rem(self, rhs: &Self) -> Option<(Self, Self)>
pub fn div_rem(self, rhs: &Self) -> Option<(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, poly};
use num::{Complex, One};
let c1 = poly![1.0, 2.0, 3.0];
let c2 = poly![3.0, 2.0, 1.0];
let expected1 = (poly![3.0], poly![-8.0, -4.0]);
assert_eq!(c1.clone().div_rem(&c2).unwrap(), 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> CheckedDiv for Poly<T>
impl<T: Scalar> CheckedDiv for Poly<T>
source§fn checked_div(&self, rhs: &Self) -> Option<Self>
fn checked_div(&self, rhs: &Self) -> Option<Self>
None is returned.source§impl<T: Scalar> CheckedRem for Poly<T>
impl<T: Scalar> CheckedRem for Poly<T>
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: PartialEq + Scalar> PartialEq for Poly<T>
impl<T: PartialEq + Scalar> PartialEq for Poly<T>
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.