Skip to main content

malachite_nz/gaussian_integer/comparison/
cmp_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 core::cmp::Ordering::{self, Greater};
12use malachite_base::num::arithmetic::traits::AbsSquared;
13use malachite_base::num::comparison::traits::PartialOrdAbs;
14
15impl PartialOrdAbs<Integer> for GaussianInteger {
16    /// Compares the absolute values of a [`GaussianInteger`] and an [`Integer`].
17    ///
18    /// The absolute value of a complex number is its distance from the origin, so this is
19    /// equivalent to comparing squared absolute values. Purely real and purely imaginary values are
20    /// handled by comparing single components. Otherwise, the complex value is greater in absolute
21    /// value unless both of its 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, PartialOrdAbs};
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.gt_abs(&Integer::from(4)));
43    /// assert!(x.lt_abs(&Integer::from(-6)));
44    /// ```
45    fn partial_cmp_abs(&self, other: &Integer) -> Option<Ordering> {
46        if self.imaginary == 0u32 {
47            self.real.partial_cmp_abs(other)
48        } else if self.real == 0u32 {
49            self.imaginary.partial_cmp_abs(other)
50        } else if !self.real.lt_abs(other) || !self.imaginary.lt_abs(other) {
51            Some(Greater)
52        } else {
53            Some(self.abs_squared().cmp(&other.abs_squared()))
54        }
55    }
56}
57
58impl PartialOrdAbs<GaussianInteger> for Integer {
59    /// Compares the absolute values of an [`Integer`] and a [`GaussianInteger`].
60    ///
61    /// # Worst-case complexity
62    /// $T(n) = O(n \log n \log\log n)$
63    ///
64    /// $M(n) = O(n \log n)$
65    ///
66    /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum number of significant
67    /// bits of the real and imaginary parts of `other` and of `self`.
68    ///
69    /// # Examples
70    /// ```
71    /// use malachite_base::num::comparison::traits::{EqAbs, PartialOrdAbs};
72    /// use malachite_nz::gaussian_integer::GaussianInteger;
73    /// use malachite_nz::integer::Integer;
74    /// use std::str::FromStr;
75    ///
76    /// // |3+4i| = 5
77    /// let y = GaussianInteger::from_str("3+4i").unwrap();
78    /// assert!(Integer::from(-5).eq_abs(&y));
79    /// assert!(Integer::from(4).lt_abs(&y));
80    /// assert!(Integer::from(-6).gt_abs(&y));
81    /// ```
82    #[inline]
83    fn partial_cmp_abs(&self, other: &GaussianInteger) -> Option<Ordering> {
84        other.partial_cmp_abs(self).map(Ordering::reverse)
85    }
86}