Struct rustnomial::SparsePolynomial [−][src]
Expand description
A type which stores the terms of a polynomial in a map. It is intended to store the terms
of polynomials where the degree is significantly larger than the number of non-zero terms.
Operations are significantly slower than with Polynomial
if the number of non-zero terms
is very close to the degree.
Fields
terms: BTreeMap<usize, N>
Implementations
Return the roots of the SparsePolynomial
.
Example
use rustnomial::{SparsePolynomial, Roots, SizedPolynomial}; let zero = SparsePolynomial::<f64>::zero(); assert_eq!(Roots::InfiniteRoots, zero.roots()); let constant = SparsePolynomial::from(vec![1.]); assert_eq!(Roots::NoRoots, constant.roots()); let monomial = SparsePolynomial::from(vec![1.0, 0.,]); assert_eq!(Roots::ManyRealRoots(vec![0.]), monomial.roots()); let binomial = SparsePolynomial::from(vec![1.0, 2.0]); assert_eq!(Roots::ManyRealRoots(vec![-2.0]), binomial.roots()); let trinomial = SparsePolynomial::from(vec![1.0, 4.0, 4.0]); assert_eq!(Roots::ManyRealRoots(vec![-2.0, -2.0]), trinomial.roots()); let quadnomial = SparsePolynomial::from(vec![1.0, 6.0, 12.0, 8.0]); assert_eq!(Roots::ManyRealRoots(vec![-2.0, -2.0, -2.0]), quadnomial.roots());
Raises the SparsePolynomial
to the power of exp, using exponentiation by squaring.
Example
use rustnomial::SparsePolynomial; let polynomial = SparsePolynomial::from(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);
pub fn div_mod(
&self,
rhs: &SparsePolynomial<N>
) -> (SparsePolynomial<N>, SparsePolynomial<N>)
pub fn div_mod(
&self,
rhs: &SparsePolynomial<N>
) -> (SparsePolynomial<N>, SparsePolynomial<N>)
Divides self by the given SparsePolynomial
, and returns the quotient and remainder.
Example
use rustnomial::SparsePolynomial; let polynomial = SparsePolynomial::from(vec![1.0, 2.0]);
Trait Implementations
type Output = SparsePolynomial<N>
type Output = SparsePolynomial<N>
The resulting type after applying the +
operator.
Performs the +
operation. Read more
Performs the +=
operation. Read more
Returns the derivative of the SparsePolynomial
.
Example
use rustnomial::{SparsePolynomial, Derivable}; let polynomial = SparsePolynomial::from(vec![4, 1, 5]); assert_eq!(SparsePolynomial::from(vec![8, 1]), polynomial.derivative());
Errors
Will panic if a term has a degree which does not have a lossless
representation in N
.
type Output = SparsePolynomial<N>
type Output = SparsePolynomial<N>
The resulting type after applying the /
operator.
Performs the /
operation. Read more
Performs the /=
operation. Read more
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::SparsePolynomial; // Corresponds to 1.0x^2 + 4.0x + 4.0 let polynomial = SparsePolynomial::from(vec![1.0, 4.0, 4.0]);
impl<N> Integrable<N, SparsePolynomial<N>> for Monomial<N> where
N: Zero + Copy + Mul<Output = N> + AddAssign + PowUsize + Div<Output = N> + TryFromUsizeExact,
impl<N> Integrable<N, SparsePolynomial<N>> for Monomial<N> where
N: Zero + Copy + Mul<Output = N> + AddAssign + PowUsize + Div<Output = N> + TryFromUsizeExact,
Returns the integral of the Monomial
.
Example
use rustnomial::{Monomial, SparsePolynomial, Integrable, FreeSizePolynomial}; let monomial = Monomial::new(3.0, 2); let integral = monomial.integral(); assert_eq!(&SparsePolynomial::from_terms(&[(1.0, 3)]), integral.inner()); assert_eq!(1., integral.eval(0., 1.));
impl<N> Integrable<N, SparsePolynomial<N>> for SparsePolynomial<N> where
N: PartialEq + Zero + Copy + Div<Output = N> + Mul<Output = N> + PowUsize + AddAssign + TryFromUsizeExact,
impl<N> Integrable<N, SparsePolynomial<N>> for SparsePolynomial<N> where
N: PartialEq + Zero + Copy + Div<Output = N> + Mul<Output = N> + PowUsize + AddAssign + TryFromUsizeExact,
Returns the integral of the Polynomial
.
Example
use rustnomial::{SparsePolynomial, Integrable}; let polynomial = SparsePolynomial::from(vec![1.0, 2.0, 5.0]); let integral = polynomial.integral(); assert_eq!(&SparsePolynomial::from(vec![1.0/3.0, 1.0, 5.0, 0.0]), integral.inner());
Errors
Will panic if a term has a degree, which when incremented by one, does not
have a lossless representation in N
.
impl<N> Mul<&'_ SparsePolynomial<N>> for SparsePolynomial<N> where
N: Mul<Output = N> + AddAssign + Copy + Zero,
impl<N> Mul<&'_ SparsePolynomial<N>> for SparsePolynomial<N> where
N: Mul<Output = N> + AddAssign + Copy + Zero,
type Output = SparsePolynomial<N>
type Output = SparsePolynomial<N>
The resulting type after applying the *
operator.
Performs the *
operation. Read more
type Output = SparsePolynomial<N>
type Output = SparsePolynomial<N>
The resulting type after applying the *
operator.
Performs the *
operation. Read more
impl<N> Mul<SparsePolynomial<N>> for SparsePolynomial<N> where
N: Mul<Output = N> + AddAssign + Copy + Zero,
impl<N> Mul<SparsePolynomial<N>> for SparsePolynomial<N> where
N: Mul<Output = N> + AddAssign + Copy + Zero,
type Output = SparsePolynomial<N>
type Output = SparsePolynomial<N>
The resulting type after applying the *
operator.
Performs the *
operation. Read more
impl<N> MulAssign<&'_ SparsePolynomial<N>> for SparsePolynomial<N> where
N: Mul<Output = N> + AddAssign + Copy + Zero,
impl<N> MulAssign<&'_ SparsePolynomial<N>> for SparsePolynomial<N> where
N: Mul<Output = N> + AddAssign + Copy + Zero,
Performs the *=
operation. Read more
Performs the *=
operation. Read more
impl<N> MulAssign<SparsePolynomial<N>> for SparsePolynomial<N> where
N: Mul<Output = N> + AddAssign + Copy + Zero,
impl<N> MulAssign<SparsePolynomial<N>> for SparsePolynomial<N> where
N: Mul<Output = N> + AddAssign + Copy + Zero,
Performs the *=
operation. Read more
impl<N> MutablePolynomial<N> for SparsePolynomial<N> where
N: Zero + Copy + AddAssign + SubAssign + CanNegate,
impl<N> MutablePolynomial<N> for SparsePolynomial<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 = SparsePolynomial<N>
type Output = SparsePolynomial<N>
The resulting type after applying the -
operator.
Performs the unary -
operation. Read more
impl<N> PartialEq<SparsePolynomial<N>> for SparsePolynomial<N> where
N: Zero + PartialEq + Copy,
impl<N> PartialEq<SparsePolynomial<N>> for SparsePolynomial<N> where
N: Zero + PartialEq + Copy,
Returns true if self has the same terms as other.
Example
use rustnomial::SparsePolynomial; let a = SparsePolynomial::from(vec![1.0, 2.0]); let b = SparsePolynomial::from(vec![2.0, 2.0]); let c = SparsePolynomial::from(vec![1.0, 0.0]); assert_ne!(a, b); assert_ne!(a, c); assert_eq!(a, b - c);
Returns the remainder of dividing self
by rhs
.
type Output = SparsePolynomial<N>
type Output = SparsePolynomial<N>
The resulting type after applying the %
operator.
Assign the remainder of dividing self
by rhs
to self
.
type Output = SparsePolynomial<N>
type Output = SparsePolynomial<N>
The resulting type after applying the <<
operator.
Performs the <<
operation. Read more
Performs the <<=
operation. Read more
type Output = SparsePolynomial<N>
type Output = SparsePolynomial<N>
The resulting type after applying the >>
operator.
Performs the >>
operation. Read more
Performs the >>=
operation. Read more
Returns the degree of the SparsePolynomial
it is called on, corresponding to the
largest non-zero term.
Example
use rustnomial::{SizedPolynomial, SparsePolynomial, Degree}; let polynomial = SparsePolynomial::from(vec![1.0, 4.0, 4.0]); assert_eq!(Degree::Num(2), polynomial.degree());
Returns a SparsePolynomial
with no terms.
Example
use rustnomial::{SizedPolynomial, SparsePolynomial}; let zero = SparsePolynomial::<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::{SparsePolynomial, SizedPolynomial}; let mut non_zero = SparsePolynomial::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 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> Sub<SparsePolynomial<N>> for SparsePolynomial<N> where
N: Zero + Copy + Sub<Output = N> + SubAssign + Neg<Output = N>,
impl<N> Sub<SparsePolynomial<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<SparsePolynomial<N>> for SparsePolynomial<N> where
N: Neg<Output = N> + Sub<Output = N> + SubAssign + Copy,
impl<N> SubAssign<SparsePolynomial<N>> for SparsePolynomial<N> where
N: Neg<Output = N> + Sub<Output = N> + SubAssign + Copy,
Performs the -=
operation. Read more
Auto Trait Implementations
impl<N> RefUnwindSafe for SparsePolynomial<N> where
N: RefUnwindSafe,
impl<N> Send for SparsePolynomial<N> where
N: Send,
impl<N> Sync for SparsePolynomial<N> where
N: Sync,
impl<N> Unpin for SparsePolynomial<N>
impl<N> UnwindSafe for SparsePolynomial<N> where
N: RefUnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more