Struct rust_poly::Poly

source ·
pub struct Poly<T: Scalar>(_);

Implementations§

source§

impl<T: Scalar> Poly<T>

source

pub fn new(coeffs: &[Complex<T>]) -> Self

source

pub const fn from_dvector(value: DVector<Complex<T>>) -> Self

source

pub fn from_complex_slice(value: &[Complex<T>]) -> Self

The same as Poly::new()

source

pub fn from_complex_vec(value: Vec<Complex<T>>) -> Self

source

pub fn from_real_slice(value: &[T]) -> Self

source

pub fn from_real_vec(value: Vec<T>) -> Self

source

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()]))
source

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());
source

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());
source

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]);
source

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]);
source

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])
source

pub fn len(&self) -> usize

source

pub fn is_empty(&self) -> bool

source

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));
source

pub fn eval(&self, x: &DMatrix<Complex<T>>) -> DMatrix<Complex<T>>

Evaluate the polynomial for each entry of a matrix.

source

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]);
source

pub fn pow_usize(&self, pow: usize) -> Self

source

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);
source

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);
source

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);
source

pub fn as_slice(&self) -> &[Complex<T>]

source

pub fn as_mut_slice(&mut self) -> &mut [Complex<T>]

source

pub fn as_ptr(&self) -> *const Complex<T>

source

pub fn as_mut_ptr(&mut self) -> *mut Complex<T>

source

pub fn as_view(&self) -> DMatrixView<'_, Complex<T>>

source

pub fn as_view_mut(&mut self) -> DMatrixViewMut<'_, Complex<T>>

source

pub fn to_vec(&self) -> Vec<Complex<T>>

source

pub fn to_dvector(&self) -> DVector<Complex<T>>

Trait Implementations§

source§

impl<T: Scalar> Add<&Poly<T>> for &Poly<T>

§

type Output = Poly<T>

The resulting type after applying the + operator.
source§

fn add(self, rhs: &Poly<T>) -> Self::Output

Performs the + operation. Read more
source§

impl<T: Scalar> Add<&Poly<T>> for Poly<T>

§

type Output = Poly<T>

The resulting type after applying the + operator.
source§

fn add(self, rhs: &Self) -> Self::Output

Performs the + operation. Read more
source§

impl<T: Scalar> Add<Poly<T>> for &Poly<T>

§

type Output = Poly<T>

The resulting type after applying the + operator.
source§

fn add(self, rhs: Poly<T>) -> Self::Output

Performs the + operation. Read more
source§

impl<T: Scalar> Add<Poly<T>> for Poly<T>

source§

fn add(self, rhs: Self) -> Self::Output

Add two polynomials

Examples
use rust_poly::{poly, Poly};

let p1 = poly![1.0, 2.0, 3.0];
let p2 = poly![4.0, 5.0];
assert_eq!(p1 + p2, poly![5.0, 7.0, 3.0]);
§

type Output = Poly<T>

The resulting type after applying the + operator.
source§

impl<T: Clone + Scalar> Clone for Poly<T>

source§

fn clone(&self) -> Poly<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug + Scalar> Debug for Poly<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: Scalar> Div<&Complex<T>> for &Poly<T>

§

type Output = Poly<T>

The resulting type after applying the / operator.
source§

fn div(self, rhs: &Complex<T>) -> Self::Output

Performs the / operation. Read more
source§

impl<T: Scalar> Div<&Complex<T>> for Poly<T>

§

type Output = Poly<T>

The resulting type after applying the / operator.
source§

fn div(self, rhs: &Complex<T>) -> Self::Output

Performs the / operation. Read more
source§

impl<T: Scalar> Div<&Poly<T>> for &Poly<T>

§

type Output = Poly<T>

The resulting type after applying the / operator.
source§

fn div(self, rhs: &Poly<T>) -> Self::Output

Performs the / operation. Read more
source§

impl<T: Scalar> Div<&Poly<T>> for Poly<T>

§

type Output = Poly<T>

The resulting type after applying the / operator.
source§

fn div(self, rhs: &Self) -> Self::Output

Performs the / operation. Read more
source§

impl<T: Scalar> Div<Complex<T>> for &Poly<T>

§

type Output = Poly<T>

The resulting type after applying the / operator.
source§

fn div(self, rhs: Complex<T>) -> Self::Output

Performs the / operation. Read more
source§

