malachite_nz/gaussian_integer/comparison/cmp_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::{self, Equal};
11use malachite_base::num::arithmetic::traits::AbsSquared;
12use malachite_base::num::comparison::traits::{OrdAbs, PartialOrdAbs};
13
14// If two component comparisons pull in the same direction (or one is a tie), they decide the
15// comparison of the sums of squares; only a strict conflict is indecisive.
16fn combine(x: Ordering, y: Ordering) -> Option<Ordering> {
17 if x == y || y == Equal {
18 Some(x)
19 } else if x == Equal {
20 Some(y)
21 } else {
22 None
23 }
24}
25
26impl PartialOrdAbs for GaussianInteger {
27 /// Compares the absolute values of two [`GaussianInteger`]s.
28 ///
29 /// See the documentation for the [`OrdAbs`] implementation.
30 #[inline]
31 fn partial_cmp_abs(&self, other: &Self) -> Option<Ordering> {
32 Some(self.cmp_abs(other))
33 }
34}
35
36impl OrdAbs for GaussianInteger {
37 /// Compares the absolute values of two [`GaussianInteger`]s.
38 ///
39 /// The absolute value of a complex number is its distance from the origin, so this is
40 /// equivalent to comparing squared absolute values:
41 ///
42 /// $$
43 /// f(x, y) = \operatorname{cmp}(|x|, |y|) = \operatorname{cmp}(|x|^2, |y|^2).
44 /// $$
45 ///
46 /// The squared absolute values are usually not actually computed: comparing the
47 /// [`Integer`](crate::integer::Integer) parts componentwise, either directly or crosswise,
48 /// often decides the ordering, and the [`AbsSquared`] fallback only runs when both pairings
49 /// strictly conflict.
50 ///
51 /// # Worst-case complexity
52 /// $T(n) = O(n \log n \log\log n)$
53 ///
54 /// $M(n) = O(n \log n)$
55 ///
56 /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum number of significant
57 /// bits of the real and imaginary parts of `self` and `other`.
58 ///
59 /// # Examples
60 /// ```
61 /// use malachite_base::num::comparison::traits::{OrdAbs, PartialOrdAbs};
62 /// use malachite_nz::gaussian_integer::GaussianInteger;
63 /// use std::cmp::Ordering::*;
64 /// use std::str::FromStr;
65 ///
66 /// let x = GaussianInteger::from_str("2+2i").unwrap();
67 /// let y = GaussianInteger::from_str("3i").unwrap();
68 /// // |2+2i|^2 = 8 and |3i|^2 = 9
69 /// assert_eq!(x.cmp_abs(&y), Less);
70 /// assert!(x.lt_abs(&y));
71 ///
72 /// let x = GaussianInteger::from_str("1+2i").unwrap();
73 /// let y = GaussianInteger::from_str("-2+i").unwrap();
74 /// assert_eq!(x.cmp_abs(&y), Equal);
75 ///
76 /// let x = GaussianInteger::from_str("3").unwrap();
77 /// let y = GaussianInteger::from_str("2+2i").unwrap();
78 /// // |3|^2 = 9 and |2+2i|^2 = 8
79 /// assert_eq!(x.cmp_abs(&y), Greater);
80 /// ```
81 fn cmp_abs(&self, other: &Self) -> Ordering {
82 if let Some(o) = combine(
83 self.real.cmp_abs(&other.real),
84 self.imaginary.cmp_abs(&other.imaginary),
85 ) {
86 return o;
87 }
88 if let Some(o) = combine(
89 self.real.cmp_abs(&other.imaginary),
90 self.imaginary.cmp_abs(&other.real),
91 ) {
92 return o;
93 }
94 self.abs_squared().cmp(&other.abs_squared())
95 }
96}