Skip to main content

malachite_nz/integer_polynomial/comparison/
partial_eq_unsigned_polynomial.rs

1// Copyright © 2026 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::integer::Integer;
10use crate::integer_polynomial::IntegerPolynomial;
11use malachite_base::num::basic::unsigneds::PrimitiveUnsigned;
12use malachite_base::unsigned_polynomial::UnsignedPolynomial;
13
14impl<T: PrimitiveUnsigned> PartialEq<UnsignedPolynomial<T>> for IntegerPolynomial
15where
16    Integer: PartialEq<T>,
17{
18    /// Determines whether an [`IntegerPolynomial`] is equal to an [`UnsignedPolynomial`].
19    ///
20    /// The two are equal when they have the same coefficients, which, since neither stores trailing
21    /// zeros, means the same number of coefficients and equal coefficients in each position. So the
22    /// zero polynomials are equal, and an [`IntegerPolynomial`] with a negative coefficient, or one
23    /// too large for `T`, is equal to no [`UnsignedPolynomial<T>`].
24    ///
25    /// # Worst-case complexity
26    /// $T(n) = O(n)$
27    ///
28    /// $M(n) = O(1)$
29    ///
30    /// where $T$ is time, $M$ is additional memory, and $n$ is the number of coefficients.
31    /// Polynomials of different degrees are compared in constant time.
32    ///
33    /// # Examples
34    /// See [here](super::partial_eq_unsigned_polynomial#partial_eq).
35    fn eq(&self, other: &UnsignedPolynomial<T>) -> bool {
36        let other = other.coefficients_asc();
37        self.coefficients.len() == other.len()
38            && self.coefficients.iter().zip(other).all(|(x, y)| x == y)
39    }
40}
41
42impl<T: PrimitiveUnsigned> PartialEq<IntegerPolynomial> for UnsignedPolynomial<T>
43where
44    Integer: PartialEq<T>,
45{
46    /// Determines whether an [`UnsignedPolynomial`] is equal to an [`IntegerPolynomial`].
47    ///
48    /// The two are equal when they have the same coefficients, so the zero polynomials are equal.
49    ///
50    /// # Worst-case complexity
51    /// $T(n) = O(n)$
52    ///
53    /// $M(n) = O(1)$
54    ///
55    /// where $T$ is time, $M$ is additional memory, and $n$ is the number of coefficients.
56    /// Polynomials of different degrees are compared in constant time.
57    ///
58    /// # Examples
59    /// See [here](super::partial_eq_unsigned_polynomial#partial_eq).
60    #[inline]
61    fn eq(&self, other: &IntegerPolynomial) -> bool {
62        other == self
63    }
64}