Skip to main content

malachite_nz/integer_polynomial/comparison/
partial_eq_natural.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::Natural;
11
12impl PartialEq<Natural> for IntegerPolynomial {
13    /// Determines whether an [`IntegerPolynomial`] is equal to a [`Natural`].
14    ///
15    /// The polynomial is equal to the [`Natural`] when it is the constant polynomial with that
16    /// value, so the zero polynomial is equal to 0 and nothing else, no polynomial with a negative
17    /// constant term is equal to any [`Natural`], and no polynomial of positive degree is equal to
18    /// any [`Natural`].
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
26    /// `min(self.coefficient(0).significant_bits(), other.significant_bits())`.
27    ///
28    /// # Examples
29    /// See [here](super::partial_eq_natural#partial_eq).
30    fn eq(&self, other: &Natural) -> bool {
31        match self.coefficients.as_slice() {
32            [] => *other == 0u32,
33            [c] => c == other,
34            _ => false,
35        }
36    }
37}
38
39impl PartialEq<IntegerPolynomial> for Natural {
40    /// Determines whether a [`Natural`] is equal to an [`IntegerPolynomial`].
41    ///
42    /// The [`Natural`] is equal to the polynomial when the polynomial is the constant polynomial
43    /// with that value, so 0 is equal to the zero polynomial.
44    ///
45    /// # Worst-case complexity
46    /// $T(n) = O(n)$
47    ///
48    /// $M(n) = O(1)$
49    ///
50    /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.significant_bits(),
51    /// other.coefficient(0).significant_bits())`.
52    ///
53    /// # Examples
54    /// See [here](super::partial_eq_natural#partial_eq).
55    #[inline]
56    fn eq(&self, other: &IntegerPolynomial) -> bool {
57        other == self
58    }
59}