Struct rustnomial::Polynomial [−][src]
pub struct Polynomial<N> { pub terms: Vec<N>, }
Expand description
A type that stores terms of a polynomial in a Vec.
Fields
terms: Vec<N>
Implementations
Reduces the size of the Polynomial
in memory if the leading terms are zero.
Example
use rustnomial::Polynomial; let mut polynomial = Polynomial::new(vec![1.0, 4.0, 4.0]); polynomial.terms = vec![0.0, 0.0, 0.0, 0.0, 1.0, 4.0, 4.0]; polynomial.trim(); assert_eq!(vec![1.0, 4.0, 4.0], polynomial.terms);
Return the roots of the Polynomial
.
Example
use rustnomial::{Polynomial, Roots, SizedPolynomial}; let zero = Polynomial::<f64>::zero(); assert_eq!(Roots::InfiniteRoots, zero.roots()); let constant = Polynomial::new(vec![1.]); assert_eq!(Roots::NoRoots, constant.roots()); let monomial = Polynomial::new(vec![1.0, 0.,]); assert_eq!(Roots::ManyRealRoots(vec![0.]), monomial.roots()); let binomial = Polynomial::new(vec![1.0, 2.0]); assert_eq!(Roots::ManyRealRoots(vec![-2.0]), binomial.roots()); let trinomial = Polynomial::new(vec![1.0, 4.0, 4.0]); assert_eq!(Roots::ManyRealRoots(vec![-2.0, -2.0]), trinomial.roots()); let quadnomial = Polynomial::new(vec![1.0, 6.0, 12.0, 8.0]); assert_eq!(Roots::ManyRealRoots(vec![-2.0, -2.0, -2.0]), quadnomial.roots());
Raises the Polynomial
to the power of exp, using exponentiation by squaring.
Example
use rustnomial::Polynomial; let polynomial = Polynomial::new(vec![1.0, 2.0]); let polynomial_sqr = polynomial.pow(2); let polynomial_cub = polynomial.pow(3); assert_eq!(polynomial.clone() * polynomial.clone(), polynomial_sqr); assert_eq!(polynomial_sqr.clone() * polynomial.clone(), polynomial_cub);
Divides self by the given Polynomial
, and returns the quotient and remainder.
Divides self by the given Polynomial
, and returns the quotient.
Trait Implementations
type Output = Polynomial<N>
type Output = Polynomial<N>
The resulting type after applying the +
operator.
Performs the +
operation. Read more
Performs the +=
operation. Read more
type Output = Polynomial<N>
type Output = Polynomial<N>
The resulting type after applying the /
operator.
Performs the /
operation. Read more
Performs the /=
operation. Read more
Returns a Polynomial
with the corresponding terms,
in order of ax^n + bx^(n-1) + … + cx + d
Arguments
terms
- A slice of (coefficient, degree) pairs.
Example
use rustnomial::{FreeSizePolynomial, Polynomial}; // Corresponds to 1.0x^2 + 4.0x + 4.0 let polynomial = Polynomial::from_terms(&[(1.0, 2), (4.0, 1), (4.0, 0)]); assert_eq!(Polynomial::new(vec![1., 4., 4.]), polynomial);
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Returns a SparsePolynomial
with the corresponding terms,
in order of ax^n + bx^(n-1) + … + cx + d
Arguments
term_vec
- A vector of constants, in decreasing order of degree.
Example
use rustnomial::{Polynomial}; // Corresponds to 1.0x^2 + 4.0x + 4.0 let polynomial = Polynomial::from(vec![1.0, 4.0, 4.0]); let polynomial: Polynomial<f64> = vec![1.0, 4.0, 4.0].into();
impl<N> Integrable<N, Polynomial<N>> for LinearBinomial<N> where
N: Zero + Copy + DivAssign + Mul<Output = N> + MulAssign + AddAssign + Div<Output = N> + TryFromUsizeContinuous,
impl<N> Integrable<N, Polynomial<N>> for LinearBinomial<N> where
N: Zero + Copy + DivAssign + Mul<Output = N> + MulAssign + AddAssign + Div<Output = N> + TryFromUsizeContinuous,
Returns the integral of the LinearBinomial
.
Example
use rustnomial::{LinearBinomial, Integrable, Polynomial}; let binomial = LinearBinomial::new([2.0, 0.]); let integral = binomial.integral(); assert_eq!(&Polynomial::new(vec![1.0, 0.0, 0.0]), integral.inner());
Will panic if N
can not losslessly represent 2usize
.
impl<N> Integrable<N, Polynomial<N>> for Polynomial<N> where
N: Zero + One + Copy + DivAssign + Mul<Output = N> + MulAssign + AddAssign + TryFromUsizeContinuous + SubAssign,
impl<N> Integrable<N, Polynomial<N>> for Polynomial<N> where
N: Zero + One + Copy + DivAssign + Mul<Output = N> + MulAssign + AddAssign + TryFromUsizeContinuous + SubAssign,
Returns the integral of the Polynomial
.
Example
use rustnomial::{Polynomial, Integrable}; let polynomial = Polynomial::new(vec![1.0, 2.0, 5.0]); let integral = polynomial.integral(); assert_eq!(&Polynomial::new(vec![1.0/3.0, 1.0, 5.0, 0.0]), integral.inner());
Errors
Will panic if N
can not losslessly encode the numbers from 0 to the degree of self self
.
impl<N> Integrable<N, Polynomial<N>> for QuadraticTrinomial<N> where
N: Zero + TryFromUsizeExact + Copy + DivAssign + Mul<Output = N> + MulAssign + AddAssign + Div<Output = N>,
impl<N> Integrable<N, Polynomial<N>> for QuadraticTrinomial<N> where
N: Zero + TryFromUsizeExact + Copy + DivAssign + Mul<Output = N> + MulAssign + AddAssign + Div<Output = N>,
Returns the integral of the Monomial
.
Example
use rustnomial::{QuadraticTrinomial, Integrable, Polynomial}; let trinomial = QuadraticTrinomial::new([3.0, 0., 0.]); let integral = trinomial.integral(); assert_eq!(&Polynomial::new(vec![1.0, 0.0, 0.0, 0.0]), integral.inner());
Errors
Will panic if N
can not losslessly represent 2usize
or 3usize
.
impl<N> Mul<&'_ Polynomial<N>> for Polynomial<N> where
N: Mul<Output = N> + AddAssign + Copy + Zero,
impl<N> Mul<&'_ Polynomial<N>> for Polynomial<N> where
N: Mul<Output = N> + AddAssign + Copy + Zero,
type Output = Polynomial<N>
type Output = Polynomial<N>
The resulting type after applying the *
operator.
Performs the *
operation. Read more
impl<N> Mul<&'_ Polynomial<N>> for &Polynomial<N> where
N: Mul<Output = N> + AddAssign + Copy + Zero,
impl<N> Mul<&'_ Polynomial<N>> for &Polynomial<N> where
N: Mul<Output = N> + AddAssign + Copy + Zero,
type Output = Polynomial<N>
type Output = Polynomial<N>
The resulting type after applying the *
operator.
Performs the *
operation. Read more
type Output = Polynomial<N>
type Output = Polynomial<N>
The resulting type after applying the *
operator.
Performs the *
operation. Read more
impl<N> Mul<Polynomial<N>> for Polynomial<N> where
N: Mul<Output = N> + AddAssign + Copy + Zero,
impl<N> Mul<Polynomial<N>> for Polynomial<N> where
N: Mul<Output = N> + AddAssign + Copy + Zero,
type Output = Polynomial<N>
type Output = Polynomial<N>
The resulting type after applying the *
operator.
Performs the *
operation. Read more
impl<N> Mul<Polynomial<N>> for &Polynomial<N> where
N: Mul<Output = N> + AddAssign + Copy + Zero,
impl<N> Mul<Polynomial<N>> for &Polynomial<N> where
N: Mul<Output = N> + AddAssign + Copy + Zero,
type Output = Polynomial<N>
type Output = Polynomial<N>
The resulting type after applying the *
operator.
Performs the *
operation. Read more
impl<N> MulAssign<&'_ Polynomial<N>> for Polynomial<N> where
N: Mul<Output = N> + AddAssign + Copy + Zero,
impl<N> MulAssign<&'_ Polynomial<N>> for Polynomial<N> where
N: Mul<Output = N> + AddAssign + Copy + Zero,
Performs the *=
operation. Read more
Performs the *=
operation. Read more
impl<N> MulAssign<Polynomial<N>> for Polynomial<N> where
N: Mul<Output = N> + AddAssign + Copy + Zero,
impl<N> MulAssign<Polynomial<N>> for Polynomial<N> where
N: Mul<Output = N> + AddAssign + Copy + Zero,
Performs the *=
operation. Read more
impl<N> MutablePolynomial<N> for Polynomial<N> where
N: Zero + Copy + AddAssign + SubAssign + CanNegate,
impl<N> MutablePolynomial<N> for Polynomial<N> where
N: Zero + Copy + AddAssign + SubAssign + CanNegate,
Tries to add the term with given coefficient and degree
to self
, returning an error
if the particular term can not be added to self without violating constraints. Read more
Tries to subtract the term with given coefficient and degree
from self
, returning
an error if the particular term can not be subtracted from self without violating
constraints. Read more
type Output = Polynomial<N>
type Output = Polynomial<N>
The resulting type after applying the -
operator.
Performs the unary -
operation. Read more
Returns true if self and other have the same terms.
Example
use rustnomial::Polynomial; let a = Polynomial::new(vec![1.0, 2.0]); let b = Polynomial::new(vec![2.0, 2.0]); let c = Polynomial::new(vec![1.0, 0.0]); assert_ne!(a, b); assert_ne!(a, c); assert_eq!(a, b - c);
impl<N> Rem<Polynomial<N>> for Polynomial<N> where
N: Copy + Zero + SubAssign + Mul<Output = N> + Div<Output = N>,
impl<N> Rem<Polynomial<N>> for Polynomial<N> where
N: Copy + Zero + SubAssign + Mul<Output = N> + Div<Output = N>,
Returns the remainder of dividing self
by rhs
.
type Output = Polynomial<N>
type Output = Polynomial<N>
The resulting type after applying the %
operator.
impl<N> RemAssign<Polynomial<N>> for Polynomial<N> where
N: Copy + Zero + SubAssign + Mul<Output = N> + Div<Output = N>,
impl<N> RemAssign<Polynomial<N>> for Polynomial<N> where
N: Copy + Zero + SubAssign + Mul<Output = N> + Div<Output = N>,
Assign the remainder of dividing self
by rhs
to self
.
type Output = Polynomial<N>
type Output = Polynomial<N>
The resulting type after applying the <<
operator.
Performs the <<
operation. Read more
Performs the <<=
operation. Read more
type Output = Polynomial<N>
type Output = Polynomial<N>
The resulting type after applying the >>
operator.
Performs the >>
operation. Read more
Performs the >>=
operation. Read more
Returns the degree of the Polynomial
it is called on, corresponding to the
largest non-zero term.
Example
use rustnomial::{SizedPolynomial, Polynomial, Degree}; let polynomial = Polynomial::new(vec![1.0, 4.0, 4.0]); assert_eq!(Degree::Num(2), polynomial.degree());
Returns a Polynomial
with no terms.
Example
use rustnomial::{SizedPolynomial, Polynomial}; let zero = Polynomial::<i32>::zero(); assert!(zero.is_zero()); assert!(zero.ordered_term_iter().next().is_none()); assert!(zero.terms.is_empty());
Sets self to zero.
Example
use rustnomial::{Polynomial, SizedPolynomial}; let mut non_zero = Polynomial::from(vec![0, 1]); assert!(!non_zero.is_zero()); non_zero.set_to_zero(); assert!(non_zero.is_zero());
Returns the term with the given degree from self
.
If the term degree is larger than the actual degree, ZeroTerm
will be returned.
However, terms which are zero will also be returned as ZeroTerm
, so this does
not indicate that the final term has been reached. Read more
Returns a Vec containing all of the terms of self
, where each item is
the coefficient and degree of each non-zero term, in order of descending degree. Read more
impl<N> Sub<&'_ Polynomial<N>> for Polynomial<N> where
N: Zero + Copy + Sub<Output = N> + SubAssign + Neg<Output = N>,
impl<N> Sub<&'_ Polynomial<N>> for Polynomial<N> where
N: Zero + Copy + Sub<Output = N> + SubAssign + Neg<Output = N>,
type Output = Polynomial<N>
type Output = Polynomial<N>
The resulting type after applying the -
operator.
Performs the -
operation. Read more
impl<N> Sub<Polynomial<N>> for Polynomial<N> where
N: Zero + Copy + Sub<Output = N> + SubAssign + Neg<Output = N>,
impl<N> Sub<Polynomial<N>> for Polynomial<N> where
N: Zero + Copy + Sub<Output = N> + SubAssign + Neg<Output = N>,
type Output = Polynomial<N>
type Output = Polynomial<N>
The resulting type after applying the -
operator.
Performs the -
operation. Read more
impl<N> Sub<Polynomial<N>> for SparsePolynomial<N> where
N: Zero + Copy + Sub<Output = N> + SubAssign + Neg<Output = N>,
impl<N> Sub<Polynomial<N>> for SparsePolynomial<N> where
N: Zero + Copy + Sub<Output = N> + SubAssign + Neg<Output = N>,
type Output = SparsePolynomial<N>
type Output = SparsePolynomial<N>
The resulting type after applying the -
operator.
Performs the -
operation. Read more
impl<N> SubAssign<&'_ Polynomial<N>> for Polynomial<N> where
N: Neg<Output = N> + Sub<Output = N> + SubAssign + Copy + Zero,
impl<N> SubAssign<&'_ Polynomial<N>> for Polynomial<N> where
N: Neg<Output = N> + Sub<Output = N> + SubAssign + Copy + Zero,
Performs the -=
operation. Read more
impl<N> SubAssign<Polynomial<N>> for Polynomial<N> where
N: Neg<Output = N> + Sub<Output = N> + SubAssign + Copy + Zero,
impl<N> SubAssign<Polynomial<N>> for Polynomial<N> where
N: Neg<Output = N> + Sub<Output = N> + SubAssign + Copy + Zero,
Performs the -=
operation. Read more
Auto Trait Implementations
impl<N> RefUnwindSafe for Polynomial<N> where
N: RefUnwindSafe,
impl<N> Send for Polynomial<N> where
N: Send,
impl<N> Sync for Polynomial<N> where
N: Sync,
impl<N> Unpin for Polynomial<N> where
N: Unpin,
impl<N> UnwindSafe for Polynomial<N> where
N: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more