Struct polynomint::Polynomial[][src]

pub struct Polynomial { /* fields omitted */ }

A wrapper struct around a Vec<isize> which treats the entries of the Vec as the coefficients of a polynomial.

Examples

use polynomint::{Polynomial, poly};

let quadratic = poly![1, 2, 1]; // x^2 + 2x + 1
let linear = poly![-6, 1]; // x - 6
assert_eq!(&quadratic * 5, poly![5, 10, 5]);
assert_eq!(&quadratic * &linear, poly![-6, -11, -4, 1]);

let mut resultant = &quadratic * &linear;
resultant %= 5;
assert_eq!(resultant, poly![-1, -1, -4, 1]);

let resultant2 = (&quadratic * &linear).rem_euclid(5);
assert_eq!(resultant2, poly![4, 4, 1, 1]);

Implementations

impl Polynomial[src]

pub fn iter(&self) -> Iter<'_, isize>[src]

Returns an immutably referencing iterator over the underlying Vec of coefficients.

Examples

use polynomint::{Polynomial, poly};

let poly = poly![5, 3, -2, 1];
let mut iter = poly.iter();

assert_eq!(iter.next(), Some(&5));
assert_eq!(iter.next(), Some(&3));
assert_eq!(iter.next(), Some(&(-2)));
assert_eq!(iter.next(), Some(&1));
assert_eq!(iter.next(), None);

pub fn iter_mut(&mut self) -> IterMut<'_, isize>[src]

Returns a mutably referencing iterator over the underlying Vec of coefficients.

Examples

use polynomint::{Polynomial, poly};

let mut poly = poly![5, 3, -2, 1];
for coeff in poly.iter_mut() {
    *coeff *= 3;
}
assert_eq!(poly, poly![15, 9, -6, 3]);

impl Polynomial[src]

pub fn times_x(&self) -> Self[src]

Gives a new polynomial equal to the old one times x.

Examples

use polynomint::{Polynomial, poly};

let first = poly![1, 2, 3];
let second = first.times_x();

assert_eq!(second, poly![0, 1, 2, 3]);

pub fn rem_euclid(&self, n: isize) -> Self[src]

Gives a new polynomial equal to the remainder of the old one when taken modulo n.

Examples

use polynomint::{Polynomial, poly};

let poly = poly![6, -5, 3, -7, 4];
assert_eq!(poly.rem_euclid(2), poly![0, 1, 1, 1]);
assert_eq!(poly.rem_euclid(4), poly![2, 3, 3, 1]);
assert_eq!(poly.rem_euclid(5), poly![1, 0, 3, 3, 4]);

pub fn derivative(&self) -> Self[src]

Creates a new polynomial which is the derivative of the old one.

Examples

use polynomint::{Polynomial, poly};

let poly1 = poly![1, -2, 5, 4]; // 4x^3 + 5x^2 - 2x + 1
assert_eq!(poly1.derivative(), poly![-2, 10, 12]); // deriv. is 12x^2 + 10x - 2
let poly2 = poly![192, 3, -4, -9, 0, 38]; // 38x^5 - 9x^3 - 4x^2 + 3x + 192
assert_eq!(poly2.derivative(), poly![3, -8, -27, 0, 190]); // deriv. is 190x^4 - 27x^2 - 8x + 3

pub fn eval(&self, x: isize) -> isize[src]

Plugs in a specific isize value x to the polynomial.

Examples

use polynomint::{poly, Polynomial};

let poly1 = poly![5,2,1];
let poly2 = poly![-5,4,-3,-1];

assert_eq!(poly1.eval(1), 8);
assert_eq!(poly2.eval(1), -5);

assert_eq!(poly1.eval(-2), 5);
assert_eq!(poly2.eval(-2), -17);

pub fn has_root(&self, x: isize) -> bool[src]

Returns true if x is a root of the polynomial; otherwise returns false.

Examples

use polynomint::{Polynomial, poly};
let poly = poly![-2, 1] * poly![-4, 1] * poly![3, 1];

assert_eq!(poly, poly![24, -10, -3, 1]);
assert!(poly.has_root(2));
assert!(poly.has_root(4));
assert!(poly.has_root(-3));
assert!(!poly.has_root(1));

