Struct polynomial_ring::Polynomial [−][src]
pub struct Polynomial<T> { /* fields omitted */ }
Expand description
Polynomial ring $R[x]
$
use num::Rational64;
use polynomial_ring::Polynomial;
let f = Polynomial::new(vec![3, 1, 4, 1, 5].into_iter().map(|x| Rational64::from_integer(x)).collect());
let g = Polynomial::new(vec![2, 7, 1].into_iter().map(|x| Rational64::from_integer(x)).collect());
let mut r = f.clone();
let q = r.division(&g);
assert_eq!(f, q * g + r);
Implementations
degree of polynomial
use polynomial_ring::Polynomial;
let p = Polynomial::new(vec![3, 2, 1]); // 3+2x+x^2
assert_eq!(p.deg(), Some(2));
let q = Polynomial::new(vec![0]); // 0
assert_eq!(q.deg(), None);
leading coefficent
use polynomial_ring::Polynomial;
let p = Polynomial::new(vec![3, 2, 1]); // 3+2x+x^2
assert_eq!(p.lc(), Some(&1));
let q = Polynomial::new(vec![0]); // 0
assert_eq!(q.lc(), None);
construct polynomial
use polynomial_ring::Polynomial;
let p = Polynomial::new(vec![3, 2, 1]);
assert_eq!(p.to_string(), "x^2+2*x+3");
construct polynomial from monomial $cx^d
$ ($c
$=coefficent, $d
$=degree)
use polynomial_ring::Polynomial;
let p = Polynomial::from_monomial(3, 2);
let q = Polynomial::new(vec![0, 0, 3]);
assert_eq!(p, q);
evaluate polynomial by Horner’s method
use polynomial_ring::Polynomial;
let p = Polynomial::new(vec![3, 2, 1]); // 3+2x+x^2
assert_eq!(p.eval(&1), 6);
assert_eq!(p.eval(&2), 11);
derivative
use polynomial_ring::{Polynomial, polynomial};
let p = polynomial![1, 2, 3, 2, 1]; // 1+2x+3x^2+2x^3+x^4
assert_eq!(p.derivative(), polynomial![2, 6, 6, 4]);
pseudo division
Let $R
$ be an integral domain.
Let $f, g \in R[x]
$, where $g \neq 0
$.
This function calculate $s \in R
$, $q, r \in R[x]
$ s.t. $sf=qg+r
$,
where $r=0
$ or $\deg(r)<\deg(g)
$.
use polynomial_ring::{polynomial, Polynomial};
let f = polynomial![1, 3, 1]; // 1+3x+x^2 ∈ Z[x]
let g = polynomial![5, 2]; // 5+2x ∈ Z[x]
let mut r = f.clone();
let (s, q) = r.pseudo_division(&g);
assert_eq!(f * s, q * g + r);
let f = polynomial![1, -1, -1, 1]; // 1-x-x^2+x^3 ∈ Z[x]
let g = polynomial![1, 2]; // 1+2x ∈ Z[x]
let mut r = f.clone();
let (s, q) = r.pseudo_division(&g);
assert_eq!(polynomial![s] * f, q * g + r);
// 1-yx-x^2+yx^3 ∈ Z[y][x]
let f = polynomial![polynomial![1], polynomial![0, -1], polynomial![-1], polynomial![0, 1]];
// -1+y^2x ∈ Z[y][x]
let g = polynomial![polynomial![-1], polynomial![0, 0, 1]];
let mut r = f.clone();
let (s, q) = r.pseudo_division(&g);
assert_eq!(f * s, q * g + r);
// x^3 ∈ Z[y][x]
let f = polynomial![polynomial![], polynomial![], polynomial![], polynomial![1]];
// yx ∈ Z[y][x]
let g = polynomial![polynomial![], polynomial![0, 1]];
let mut r = f.clone();
let (s, q) = r.pseudo_division(&g);
assert_eq!(f * s, q * g + r);
make polynomial monic
use num::Rational64;
use polynomial_ring::Polynomial;
let mut p = Polynomial::new(vec![1, 2, 3].into_iter().map(|x| Rational64::from_integer(x)).collect());
p.monic();
let q = Polynomial::new(vec![(1, 3), (2, 3), (1, 1)].into_iter().map(|(n, d)| Rational64::new(n, d)).collect());
assert_eq!(p, q);
polynomial division
use num::Rational64;
use polynomial_ring::Polynomial;
let f = Polynomial::new(vec![3, 1, 4, 1, 5].into_iter().map(|x| Rational64::from_integer(x)).collect());
let g = Polynomial::new(vec![2, 7, 1].into_iter().map(|x| Rational64::from_integer(x)).collect());
let mut r = f.clone();
let q = r.division(&g);
assert_eq!(f, q * g + r);
square free
use polynomial_ring::{Polynomial, polynomial};
use num::Rational64;
let f = polynomial![Rational64::from(1), Rational64::from(1)];
let g = polynomial![Rational64::from(1), Rational64::from(1), Rational64::from(1)];
let p = &f * &f * &f * &g * &g; // (x+1)^3(x^2+x+1)^2
assert_eq!(p.square_free(), &f * &g); // (x+1)(x^2+x+1)
Trait Implementations
impl<'a, M> Add<&'a Polynomial<M>> for &'a Polynomial<M> where
M: Sized + Clone + Zero + for<'x> AddAssign<&'x M>,
impl<'a, M> Add<&'a Polynomial<M>> for &'a Polynomial<M> where
M: Sized + Clone + Zero + for<'x> AddAssign<&'x M>,
impl<'a, M> Add<&'a Polynomial<M>> for Polynomial<M> where
M: Sized + Clone + Zero + for<'x> AddAssign<&'x M>,
impl<'a, M> Add<&'a Polynomial<M>> for Polynomial<M> where
M: Sized + Clone + Zero + for<'x> AddAssign<&'x M>,
impl<'a, M> Add<Polynomial<M>> for &'a Polynomial<M> where
M: Sized + Clone + Zero + for<'x> AddAssign<&'x M>,
impl<'a, M> Add<Polynomial<M>> for &'a Polynomial<M> where
M: Sized + Clone + Zero + for<'x> AddAssign<&'x M>,
type Output = Polynomial<M>
type Output = Polynomial<M>
The resulting type after applying the +
operator.
Performs the +
operation. Read more
impl<M> Add<Polynomial<M>> for Polynomial<M> where
M: Sized + Clone + Zero + for<'x> AddAssign<&'x M>,
impl<M> Add<Polynomial<M>> for Polynomial<M> where
M: Sized + Clone + Zero + for<'x> AddAssign<&'x M>,
impl<'a, M> AddAssign<&'a Polynomial<M>> for Polynomial<M> where
M: Sized + Clone + Zero + for<'x> AddAssign<&'x M>,
impl<'a, M> AddAssign<&'a Polynomial<M>> for Polynomial<M> where
M: Sized + Clone + Zero + for<'x> AddAssign<&'x M>,
Performs the +=
operation. Read more
impl<M> AddAssign<Polynomial<M>> for Polynomial<M> where
M: Sized + Clone + Zero + for<'x> AddAssign<&'x M>,
impl<M> AddAssign<Polynomial<M>> for Polynomial<M> where
M: Sized + Clone + Zero + for<'x> AddAssign<&'x M>,
Performs the +=
operation. Read more
Returns the “default value” for a type. Read more
type Output = Polynomial<K>
type Output = Polynomial<K>
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
Performs the /=
operation. Read more
type Output = Polynomial<R>
type Output = Polynomial<R>
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
Performs the *=
operation. Read more
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
type Output = Polynomial<K>
type Output = Polynomial<K>
The resulting type after applying the %
operator.
Performs the %
operation. Read more
Performs the %=
operation. Read more
Performs the %=
operation. Read more
impl<'a, G> Sub<&'a Polynomial<G>> for &'a Polynomial<G> where
G: Sized + Clone + Zero + for<'x> SubAssign<&'x G>,
impl<'a, G> Sub<&'a Polynomial<G>> for &'a Polynomial<G> where
G: Sized + Clone + Zero + for<'x> SubAssign<&'x G>,
impl<'a, G> Sub<&'a Polynomial<G>> for Polynomial<G> where
G: Sized + Clone + Zero + for<'x> SubAssign<&'x G>,
impl<'a, G> Sub<&'a Polynomial<G>> for Polynomial<G> where
G: Sized + Clone + Zero + for<'x> SubAssign<&'x G>,
impl<'a, G> Sub<Polynomial<G>> for &'a Polynomial<G> where
G: Sized + Clone + Zero + for<'x> SubAssign<&'x G>,
impl<'a, G> Sub<Polynomial<G>> for &'a Polynomial<G> where
G: Sized + Clone + Zero + for<'x> SubAssign<&'x G>,
type Output = Polynomial<G>
type Output = Polynomial<G>
The resulting type after applying the -
operator.
Performs the -
operation. Read more
impl<G> Sub<Polynomial<G>> for Polynomial<G> where
G: Sized + Clone + Zero + for<'x> SubAssign<&'x G>,
impl<G> Sub<Polynomial<G>> for Polynomial<G> where
G: Sized + Clone + Zero + for<'x> SubAssign<&'x G>,
impl<'a, G> SubAssign<&'a Polynomial<G>> for Polynomial<G> where
G: Sized + Clone + Zero + for<'x> SubAssign<&'x G>,
impl<'a, G> SubAssign<&'a Polynomial<G>> for Polynomial<G> where
G: Sized + Clone + Zero + for<'x> SubAssign<&'x G>,
Performs the -=
operation. Read more
impl<G> SubAssign<Polynomial<G>> for Polynomial<G> where
G: Sized + Clone + Zero + for<'x> SubAssign<&'x G>,
impl<G> SubAssign<Polynomial<G>> for Polynomial<G> where
G: Sized + Clone + Zero + for<'x> SubAssign<&'x G>,
Performs the -=
operation. Read more
impl<M> Sum<Polynomial<M>> for Polynomial<M> where
M: Sized + Clone + Zero + for<'x> AddAssign<&'x M>,
impl<M> Sum<Polynomial<M>> for Polynomial<M> where
M: Sized + Clone + Zero + for<'x> AddAssign<&'x M>,
Auto Trait Implementations
impl<T> RefUnwindSafe for Polynomial<T> where
T: RefUnwindSafe,
impl<T> Send for Polynomial<T> where
T: Send,
impl<T> Sync for Polynomial<T> where
T: Sync,
impl<T> Unpin for Polynomial<T> where
T: Unpin,
impl<T> UnwindSafe for Polynomial<T> where
T: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more