Skip to main content

malachite_nz/gaussian_integer/arithmetic/
sub.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::ops::{Sub, SubAssign};
11
12impl Sub<Self> for GaussianInteger {
13    type Output = Self;
14
15    /// Subtracts two [`GaussianInteger`]s, taking both by value.
16    ///
17    /// $$
18    /// f(x, y) = x - y.
19    /// $$
20    ///
21    /// # Worst-case complexity
22    /// $T(n) = O(n)$
23    ///
24    /// $M(n) = O(n)$
25    ///
26    /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum number of significant
27    /// bits of the real and imaginary parts of `self` and `other`.
28    ///
29    /// # Examples
30    /// ```
31    /// use malachite_nz::gaussian_integer::GaussianInteger;
32    /// use std::str::FromStr;
33    ///
34    /// let x = GaussianInteger::from_str("2-3i").unwrap();
35    /// let y = GaussianInteger::from_str("-1+4i").unwrap();
36    /// assert_eq!((x - y).to_string(), "3-7i");
37    /// ```
38    #[inline]
39    fn sub(mut self, other: Self) -> Self {
40        self -= other;
41        self
42    }
43}
44
45impl Sub<&Self> for GaussianInteger {
46    type Output = Self;
47
48    /// Subtracts two [`GaussianInteger`]s, taking the first by value and the second by reference.
49    ///
50    /// $$
51    /// f(x, y) = x - y.
52    /// $$
53    ///
54    /// # Worst-case complexity
55    /// $T(n) = O(n)$
56    ///
57    /// $M(n) = O(n)$
58    ///
59    /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum number of significant
60    /// bits of the real and imaginary parts of `self` and `other`.
61    ///
62    /// # Examples
63    /// ```
64    /// use malachite_nz::gaussian_integer::GaussianInteger;
65    /// use std::str::FromStr;
66    ///
67    /// let x = GaussianInteger::from_str("2-3i").unwrap();
68    /// let y = GaussianInteger::from_str("-1+4i").unwrap();
69    /// assert_eq!((x - &y).to_string(), "3-7i");
70    /// ```
71    #[inline]
72    fn sub(mut self, other: &Self) -> Self {
73        self -= other;
74        self
75    }
76}
77
78impl Sub<GaussianInteger> for &GaussianInteger {
79    type Output = GaussianInteger;
80
81    /// Subtracts two [`GaussianInteger`]s, taking the first by reference and the second by value.
82    ///
83    /// $$
84    /// f(x, y) = x - y.
85    /// $$
86    ///
87    /// # Worst-case complexity
88    /// $T(n) = O(n)$
89    ///
90    /// $M(n) = O(n)$
91    ///
92    /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum number of significant
93    /// bits of the real and imaginary parts of `self` and `other`.
94    ///
95    /// # Examples
96    /// ```
97    /// use malachite_nz::gaussian_integer::GaussianInteger;
98    /// use std::str::FromStr;
99    ///
100    /// let x = GaussianInteger::from_str("2-3i").unwrap();
101    /// let y = GaussianInteger::from_str("-1+4i").unwrap();
102    /// assert_eq!((&x - y).to_string(), "3-7i");
103    /// ```
104    #[inline]
105    fn sub(self, other: GaussianInteger) -> GaussianInteger {
106        GaussianInteger {
107            real: &self.real - other.real,
108            imaginary: &self.imaginary - other.imaginary,
109        }
110    }
111}
112
113impl Sub<&GaussianInteger> for &GaussianInteger {
114    type Output = GaussianInteger;
115
116    /// Subtracts two [`GaussianInteger`]s, taking both by reference.
117    ///
118    /// $$
119    /// f(x, y) = x - y.
120    /// $$
121    ///
122    /// # Worst-case complexity
123    /// $T(n) = O(n)$
124    ///
125    /// $M(n) = O(n)$
126    ///
127    /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum number of significant
128    /// bits of the real and imaginary parts of `self` and `other`.
129    ///
130    /// # Examples
131    /// ```
132    /// use malachite_nz::gaussian_integer::GaussianInteger;
133    /// use std::str::FromStr;
134    ///
135    /// let x = GaussianInteger::from_str("1000000000000+i").unwrap();
136    /// let y = GaussianInteger::from_str("i").unwrap();
137    /// assert_eq!((&x - &y).to_string(), "1000000000000");
138    /// ```
139    #[inline]
140    fn sub(self, other: &GaussianInteger) -> GaussianInteger {
141        GaussianInteger {
142            real: &self.real - &other.real,
143            imaginary: &self.imaginary - &other.imaginary,
144        }
145    }
146}
147
148impl SubAssign<Self> for GaussianInteger {
149    /// Subtracts a [`GaussianInteger`] from a [`GaussianInteger`] in place, taking the
150    /// [`GaussianInteger`] on the right-hand side by value.
151    ///
152    /// $$
153    /// x \gets x - y.
154    /// $$
155    ///
156    /// # Worst-case complexity
157    /// $T(n) = O(n)$
158    ///
159    /// $M(n) = O(n)$
160    ///
161    /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum number of significant
162    /// bits of the real and imaginary parts of `self` and `other`.
163    ///
164    /// # Examples
165    /// ```
166    /// use malachite_nz::gaussian_integer::GaussianInteger;
167    /// use std::str::FromStr;
168    ///
169    /// let x = GaussianInteger::from_str("2-3i").unwrap();
170    /// let y = GaussianInteger::from_str("-1+4i").unwrap();
171    /// let mut sum = x;
172    /// sum -= y;
173    /// assert_eq!(sum.to_string(), "3-7i");
174    /// ```
175    #[inline]
176    fn sub_assign(&mut self, other: Self) {
177        self.real -= other.real;
178        self.imaginary -= other.imaginary;
179    }
180}
181
182impl SubAssign<&Self> for GaussianInteger {
183    /// Subtracts a [`GaussianInteger`] from a [`GaussianInteger`] in place, taking the
184    /// [`GaussianInteger`] on the right-hand side by reference.
185    ///
186    /// $$
187    /// x \gets x - y.
188    /// $$
189    ///
190    /// # Worst-case complexity
191    /// $T(n) = O(n)$
192    ///
193    /// $M(n) = O(n)$
194    ///
195    /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum number of significant
196    /// bits of the real and imaginary parts of `self` and `other`.
197    ///
198    /// # Examples
199    /// ```
200    /// use malachite_nz::gaussian_integer::GaussianInteger;
201    /// use std::str::FromStr;
202    ///
203    /// let x = GaussianInteger::from_str("2-3i").unwrap();
204    /// let y = GaussianInteger::from_str("-1+4i").unwrap();
205    /// let mut sum = x;
206    /// sum -= &y;
207    /// assert_eq!(sum.to_string(), "3-7i");
208    /// ```
209    #[inline]
210    fn sub_assign(&mut self, other: &Self) {
211        self.real -= &other.real;
212        self.imaginary -= &other.imaginary;
213    }
214}