Skip to main content

malachite_nz/gaussian_integer/comparison/
eq_abs_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::gaussian_integer::GaussianInteger;
10use crate::natural::Natural;
11use malachite_base::num::arithmetic::traits::AbsSquared;
12use malachite_base::num::comparison::traits::{EqAbs, PartialOrdAbs};
13
14impl EqAbs<Natural> for GaussianInteger {
15    /// Determines whether the absolute values of a [`GaussianInteger`] and a [`Natural`] are equal.
16    ///
17    /// The absolute value of a complex number is its distance from the origin, so two values are
18    /// equal in absolute value exactly when their squared absolute values are equal. Purely real
19    /// and purely imaginary values are handled by comparing single components. Otherwise, equality
20    /// is impossible unless both components are smaller in absolute value than the real operand, so
21    /// the squared absolute values are only computed in that case.
22    ///
23    /// # Worst-case complexity
24    /// $T(n) = O(n \log n \log\log n)$
25    ///
26    /// $M(n) = O(n \log n)$
27    ///
28    /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum number of significant
29    /// bits of the real and imaginary parts of `self` and of `other`.
30    ///
31    /// # Examples
32    /// ```
33    /// use malachite_base::num::comparison::traits::EqAbs;
34    /// use malachite_nz::gaussian_integer::GaussianInteger;
35    /// use malachite_nz::natural::Natural;
36    /// use std::str::FromStr;
37    ///
38    /// // |3+4i| = 5
39    /// let x = GaussianInteger::from_str("3+4i").unwrap();
40    /// assert!(x.eq_abs(&Natural::from(5u32)));
41    /// assert_eq!(x.eq_abs(&Natural::from(4u32)), false);
42    /// ```
43    fn eq_abs(&self, other: &Natural) -> bool {
44        if self.imaginary == 0u32 {
45            self.real.eq_abs(other)
46        } else if self.real == 0u32 {
47            self.imaginary.eq_abs(other)
48        } else {
49            self.real.lt_abs(other)
50                && self.imaginary.lt_abs(other)
51                && self.abs_squared() == other.abs_squared()
52        }
53    }
54}
55
56impl EqAbs<GaussianInteger> for Natural {
57    /// Determines whether the absolute values of a [`Natural`] and a [`GaussianInteger`] are equal.
58    ///
59    /// # Worst-case complexity
60    /// $T(n) = O(n \log n \log\log n)$
61    ///
62    /// $M(n) = O(n \log n)$
63    ///
64    /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum number of significant
65    /// bits of the real and imaginary parts of `other` and of `self`.
66    ///
67    /// # Examples
68    /// ```
69    /// use malachite_base::num::comparison::traits::EqAbs;
70    /// use malachite_nz::gaussian_integer::GaussianInteger;
71    /// use malachite_nz::natural::Natural;
72    /// use std::str::FromStr;
73    ///
74    /// // |3+4i| = 5
75    /// let y = GaussianInteger::from_str("3+4i").unwrap();
76    /// assert!(Natural::from(5u32).eq_abs(&y));
77    /// assert_eq!(Natural::from(4u32).eq_abs(&y), false);
78    /// ```
79    #[inline]
80    fn eq_abs(&self, other: &GaussianInteger) -> bool {
81        other.eq_abs(self)
82    }
83}