pub fn has_root_mod(&self, x: isize, div: isize) -> bool[src]

Returns true if x is a root of the polynomial taken modulo div; otherwise returns false.

Examples

use polynomint::{Polynomial, poly};

let poly = poly![-2, 1] * poly![-6, 1];

assert_eq!(poly, poly![12, -8, 1]);
assert!(poly.has_root_mod(2, 5));
assert!(poly.has_root_mod(1, 5));
assert!(poly.has_root_mod(2, 3));
assert!(poly.has_root_mod(0, 3));
assert!(!poly.has_root_mod(4, 5));

pub fn factor_root(&self, a: isize) -> Option<Self>[src]

If a is a root of self, returns Some(p) where self = p * (x - a). (That is, if a is a root of self, this returns the result of factoring x - a out of self.) Otherwise returns None.

Examples

use polynomint::{Polynomial, poly};

let poly = poly![12, -8, 1]; // x^2 - 8x + 12 = (x - 2)(x - 6)
assert_eq!(poly.factor_root(2), Some(poly![-6, 1]));
assert_eq!(poly.factor_root(6), Some(poly![-2, 1]));
assert_eq!(poly.factor_root(5), None);

pub fn factor_root_mod(&self, a: isize, p: isize) -> Option<Self>[src]

If a is a root of self and if p is a prime, this returns the result of factoring x - a out of self, if everything is considered a polynomial with coefficients modulo p. Otherwise returns None.

The API demands that p be prime because factoring gets more complicated when the modulus is composite, like the integers mod 4—the example below, x^2 - 8x + 12, just becomes x^2, but x^2 = x^2 + 4x + 4 = (x + 2)^2, and unique factorization is lost.

Examples

use polynomint::{Polynomial, poly};

let poly = poly![12, -8, 1]; // x^2 - 8x + 12 = (x - 2)(x - 6)
                             // = x^2 + x (mod 3) = x(x - 2) or x(x + 1) mod 3
                             // = x^2 + 2x + 2 = (x - 2)(x + 4) or (x - 1)(x + 3) mod 5
assert_eq!(poly.factor_root_mod(2, 3), Some(poly![0, 1]));
assert_eq!(poly.factor_root_mod(0, 3), Some(poly![1, 1]));
assert_eq!(poly.factor_root_mod(1, 3), None);
assert_eq!(poly.factor_root_mod(2, 5), Some(poly![4, 1]));
assert_eq!(poly.factor_root_mod(1, 5), Some(poly![3, 1]));
assert_eq!(poly.factor_root_mod(0, 5), None);

let poly2 = poly![1, 0, 1]; // x^2 + 1 = (x + 1)^2 mod 2

assert_eq!(poly2.factor_root_mod(1, 2), Some(poly![1, 1]));
assert_eq!(poly2.factor_root_mod(0, 2), None);

impl Polynomial[src]

pub fn new(coeffs: Vec<isize>) -> Self[src]

Creates a polynomial with the given coefficients, stored in increasing order, with any trailing (higher-degree) zeroes removed.

Examples

use polynomint::Polynomial;

let quadratic = Polynomial::new(vec![1, 2, 3]); // 3x^2 + 2x + 1
let cubic = Polynomial::new(vec![8, 12, 6, 1]); // x^3 + 6x^2 + 12x + 8

pub fn zero() -> Self[src]

Creates the zero polynomial, which is stored internally as an empty vector.

pub fn constant(i: isize) -> Self[src]

Creates a constant polynomial with coefficient equal to the argument passed; if the argument passed is zero, it is stored internally as an empty vector to match zero().

pub fn degree(&self) -> isize[src]

Gives the highest power which has a nonzero coefficient; constants are degree zero, except the constant polynomial 0, which has degree -1.

Examples

use polynomint::{Polynomial, poly};

let zero = Polynomial::zero();
let alt_zero = Polynomial::constant(0);
let three = Polynomial::constant(3);
let classic = poly![1, 2, 1, 0, 0]; // x^2 + 2x + 1

assert_eq!(zero.degree(), -1);
assert_eq!(alt_zero.degree(), -1);
assert_eq!(three.degree(), 0);
assert_eq!(classic.degree(), 2);

