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

Returns a Polynomial with the corresponding terms, in order of ax^n + bx^(n-1) + … + cx + d

Arguments

  • terms - 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::new(vec![1.0, 4.0, 4.0]);

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

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 Polynomial.

Example

use rustnomial::{Polynomial, Derivable};
let polynomial = Polynomial::new(vec![4, 1, 5]);
assert_eq!(Polynomial::new(vec![8, 1]), polynomial.derivative());

Errors

Will panic if N can not losslessly encode the numbers from 0 to the degree of self.

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

Returns the value of the Polynomial at the given point.

Example

use rustnomial::{Polynomial, Evaluable};
let a = Polynomial::new(vec![1, 2, 3, 4]);
assert_eq!(10, a.eval(1));
assert_eq!(1234, a.eval(10));

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

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::{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();

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

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.

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.

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

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

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

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

The resulting type after applying the - operator.

Performs the - operation. Read more

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.