Struct polynomial::Polynomial

source ·
pub struct Polynomial<T> { /* private fields */ }
Expand description

A polynomial.

Implementations§

source§

impl<T: Zero> Polynomial<T>

source

pub fn new(data: Vec<T>) -> Self

Creates a new Polynomial from a Vec of coefficients.

Examples
use polynomial::Polynomial;
let poly = Polynomial::new(vec![1, 2, 3]);
assert_eq!("1+2*x+3*x^2", poly.pretty("x"));
source§

impl<T> Polynomial<T>where T: One + Zero + Clone + Neg<Output = T> + Div<Output = T> + Mul<Output = T> + Sub<Output = T>,

source

pub fn lagrange(xs: &[T], ys: &[T]) -> Option<Self>

Creates the Lagrange polynomial that fits a number of points.

Returns None if any two x-coordinates are the same.

Examples
use polynomial::Polynomial;
let poly = Polynomial::lagrange(&[1, 2, 3], &[10, 40, 90]).unwrap();
println!("{}", poly.pretty("x"));
assert_eq!("10*x^2", poly.pretty("x"));
source§

impl<T> Polynomial<T>where T: One + Zero + Clone + Neg<Output = T> + Div<Output = T> + Mul<Output = T> + Sub<Output = T> + FromPrimitive,

source

pub fn chebyshev<F: Fn(T) -> T>( f: &F, n: usize, xmin: f64, xmax: f64 ) -> Option<Self>

Chebyshev approximation fits a function to a polynomial over a range of values.

This attempts to minimize the maximum error.

Retrurns None if n < 1 or xmin >= xmax.

Examples
use polynomial::Polynomial;
use std::f64::consts::PI;
let p = Polynomial::chebyshev(&f64::sin, 7, -PI/4., PI/4.).unwrap();
assert!((p.eval(0.) - (0.0_f64).sin()).abs() < 0.0001);
assert!((p.eval(0.1) - (0.1_f64).sin()).abs() < 0.0001);
assert!((p.eval(-0.1) - (-0.1_f64).sin()).abs() < 0.0001);
source§

impl<T: Zero + Mul<Output = T> + Clone> Polynomial<T>

source

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

Evaluates the polynomial at a point.

Examples
use polynomial::Polynomial;
let poly = Polynomial::new(vec![1, 2, 3]);
assert_eq!(1, poly.eval(0));
assert_eq!(6, poly.eval(1));
assert_eq!(17, poly.eval(2));
source§

impl<T> Polynomial<T>

source

pub fn data(&self) -> &[T]

Gets the slice of internal data.

source§

impl<T> Polynomial<T>where T: Zero + One + Eq + Neg<Output = T> + Ord + Display + Clone,

source

pub fn pretty(&self, x: &str) -> String

Pretty prints the polynomial.

Trait Implementations§

source§

impl<'a, 'b, Lhs, Rhs> Add<&'b Polynomial<Rhs>> for &'a Polynomial<Lhs>where Lhs: Zero + Add<Rhs> + Clone, Rhs: Zero + Clone, <Lhs as Add<Rhs>>::Output: Zero,

§

type Output = Polynomial<<Lhs as Add<Rhs>>::Output>

The resulting type after applying the + operator.
source§

fn add(self, other: &Polynomial<Rhs>) -> Polynomial<<Lhs as Add<Rhs>>::Output>

Performs the + operation. Read more
source§

impl<'a, Lhs, Rhs> Add<&'a Polynomial<Rhs>> for Polynomial<Lhs>where Lhs: Zero + Add<Rhs> + Clone, Rhs: Zero + Clone, <Lhs as Add<Rhs>>::Output: Zero,

§

type Output = Polynomial<<Lhs as Add<Rhs>>::Output>

The resulting type after applying the + operator.
source§

fn add(self, other: &Polynomial<Rhs>) -> Polynomial<<Lhs as Add<Rhs>>::Output>

Performs the + operation. Read more
source§

impl<'a, Lhs, Rhs> Add<Polynomial<Rhs>> for &'a Polynomial<Lhs>where Lhs: Zero + Add<Rhs> + Clone, Rhs: Zero + Clone, <Lhs as Add<Rhs>>::Output: Zero,

§

type Output = Polynomial<<Lhs as Add<Rhs>>::Output>

The resulting type after applying the + operator.
source§

fn add(self, other: Polynomial<Rhs>) -> Polynomial<<Lhs as Add<Rhs>>::Output>

Performs the + operation. Read more
source§

impl<Lhs, Rhs> Add<Polynomial<Rhs>> for Polynomial<Lhs>where Lhs: Zero + Add<Rhs> + Clone, Rhs: Zero + Clone, <Lhs as Add<Rhs>>::Output: Zero,

§

type Output = Polynomial<<Lhs as Add<Rhs>>::Output>

The resulting type after applying the + operator.
source§

fn add(self, other: Polynomial<Rhs>) -> Polynomial<<Lhs as Add<Rhs>>::Output>

Performs the + operation. Read more
source§

impl<T: Clone> Clone for Polynomial<T>

source§

fn clone(&self) -> Polynomial<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> Debug for Polynomial<T>

source§

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

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

impl<'a, 'b, Lhs, Rhs> Mul<&'b Polynomial<Rhs>> for &'a Polynomial<Lhs>where Lhs: Zero + Mul<Rhs> + Clone, Rhs: Zero + Clone, <Lhs as Mul<Rhs>>::Output: Zero,

§

type Output = Polynomial<<Lhs as Mul<Rhs>>::Output>