pub fn is_zero(&self) -> bool[src]

Checks whether a polynomial is the zero polynomial.

Examples

use polynomint::{Polynomial, poly};

let zero = Polynomial::zero();
let also_zero = Polynomial::constant(0);
let yet_again_zero = poly![0, 0, 0, 0];
let even_more_zero = poly![1, 2, 1] - poly![1, 2, 1];
let not_zero = poly![0, 1];

assert!(zero.is_zero());
assert!(also_zero.is_zero());
assert!(yet_again_zero.is_zero());
assert!(even_more_zero.is_zero());
assert!(!not_zero.is_zero());

pub fn coeffs(&self) -> &Vec<isize>[src]

Returns a reference to self’s vector of coefficients, in order of ascending degree (poly.coeffs()[n] is the x^n coefficient of poly).

pub fn coeffs_mut(&mut self) -> &mut Vec<isize>[src]

Returns a mutable reference to self’s vector of coefficients, in order of ascending degree (poly.coeffs_mut()[n] is the x^n coefficient of poly).

Trait Implementations

impl<'a> Add<&'a Polynomial> for &'a Polynomial[src]

type Output = Polynomial

The resulting type after applying the + operator.

impl Add<Polynomial> for Polynomial[src]

type Output = Self

The resulting type after applying the + operator.

impl Add<isize> for Polynomial[src]

type Output = Self

The resulting type after applying the + operator.

impl<'a> Add<isize> for &'a Polynomial[src]

type Output = Polynomial

The resulting type after applying the + operator.

impl<'a> AddAssign<&'a Polynomial> for Polynomial[src]

impl AddAssign<Polynomial> for Polynomial[src]

impl AddAssign<isize> for Polynomial[src]

impl Clone for Polynomial[src]

impl Debug for Polynomial[src]

impl Display for Polynomial[src]

impl Eq for Polynomial[src]

impl Index<usize> for Polynomial[src]

type Output = isize

The returned type after indexing.

impl IndexMut<usize> for Polynomial[src]

impl IntoIterator for Polynomial[src]

type Item = isize

The type of the elements being iterated over.

type IntoIter = IntoIter<isize>

Which kind of iterator are we turning this into?

impl<'a> Mul<&'a Polynomial> for &'a Polynomial[src]

type Output = Polynomial

The resulting type after applying the * operator.

impl Mul<Polynomial> for Polynomial[src]

type Output = Self

The resulting type after applying the * operator.

impl Mul<isize> for Polynomial[src]

type Output = Self

The resulting type after applying the * operator.

impl<'a> Mul<isize> for &'a Polynomial[src]

type Output = Polynomial

The resulting type after applying the * operator.

impl<'a> MulAssign<&'a Polynomial> for Polynomial[src]

impl MulAssign<Polynomial> for Polynomial[src]

impl MulAssign<isize> for Polynomial[src]

impl Neg for Polynomial[src]

type Output = Self

The resulting type after applying the - operator.

impl Neg for &Polynomial[src]

type Output = Polynomial

The resulting type after applying the - operator.

impl PartialEq<Polynomial> for Polynomial[src]

impl Rem<isize> for Polynomial[src]

type Output = Self

The resulting type after applying the % operator.

impl<'a> Rem<isize> for &'a Polynomial[src]

type Output = Polynomial

The resulting type after applying the % operator.

impl RemAssign<isize> for Polynomial[src]

impl StructuralEq for Polynomial[src]

impl StructuralPartialEq for Polynomial[src]

impl<'a> Sub<&'a Polynomial> for &'a Polynomial[src]

type Output = Polynomial

The resulting type after applying the - operator.

impl Sub<Polynomial> for Polynomial[src]

type Output = Self

The resulting type after applying the - operator.

impl Sub<isize> for Polynomial[src]

type Output = Self

The resulting type after applying the - operator.

impl<'a> Sub<isize> for &'a Polynomial[src]

type Output = Polynomial

The resulting type after applying the - operator.

impl<'a> SubAssign<&'a Polynomial> for Polynomial[src]

impl SubAssign<Polynomial> for Polynomial[src]

impl SubAssign<isize> for Polynomial[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.