pub struct Poly<T: RealScalar>(/* private fields */);
Implementations§
Source§impl<T: RealScalar> Poly<T>
impl<T: RealScalar> Poly<T>
Source§impl<T: RealScalar> Poly<T>
impl<T: RealScalar> Poly<T>
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>
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
pub fn to_vec(&self) -> Vec<Complex<T>>
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
pub fn from_real_iterator(coeffs: impl Iterator<Item = T>) -> Self
pub fn from_complex_iterator(coeffs: impl Iterator<Item = Complex<T>>) -> Self
Source§impl<T: RealScalar> Poly<T>
impl<T: RealScalar> Poly<T>
Sourcepub fn div_rem(self, other: &Self) -> Option<(Self, Self)>
pub fn div_rem(self, other: &Self) -> Option<(Self, Self)>
Calculate the quotient and remainder using 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);
Source§impl<T: RealScalar> Poly<T>
impl<T: RealScalar> Poly<T>
Sourcepub fn coeffs(&self) -> &[Complex<T>]
pub fn coeffs(&self) -> &[Complex<T>]
Return a slice containing the coefficients in ascending order of degree
This is an alias for Poly::as_slice
for API consistency.
Sourcepub fn coeffs_mut(&mut self) -> &mut [Complex<T>]
pub fn coeffs_mut(&mut self) -> &mut [Complex<T>]
Return a mutable slice containing the coefficient in ascending order of degree
This is an alias for Poly::as_mut_slice()
for API consistency.
Source§impl<T: RealScalar> Poly<T>
impl<T: RealScalar> Poly<T>
Sourcepub fn roots(&self, epsilon: T, max_iter: usize) -> Result<T>
pub fn roots(&self, epsilon: T, max_iter: usize) -> Result<T>
A convenient way of finding roots, with a pre-configured root finder. Should work well for most real polynomials of low degree.
Use Poly::roots_expert
if you need more control over performance or accuracy.
§Errors
- Solver did not converge within
max_iter
iterations - Some other edge-case was encountered which could not be handled (please report this, as we can make this solver more robust!)
Sourcepub fn roots_expert(
&self,
epsilon: T,
max_iter: usize,
_min_iter: usize,
polishing_mode: PolishingMode<T>,
multiples_handling_mode: MultiplesHandlingMode<T>,
initial_guess_pool: &[Complex<T>],
initial_guess_mode: InitialGuessMode<T>,
) -> Result<T>
pub fn roots_expert( &self, epsilon: T, max_iter: usize, _min_iter: usize, polishing_mode: PolishingMode<T>, multiples_handling_mode: MultiplesHandlingMode<T>, initial_guess_pool: &[Complex<T>], initial_guess_mode: InitialGuessMode<T>, ) -> Result<T>
Highly configurable root finder.
Poly::roots
will often be good enough, but you may know something
about the polynomial you are factoring that allows you to tweak the
settings.
§Errors
- Solver did not converge within
max_iter
iterations - Some other edge-case was encountered which could not be handled (please report this, as we can make this solver more robust!)
- The combination of parameters that was provided is invalid
Source§impl<T: RealScalar + FromPrimitive> Poly<T>
impl<T: RealScalar + FromPrimitive> Poly<T>
pub fn cheby(n: usize) -> Self
Sourcepub fn cheby1(n: usize) -> Self
pub fn cheby1(n: usize) -> Self
Get the nth Chebyshev polynomial of the first kind.
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>
Source§impl<T: RealScalar> Poly<T>
impl<T: RealScalar> Poly<T>
Source§impl<T: RealScalar> Poly<T>
impl<T: RealScalar> Poly<T>
pub fn new(coeffs: &[Complex<T>]) -> Self
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]);
pub fn len(&self) -> usize
Sourcepub fn degree_usize(&self) -> usize
pub fn degree_usize(&self) -> usize
Return the degree as usize
.
Note that unlike Poly::degree
, this will saturate at 0 for zero
polynomials. As the degree of zero polynomials is undefined.
Sourcepub fn degree(&self) -> i64
pub fn degree(&self) -> i64
The degree of a polynomial (the maximum exponent)
Note that this will return -1
for zero polynomials. The degree of
zero polynomials is undefined, but we use the -1
convention adopted
by some authors.
§Panics
May theoretically panic for absurdly large polynomials, however such polynomials will likely not fit in memory anyway.\n\n
pub fn is_empty(&self) -> bool
Sourcepub fn pow(self, pow: u32) -> Self
pub fn pow(self, pow: u32) -> Self
Raises a polynomial to an integer power.
§Caveats
We adopt the convention that $0^0=1$, even though some authors leave this case undefined. We believe this to be more useful as it naturally arises in integer exponentiation when defined as repeated multiplication (as we implement it).
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]);
Sourcepub fn conj(&self) -> Self
pub fn conj(&self) -> Self
Compute the conjugate polynomial, that is a polynomial where every coefficient is conjugated.
To evaluate a conjugate polynomial, you must evaluate it at the conjugate
of the input, i.e. poly.conj().eval(z.conj())
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]);
Source§impl<T: RealScalar + PartialOrd> Poly<T>
impl<T: RealScalar + PartialOrd> Poly<T>
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 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);
Source§impl<T: RealScalar> Poly<T>
impl<T: RealScalar> Poly<T>
Sourcepub fn eval_multiple(&self, points: &[Complex<T>], out: &mut [Complex<T>])
pub fn eval_multiple(&self, points: &[Complex<T>], out: &mut [Complex<T>])
Evaluate the polynomial for each entry of a slice.
Sourcepub fn eval(&self, x: Complex<T>) -> Complex<T>
pub fn eval(&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));
Source§impl<T: RealScalar + PartialOrd> Poly<T>
impl<T: RealScalar + PartialOrd> Poly<T>
Source§impl<T: RealScalar> Poly<T>
impl<T: RealScalar> Poly<T>
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));
Trait Implementations§
Source§impl<T: RealScalar> Add for Poly<T>
impl<T: RealScalar> Add for Poly<T>
Source§impl<T: RealScalar> CheckedDiv for Poly<T>
impl<T: RealScalar> 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: RealScalar> CheckedRem for Poly<T>
impl<T: RealScalar> CheckedRem for Poly<T>
Source§fn checked_rem(&self, rhs: &Self) -> Option<Self>
fn checked_rem(&self, rhs: &Self) -> Option<Self>
None
is returned. Read moreSource§impl<T: RealScalar> Div for Poly<T>
impl<T: RealScalar> Div for Poly<T>
Source§impl<'a, T: RealScalar> IntoIterator for &'a Poly<T>
impl<'a, T: RealScalar> IntoIterator for &'a Poly<T>
Source§impl<'a, T: RealScalar> IntoIterator for &'a mut Poly<T>
impl<'a, T: RealScalar> IntoIterator for &'a mut Poly<T>
Source§impl<T: RealScalar> Mul for Poly<T>
impl<T: RealScalar> Mul for Poly<T>
Source§impl<T: RealScalar> Neg for &Poly<T>
impl<T: RealScalar> Neg for &Poly<T>
Source§impl<T: RealScalar> Neg for Poly<T>
impl<T: RealScalar> Neg for Poly<T>
Source§impl<T: RealScalar> One for Poly<T>
impl<T: RealScalar> One for Poly<T>
Source§impl<T: RealScalar> Rem for Poly<T>
impl<T: RealScalar> Rem for Poly<T>
Source§impl<T: RealScalar> Sub for Poly<T>
impl<T: RealScalar> Sub for Poly<T>
Source§impl<T: RealScalar> Sum for Poly<T>
impl<T: RealScalar> Sum for Poly<T>
Source§impl<T: RealScalar> Zero for Poly<T>
impl<T: RealScalar> Zero for Poly<T>
impl<T: Eq + RealScalar> Eq for Poly<T>
impl<T: RealScalar> StructuralPartialEq for Poly<T>
Auto Trait Implementations§
impl<T> Freeze for Poly<T>
impl<T> RefUnwindSafe for Poly<T>where
T: RefUnwindSafe,
impl<T> Send for Poly<T>where
T: Send,
impl<T> Sync for Poly<T>where
T: Sync,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more