Skip to main content

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