Skip to main content

malachite_q/rational_vector/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::rational_vector::RationalVector;
10use core::ops::Neg;
11use malachite_base::num::arithmetic::traits::NegAssign;
12
13impl Neg for RationalVector {
14    type Output = Self;
15
16    /// Negates a [`RationalVector`], taking it by value.
17    ///
18    /// Every element is negated, so the dimension is unchanged.
19    ///
20    /// $$
21    /// f(v) = -v.
22    /// $$
23    ///
24    /// # Worst-case complexity
25    /// $T(n) = O(n)$
26    ///
27    /// $M(n) = O(1)$
28    ///
29    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.dimension()`.
30    ///
31    /// # Examples
32    /// ```
33    /// use core::str::FromStr;
34    /// use malachite_q::rational_vector::RationalVector;
35    ///
36    /// let v = RationalVector::from_str("(1/2, -3, 2/3)").unwrap();
37    /// assert_eq!((-v).to_string(), "(-1/2, 3, -2/3)");
38    /// assert_eq!((-RationalVector::from_str("()").unwrap()).to_string(), "()");
39    /// ```
40    #[inline]
41    fn neg(mut self) -> Self {
42        self.neg_assign();
43        self
44    }
45}
46
47impl Neg for &RationalVector {
48    type Output = RationalVector;
49
50    /// Negates a [`RationalVector`], taking it by reference.
51    ///
52    /// Every element is negated, so the dimension is unchanged.
53    ///
54    /// $$
55    /// f(v) = -v.
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 total number of bits of the
64    /// elements' numerators and denominators.
65    ///
66    /// # Examples
67    /// ```
68    /// use core::str::FromStr;
69    /// use malachite_q::rational_vector::RationalVector;
70    ///
71    /// let v = RationalVector::from_str("(1/2, -3, 2/3)").unwrap();
72    /// assert_eq!((-&v).to_string(), "(-1/2, 3, -2/3)");
73    /// ```
74    #[inline]
75    fn neg(self) -> RationalVector {
76        RationalVector {
77            elements: self.elements.iter().map(|x| -x).collect(),
78        }
79    }
80}
81
82impl NegAssign for RationalVector {
83    /// Negates a [`RationalVector`] in place.
84    ///
85    /// $$
86    /// v \gets -v.
87    /// $$
88    ///
89    /// # Worst-case complexity
90    /// $T(n) = O(n)$
91    ///
92    /// $M(n) = O(1)$
93    ///
94    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.dimension()`.
95    ///
96    /// # Examples
97    /// ```
98    /// use core::str::FromStr;
99    /// use malachite_base::num::arithmetic::traits::NegAssign;
100    /// use malachite_q::rational_vector::RationalVector;
101    ///
102    /// let mut v = RationalVector::from_str("(1/2, -3, 2/3)").unwrap();
103    /// v.neg_assign();
104    /// assert_eq!(v.to_string(), "(-1/2, 3, -2/3)");
105    /// ```
106    #[inline]
107    fn neg_assign(&mut self) {
108        for x in &mut self.elements {
109            x.neg_assign();
110        }
111    }
112}