malachite_nz/integer_polynomial/comparison/partial_eq_natural_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_polynomial::IntegerPolynomial;
10use crate::natural_polynomial::NaturalPolynomial;
11
12impl PartialEq<NaturalPolynomial> for IntegerPolynomial {
13 /// Determines whether an [`IntegerPolynomial`] is equal to a [`NaturalPolynomial`].
14 ///
15 /// The two are equal when they have the same coefficients, which, since neither stores trailing
16 /// zeros, means the same number of coefficients and equal coefficients in each position. So the
17 /// zero polynomials are equal, and an [`IntegerPolynomial`] with a negative coefficient is
18 /// equal to no [`NaturalPolynomial`].
19 ///
20 /// # Worst-case complexity
21 /// $T(n) = O(n)$
22 ///
23 /// $M(n) = O(1)$
24 ///
25 /// where $T$ is time, $M$ is additional memory, and $n$ is the smaller of the two polynomials'
26 /// total number of bits, summed over their coefficients. Polynomials of different degrees are
27 /// compared in constant time.
28 ///
29 /// # Examples
30 /// See [here](super::partial_eq_natural_polynomial#partial_eq).
31 fn eq(&self, other: &NaturalPolynomial) -> bool {
32 let other = other.coefficients_asc();
33 self.coefficients.len() == other.len()
34 && self.coefficients.iter().zip(other).all(|(x, y)| x == y)
35 }
36}
37
38impl PartialEq<IntegerPolynomial> for NaturalPolynomial {
39 /// Determines whether a [`NaturalPolynomial`] is equal to an [`IntegerPolynomial`].
40 ///
41 /// The two are equal when they have the same coefficients, so the zero polynomials are equal.
42 ///
43 /// # Worst-case complexity
44 /// $T(n) = O(n)$
45 ///
46 /// $M(n) = O(1)$
47 ///
48 /// where $T$ is time, $M$ is additional memory, and $n$ is the smaller of the two polynomials'
49 /// total number of bits, summed over their coefficients. Polynomials of different degrees are
50 /// compared in constant time.
51 ///
52 /// # Examples
53 /// See [here](super::partial_eq_natural_polynomial#partial_eq).
54 #[inline]
55 fn eq(&self, other: &IntegerPolynomial) -> bool {
56 other == self
57 }
58}