The resulting type after applying the * operator.
source§

fn mul(self, other: &Polynomial<Rhs>) -> Polynomial<<Lhs as Mul<Rhs>>::Output>

Performs the * operation. Read more
source§

impl<'a, Lhs, Rhs> Mul<&'a Polynomial<Rhs>> for Polynomial<Lhs>where Lhs: Zero + Mul<Rhs> + Clone, Rhs: Zero + Clone, <Lhs as Mul<Rhs>>::Output: Zero,

§

type Output = Polynomial<<Lhs as Mul<Rhs>>::Output>

The resulting type after applying the * operator.
source§

fn mul(self, other: &Polynomial<Rhs>) -> Polynomial<<Lhs as Mul<Rhs>>::Output>

Performs the * operation. Read more
source§

impl<'a, Lhs, Rhs> Mul<Polynomial<Rhs>> for &'a Polynomial<Lhs>where Lhs: Zero + Mul<Rhs> + Clone, Rhs: Zero + Clone, <Lhs as Mul<Rhs>>::Output: Zero,

§

type Output = Polynomial<<Lhs as Mul<Rhs>>::Output>

The resulting type after applying the * operator.
source§

fn mul(self, other: Polynomial<Rhs>) -> Polynomial<<Lhs as Mul<Rhs>>::Output>

Performs the * operation. Read more
source§

impl<Lhs, Rhs> Mul<Polynomial<Rhs>> for Polynomial<Lhs>where Lhs: Zero + Mul<Rhs> + Clone, Rhs: Zero + Clone, <Lhs as Mul<Rhs>>::Output: Zero,

§

type Output = Polynomial<<Lhs as Mul<Rhs>>::Output>

The resulting type after applying the * operator.
source§

fn mul(self, other: Polynomial<Rhs>) -> Polynomial<<Lhs as Mul<Rhs>>::Output>

Performs the * operation. Read more
source§

impl<'a, T> Neg for &'a Polynomial<T>where T: Neg + Zero + Clone, <T as Neg>::Output: Zero,

§

type Output = Polynomial<<T as Neg>::Output>

The resulting type after applying the - operator.
source§

fn neg(self) -> Polynomial<<T as Neg>::Output>

Performs the unary - operation. Read more
source§

impl<T> Neg for Polynomial<T>where T: Neg + Zero + Clone, <T as Neg>::Output: Zero,

§

type Output = Polynomial<<T as Neg>::Output>

The resulting type after applying the - operator.
source§

fn neg(self) -> Polynomial<<T as Neg>::Output>

Performs the unary - operation. Read more
source§

impl<T: Zero + One + Clone> One for Polynomial<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§

impl<T: PartialEq> PartialEq for Polynomial<T>

source§

fn eq(&self, other: &Polynomial<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<'a, 'b, Lhs, Rhs> Sub<&'b Polynomial<Rhs>> for &'a Polynomial<Lhs>where Lhs: Zero + Sub<Rhs> + Clone, Rhs: Zero + Clone, <Lhs as Sub<Rhs>>::Output: Zero,

§

type Output = Polynomial<<Lhs as Sub<Rhs>>::Output>

The resulting type after applying the - operator.
source§

fn sub(self, other: &Polynomial<Rhs>) -> Polynomial<<Lhs as Sub<Rhs>>::Output>

Performs the - operation. Read more
source§

impl<'a, Lhs, Rhs> Sub<&'a Polynomial<Rhs>> for Polynomial<Lhs>where Lhs: Zero + Sub<Rhs> + Clone, Rhs: Zero + Clone, <Lhs as Sub<Rhs>>::Output: Zero,

§

type Output = Polynomial<<Lhs as Sub<Rhs>>::Output>

The resulting type after applying the - operator.
source§

fn sub(self, other: &Polynomial<Rhs>) -> Polynomial<<Lhs as Sub<Rhs>>::Output>

Performs the - operation. Read more
source§

impl<'a, Lhs, Rhs> Sub<Polynomial<Rhs>> for &'a Polynomial<Lhs>where Lhs: Zero + Sub<Rhs> + Clone, Rhs: Zero + Clone, <Lhs as Sub<Rhs>>::Output: Zero,

§

type Output = Polynomial<<Lhs as Sub<Rhs>>::Output>

The resulting type after applying the - operator.
source§

fn sub(self, other: Polynomial<Rhs>) -> Polynomial<<Lhs as Sub<Rhs>>::Output>

Performs the - operation. Read more
source§

impl<Lhs, Rhs> Sub<Polynomial<Rhs>> for Polynomial<Lhs>where Lhs: Zero + Sub<Rhs> + Clone, Rhs: Zero + Clone, <Lhs as Sub<Rhs>>::Output: Zero,

§

type Output = Polynomial<<Lhs as Sub<Rhs>>::Output>

The resulting type after applying the - operator.
source§

fn sub(self, other: Polynomial<Rhs>) -> Polynomial<<Lhs as Sub<Rhs>>::Output>

Performs the - operation. Read more
source§

impl<T: Zero + Clone> Zero for Polynomial<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> Eq for Polynomial<T>

source§

impl<T> StructuralEq for Polynomial<T>

source§

impl<T> StructuralPartialEq for Polynomial<T>

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for Polynomial<T>where T: RefUnwindSafe,

§

impl<T> Send for Polynomial<T>where T: Send,

§

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

§

impl<T> Unpin for Polynomial<T>where T: Unpin,

§

impl<T> UnwindSafe for Polynomial<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> 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.