Skip to main content

malachite_nz/gaussian_integer/comparison/
eq_abs.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 core::cmp::Ordering::Equal;
11use malachite_base::num::comparison::traits::{EqAbs, OrdAbs};
12
13impl EqAbs for GaussianInteger {
14    /// Determines whether the absolute values of two [`GaussianInteger`]s are equal.
15    ///
16    /// The absolute value of a complex number is its distance from the origin, so this is
17    /// equivalent to comparing squared absolute values. The comparison delegates to [`OrdAbs`],
18    /// whose componentwise and crosswise screens usually decide the answer without computing the
19    /// squared absolute values.
20    ///
21    /// # Worst-case complexity
22    /// $T(n) = O(n \log n \log\log n)$
23    ///
24    /// $M(n) = O(n \log n)$
25    ///
26    /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum number of significant
27    /// bits of the real and imaginary parts of `self` and `other`.
28    ///
29    /// # Examples
30    /// ```
31    /// use malachite_base::num::comparison::traits::EqAbs;
32    /// use malachite_nz::gaussian_integer::GaussianInteger;
33    /// use std::str::FromStr;
34    ///
35    /// let x = GaussianInteger::from_str("1+2i").unwrap();
36    /// let y = GaussianInteger::from_str("-2+i").unwrap();
37    /// assert!(x.eq_abs(&y));
38    ///
39    /// let x = GaussianInteger::from_str("2+2i").unwrap();
40    /// let y = GaussianInteger::from_str("3i").unwrap();
41    /// // |2+2i|^2 = 8 and |3i|^2 = 9
42    /// assert_eq!(x.eq_abs(&y), false);
43    /// ```
44    #[inline]
45    fn eq_abs(&self, other: &Self) -> bool {
46        self.cmp_abs(other) == Equal
47    }
48}