Skip to main content

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