Struct rustnomial::SparsePolynomial[][src]

pub struct SparsePolynomial<N> {
    pub terms: BTreeMap<usize, N>,
}
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);

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

The resulting type after applying the + operator.

Performs the + operation. Read more

Performs the += operation. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. 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.

Formats the value using the given formatter. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

Performs the /= operation. Read more

Evaluates self at point, and returns the result.

Returns a SparsePolynomial with the corresponding terms.

Arguments

  • terms - A slice of (coefficient, degree) pairs.

Example

use rustnomial::{SparsePolynomial, FreeSizePolynomial};
// Corresponds to 1.0x^2 + 4.0x + 4.0
let polynomial = SparsePolynomial::from_terms(&[(1.0, 2), (4.0, 1), (4.0, 1)]);

Adds the term with given coefficient coeff and degree degree to self.

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

The associated error which can be returned from parsing.

Parses a string s to return a value of this type. Read more

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.));

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.

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

Performs the *= operation. Read more

Performs the *= operation. Read more

Performs the *= operation. Read more

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

The resulting type after applying the - operator.

Performs the unary - operation. Read more

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

This method tests for !=.

Returns the remainder of dividing self by rhs.

The resulting type after applying the % operator.

Assign the remainder of dividing self by rhs to self.

The resulting type after applying the << operator.

Performs the << operation. Read more

Performs the <<= operation. Read more

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

Returns true if all terms of self are zero, and false if a non-zero term exists. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

Performs the -= operation. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.