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

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