Struct polynomial::Polynomial
source · pub struct Polynomial<T> { /* private fields */ }
Expand description
A polynomial.
Implementations§
source§impl<T: Zero> Polynomial<T>
impl<T: Zero> Polynomial<T>
source§impl<T> Polynomial<T>where
T: One + Zero + Clone + Neg<Output = T> + Div<Output = T> + Mul<Output = T> + Sub<Output = T>,
impl<T> Polynomial<T>where T: One + Zero + Clone + Neg<Output = T> + Div<Output = T> + Mul<Output = T> + Sub<Output = T>,
sourcepub fn lagrange(xs: &[T], ys: &[T]) -> Option<Self>
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,
impl<T> Polynomial<T>where T: One + Zero + Clone + Neg<Output = T> + Div<Output = T> + Mul<Output = T> + Sub<Output = T> + FromPrimitive,
sourcepub fn chebyshev<F: Fn(T) -> T>(
f: &F,
n: usize,
xmin: f64,
xmax: f64
) -> Option<Self>
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);
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,
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>
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>
fn add(self, other: &Polynomial<Rhs>) -> Polynomial<<Lhs as Add<Rhs>>::Output>
Performs the
+
operation. Read moresource§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,
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>
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>
fn add(self, other: &Polynomial<Rhs>) -> Polynomial<<Lhs as Add<Rhs>>::Output>
Performs the
+
operation. Read moresource§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,
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>
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>
fn add(self, other: Polynomial<Rhs>) -> Polynomial<<Lhs as Add<Rhs>>::Output>
Performs the
+
operation. Read moresource§impl<Lhs, Rhs> Add<Polynomial<Rhs>> for Polynomial<Lhs>where
Lhs: Zero + Add<Rhs> + Clone,
Rhs: Zero + Clone,
<Lhs as Add<Rhs>>::Output: Zero,
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>
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>
fn add(self, other: Polynomial<Rhs>) -> Polynomial<<Lhs as Add<Rhs>>::Output>
Performs the
+
operation. Read moresource§impl<T: Clone> Clone for Polynomial<T>
impl<T: Clone> Clone for Polynomial<T>
source§fn clone(&self) -> Polynomial<T>
fn clone(&self) -> Polynomial<T>
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl<T: Debug> Debug for Polynomial<T>
impl<T: Debug> Debug for Polynomial<T>
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,
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>
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>
fn mul(self, other: &Polynomial<Rhs>) -> Polynomial<<Lhs as Mul<Rhs>>::Output>
Performs the
*
operation. Read moresource§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,
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>
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>
fn mul(self, other: &Polynomial<Rhs>) -> Polynomial<<Lhs as Mul<Rhs>>::Output>
Performs the
*
operation. Read moresource§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,
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>
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>
fn mul(self, other: Polynomial<Rhs>) -> Polynomial<<Lhs as Mul<Rhs>>::Output>
Performs the
*
operation. Read moresource§impl<Lhs, Rhs> Mul<Polynomial<Rhs>> for Polynomial<Lhs>where
Lhs: Zero + Mul<Rhs> + Clone,
Rhs: Zero + Clone,
<Lhs as Mul<Rhs>>::Output: Zero,
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>
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>
fn mul(self, other: Polynomial<Rhs>) -> Polynomial<<Lhs as Mul<Rhs>>::Output>
Performs the
*
operation. Read moresource§impl<T: PartialEq> PartialEq for Polynomial<T>
impl<T: PartialEq> PartialEq for Polynomial<T>
source§fn eq(&self, other: &Polynomial<T>) -> bool
fn eq(&self, other: &Polynomial<T>) -> bool
This method tests for
self
and other
values to be equal, and is used
by ==
.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,
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>
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>
fn sub(self, other: &Polynomial<Rhs>) -> Polynomial<<Lhs as Sub<Rhs>>::Output>
Performs the
-
operation. Read moresource§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,
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>
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>
fn sub(self, other: &Polynomial<Rhs>) -> Polynomial<<Lhs as Sub<Rhs>>::Output>
Performs the
-
operation. Read moresource§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,
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>
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>
fn sub(self, other: Polynomial<Rhs>) -> Polynomial<<Lhs as Sub<Rhs>>::Output>
Performs the
-
operation. Read moresource§impl<Lhs, Rhs> Sub<Polynomial<Rhs>> for Polynomial<Lhs>where
Lhs: Zero + Sub<Rhs> + Clone,
Rhs: Zero + Clone,
<Lhs as Sub<Rhs>>::Output: Zero,
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>
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>
fn sub(self, other: Polynomial<Rhs>) -> Polynomial<<Lhs as Sub<Rhs>>::Output>
Performs the
-
operation. Read moreimpl<T: Eq> Eq for Polynomial<T>
impl<T> StructuralEq for Polynomial<T>
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> 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
Mutably borrows from an owned value. Read more