impl<T: Scalar> Div<Complex<T>> for Poly<T>

source§

fn div(self, rhs: Complex<T>) -> Self::Output

use rust_poly::{poly, Poly};
use num_complex::Complex;

let p = poly![2.0, 4.0, 6.0];
assert_eq!(p / Complex::from(2.0), poly![1.0, 2.0, 3.0]);
§

type Output = Poly<T>

The resulting type after applying the / operator.
source§

impl<T: Scalar> Div<Poly<T>> for &Poly<T>

§

type Output = Poly<T>

The resulting type after applying the / operator.
source§

fn div(self, rhs: Poly<T>) -> Self::Output

Performs the / operation. Read more
source§

impl<T: Scalar> Div<Poly<T>> for Poly<T>

§

type Output = Poly<T>

The resulting type after applying the / operator.
source§

fn div(self, rhs: Self) -> Self::Output

Performs the / operation. Read more
source§

impl<T: Scalar> From<&[Complex<T>]> for Poly<T>

source§

fn from(value: &[Complex<T>]) -> Self

Converts to this type from the input type.
source§

impl<T: Scalar> From<&[T]> for Poly<T>

source§

fn from(value: &[T]) -> Self

Converts to this type from the input type.
source§

impl<T: Scalar> From<Matrix<Complex<T>, Dyn, Const<1>, VecStorage<Complex<T>, Dyn, Const<1>>>> for Poly<T>

source§

fn from(value: DVector<Complex<T>>) -> Self

Converts to this type from the input type.
source§

impl<T: Scalar> From<Poly<T>> for *const Complex<T>

source§

fn from(val: Poly<T>) -> Self

Converts to this type from the input type.
source§

impl<T: Scalar> From<Poly<T>> for *mut Complex<T>

source§

fn from(val: Poly<T>) -> Self

Converts to this type from the input type.
source§

impl<T: Scalar> From<Poly<T>> for DVector<Complex<T>>

source§

fn from(val: Poly<T>) -> Self

Converts to this type from the input type.
source§

impl<T: Scalar> From<Poly<T>> for Vec<Complex<T>>

source§

fn from(val: Poly<T>) -> Self

Converts to this type from the input type.
source§

impl<T: Scalar> From<Vec<Complex<T>, Global>> for Poly<T>

source§

fn from(value: Vec<Complex<T>>) -> Self

Converts to this type from the input type.
source§

impl<T: Scalar> From<Vec<T, Global>> for Poly<T>

source§

fn from(value: Vec<T>) -> Self

Converts to this type from the input type.
source§

impl<T: Scalar> Index<usize> for Poly<T>

§

type Output = Complex<T>

The returned type after indexing.
source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl<T: Scalar> Mul<&Complex<T>> for Poly<T>

§

type Output = Poly<T>

The resulting type after applying the * operator.
source§

fn mul(self, rhs: &Complex<T>) -> Self::Output

Performs the * operation. Read more
source§

impl<T: Scalar> Mul<&Poly<T>> for &Poly<T>

§

type Output = Poly<T>

The resulting type after applying the * operator.
source§

fn mul(self, rhs: &Poly<T>) -> Self::Output

Performs the * operation. Read more
source§

impl<T: Scalar> Mul<&Poly<T>> for Poly<T>

§

type Output = Poly<T>

The resulting type after applying the * operator.
source§

fn mul(self, rhs: &Self) -> Self::Output

Performs the * operation. Read more
source§

impl<T: Scalar> Mul<Complex<T>> for Poly<T>

source§

fn mul(self, rhs: Complex<T>) -> Self::Output

use rust_poly::{poly, Poly};
use num_complex::Complex;

let p = poly![1.0, 2.0, 3.0];
assert_eq!(p * Complex::from(2.0), poly![2.0, 4.0, 6.0]);
§

type Output = Poly<T>

The resulting type after applying the * operator.
source§

impl<T: Scalar> Mul<Poly<T>> for &Poly<T>

§

type Output = Poly<T>

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Poly<T>) -> Self::Output

Performs the * operation. Read more
source§

impl<T: Scalar> Mul<Poly<T>> for Poly<T>

source§

fn mul(self, rhs: Self) -> Self::Output

Multiply two polynomials.

Examples
use rust_poly::{poly, Poly};
use num_complex::Complex;

