Struct polynomial_ring::Polynomial
source · [−]pub struct Polynomial<T> { /* private fields */ }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);calculate resultant
use polynomial_ring::{polynomial, Polynomial};
use num_traits::{One, Zero};
let f = polynomial![0, 2, 0, 1]; // 2x+x^3 ∈ Z[x]
let g = polynomial![2, 3, 5]; // 2+3x+5x^2 ∈ Z[x]
let r = f.resultant(g);
assert_eq!(r, 164);
let f = polynomial![-4, 0, 0, 0, 1]; // -4+x^4 ∈ Z[x]
let g = polynomial![0, 2, 0, 1]; // 2x+x^3 ∈ Z[x]
let r = f.resultant(g); // deg(gcd(f, g)) = deg(x^2-2) = 2 ≠ 0
assert_eq!(r, 0);
let f = polynomial![polynomial![1], polynomial![0], polynomial![1]]; // 1+x^2 ∈ Z[y][x]
let g = polynomial![polynomial![1], polynomial![1, 2]]; // 1+(1+2y)x ∈ Z[y][x]
let r = f.resultant(g);
assert_eq!(r, polynomial![2, 4, 4]); // 2+4y+4y^2
let y3 = Polynomial::from_monomial(polynomial![-1], 3); // -y^3 ∈ Z[x][y]
let y2xy = Polynomial::from_monomial(polynomial![0, -1], 2) + &y3; // -xy^2-y^3 ∈ Z[x][y]
let x = polynomial![polynomial![0, 1]]; // x ∈ Z[x][y]
let f = polynomial![y3, polynomial![], x.clone()]; // -y^3+xz^2 ∈ Z[x][y][z]
let g = polynomial![y2xy, polynomial![], x]; // -xy^2-y^3+xz^2 ∈ Z[x][y][z]
let r = f.resultant(g);
assert_eq!(r, Polynomial::from_monomial(Polynomial::from_monomial(1, 4), 4)); // x^4y^4pub fn primitive_part_mut(&mut self) where
R: Sized + Clone + Zero + for<'x> DivAssign<&'x R> + RingNormalize,
for<'x> &'x R: Rem<Output = R>,
pub fn primitive_part_mut(&mut self) where
R: Sized + Clone + Zero + for<'x> DivAssign<&'x R> + RingNormalize,
for<'x> &'x R: Rem<Output = R>,
calculate primitive part of input polynomial
divide polynomial by GCD of coefficents.
use polynomial_ring::{polynomial, Polynomial};
use num_traits::{One, Zero};
let mut f = polynomial![2, 4, -2, 6]; // 2+4x+2x^2+6x^3 ∈ Z[x]
f.primitive_part_mut();
assert_eq!(f, polynomial![1, 2, -1, 3]);// 1+2x+x^2+3x^3 ∈ Z[x]
let mut g = polynomial![polynomial![1, 1], polynomial![1, 2, 1], polynomial![3, 4, 1], polynomial![-1, -1]]; // (1+x)+(1+2x+x^2)y+(3+4x+x^2)y^2+(-1-x)y^3 ∈ Z[x][y]
g.primitive_part_mut();
assert_eq!(g, polynomial![polynomial![1], polynomial![1, 1], polynomial![3, 1], polynomial![-1]]); // 1+(1+x)y+(3+x)y^2-y^3 ∈ Z[x][y]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);calculate square-free polynomial
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