malachite_nz/gaussian_integer/arithmetic/conjugate.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 malachite_base::num::arithmetic::traits::{Conjugate, ConjugateAssign, NegAssign};
11
12impl Conjugate for GaussianInteger {
13 type Output = Self;
14
15 /// Computes the complex conjugate of a [`GaussianInteger`], taking it by value. The sign of the
16 /// imaginary part is flipped.
17 ///
18 /// $$
19 /// f(x) = \overline{x} = \Re(x) - \Im(x) i.
20 /// $$
21 ///
22 /// # Worst-case complexity
23 /// Constant time and additional memory.
24 ///
25 /// # Examples
26 /// ```
27 /// use malachite_base::num::arithmetic::traits::Conjugate;
28 /// use malachite_base::num::basic::traits::I;
29 /// use malachite_nz::gaussian_integer::GaussianInteger;
30 /// use std::str::FromStr;
31 ///
32 /// assert_eq!(GaussianInteger::I.conjugate().to_string(), "-i");
33 /// assert_eq!(
34 /// GaussianInteger::from_str("2-3i")
35 /// .unwrap()
36 /// .conjugate()
37 /// .to_string(),
38 /// "2+3i"
39 /// );
40 /// assert_eq!(
41 /// GaussianInteger::from_str("-123")
42 /// .unwrap()
43 /// .conjugate()
44 /// .to_string(),
45 /// "-123"
46 /// );
47 /// ```
48 fn conjugate(mut self) -> Self {
49 self.conjugate_assign();
50 self
51 }
52}
53
54impl Conjugate for &GaussianInteger {
55 type Output = GaussianInteger;
56
57 /// Computes the complex conjugate of a [`GaussianInteger`], taking it by reference. The sign of
58 /// the imaginary part is flipped.
59 ///
60 /// $$
61 /// f(x) = \overline{x} = \Re(x) - \Im(x) i.
62 /// $$
63 ///
64 /// # Worst-case complexity
65 /// $T(n) = O(n)$
66 ///
67 /// $M(n) = O(n)$
68 ///
69 /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum number of significant
70 /// bits of the real and imaginary parts.
71 ///
72 /// # Examples
73 /// ```
74 /// use malachite_base::num::arithmetic::traits::Conjugate;
75 /// use malachite_nz::gaussian_integer::GaussianInteger;
76 /// use std::str::FromStr;
77 ///
78 /// let x = GaussianInteger::from_str("2-3i").unwrap();
79 /// assert_eq!((&x).conjugate().to_string(), "2+3i");
80 /// ```
81 fn conjugate(self) -> GaussianInteger {
82 GaussianInteger {
83 real: self.real.clone(),
84 imaginary: -&self.imaginary,
85 }
86 }
87}
88
89impl ConjugateAssign for GaussianInteger {
90 /// Replaces a [`GaussianInteger`] with its complex conjugate. The sign of the imaginary part is
91 /// flipped.
92 ///
93 /// $$
94 /// x \gets \overline{x} = \Re(x) - \Im(x) i.
95 /// $$
96 ///
97 /// # Worst-case complexity
98 /// Constant time and additional memory.
99 ///
100 /// # Examples
101 /// ```
102 /// use malachite_base::num::arithmetic::traits::ConjugateAssign;
103 /// use malachite_nz::gaussian_integer::GaussianInteger;
104 /// use std::str::FromStr;
105 ///
106 /// let mut x = GaussianInteger::from_str("2-3i").unwrap();
107 /// x.conjugate_assign();
108 /// assert_eq!(x.to_string(), "2+3i");
109 /// ```
110 #[inline]
111 fn conjugate_assign(&mut self) {
112 self.imaginary.neg_assign();
113 }
114}