let p1 = poly![1.0, 2.0, 3.0];
let p2 = poly![3.0, 2.0, 1.0];
assert_eq!(p1 * p2, poly![3.0, 8.0, 14.0, 8.0, 3.0]);
§

type Output = Poly<T>

The resulting type after applying the * operator.
source§

impl<T: Scalar> Neg for &Poly<T>

§

type Output = Poly<T>

The resulting type after applying the - operator.
source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
source§

impl<T: Scalar> Neg for Poly<T>

§

type Output = Poly<T>

The resulting type after applying the - operator.
source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
source§

impl<T: Scalar> One for Poly<T>

source§

fn one() -> Self

Returns the multiplicative identity element of Self, 1. Read more
source§

fn set_one(&mut self)

Sets self to the multiplicative identity element of Self, 1.
source§

fn is_one(&self) -> boolwhere Self: PartialEq<Self>,

Returns true if self is equal to the multiplicative identity. Read more
source§

impl<T: PartialEq + Scalar> PartialEq<Poly<T>> for Poly<T>

source§

fn eq(&self, other: &Poly<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: Scalar> Rem<&Poly<T>> for &Poly<T>

§

type Output = Poly<T>

The resulting type after applying the % operator.
source§

fn rem(self, rhs: &Poly<T>) -> Self::Output

Performs the % operation. Read more
source§

impl<T: Scalar> Rem<&Poly<T>> for Poly<T>

§

type Output = Poly<T>

The resulting type after applying the % operator.
source§

fn rem(self, rhs: &Self) -> Self::Output

Performs the % operation. Read more
source§

impl<T: Scalar> Rem<Poly<T>> for &Poly<T>

§

type Output = Poly<T>

The resulting type after applying the % operator.
source§

fn rem(self, rhs: Poly<T>) -> Self::Output

Performs the % operation. Read more
source§

impl<T: Scalar> Rem<Poly<T>> for Poly<T>

§

type Output = Poly<T>

The resulting type after applying the % operator.
source§

fn rem(self, rhs: Self) -> Self::Output

Performs the % operation. Read more
source§

impl<T: Scalar> Sub<&Poly<T>> for &Poly<T>

§

type Output = Poly<T>

The resulting type after applying the - operator.
source§

fn sub(self, rhs: &Poly<T>) -> Self::Output

Performs the - operation. Read more
source§

impl<T: Scalar> Sub<&Poly<T>> for Poly<T>

§

type Output = Poly<T>

The resulting type after applying the - operator.
source§

fn sub(self, rhs: &Self) -> Self::Output

Performs the - operation. Read more
source§

impl<T: Scalar> Sub<Poly<T>> for &Poly<T>

§

type Output = Poly<T>

The resulting type after applying the - operator.
source§

fn sub(self, rhs: Poly<T>) -> Self::Output

Performs the - operation. Read more
source§

impl<T: Scalar> Sub<Poly<T>> for Poly<T>

source§

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]);
§

type Output = Poly<T>

The resulting type after applying the - operator.
source§

impl<T: Scalar> Sum<Poly<T>> for Poly<T>

source§

fn sum<I: Iterator<Item = Self>>(iter: I) -> Self

Method which takes an iterator and generates Self from the elements by “summing up” the items.
source§

impl<T: Scalar> Zero for Poly<T>

source§

fn zero() -> Self

Returns the additive identity element of Self, 0. Read more
source§

fn is_zero(&self) -> bool

Returns true if self is equal to the additive identity.
source§

fn set_zero(&mut self)

Sets self to the additive identity element of Self, 0.
source§

impl<T: Eq + Scalar> Eq for Poly<T>

source§

impl<T: Scalar> StructuralEq for Poly<T>

source§

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> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
§

impl<SS, SP> SupersetOf<SS> for SPwhere SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> ClosedNeg for Twhere T: Neg<Output = T>,

source§

impl<T, Rhs, Output> NumOps<Rhs, Output> for Twhere T: Sub<Rhs, Output = Output> + Mul<Rhs, Output = Output> + Div<Rhs, Output = Output> + Add<Rhs, Output = Output> + Rem<Rhs, Output = Output>,

source§

impl<T, Base> RefNum<Base> for Twhere T: NumOps<Base, Base> + for<'r> NumOps<&'r Base, Base>,

source§

impl<T> Scalar for Twhere T: 'static + Clone + PartialEq<T> + Debug,