Skip to main content

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