Struct rust_poly::Poly

source ·
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>

source

pub fn new(coeffs: Array1<T>) -> Self

Create a new polynomial from a 1D array of coefficients

source

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

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.

source

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

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>

source§

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

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<Poly<T>> for Poly<T>

source§

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

Computes the quotient of two polynomials, truncating the remainder.

See also Poly::div_rem().

§

type Output = Poly<T>

The resulting type after applying the / operator.
source§

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

source§

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

type Output = Poly<T>

The resulting type after applying the * operator.
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>

source§

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

Computes the remainder of the division of two polynomials.

See also Poly::div_rem().

§

type Output = Poly<T>

The resulting type after applying the % operator.
source§

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

source§

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

type Output = Poly<T>

The resulting type after applying the - operator.
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>where T: Send,

§

impl<T> Sync for Poly<T>where T: Sync,

§

impl<T> Unpin for Poly<T>

§

impl<T> UnwindSafe for Poly<T>where T: RefUnwindSafe,

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> 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.
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>,