Skip to main content

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