malachite_nz/integer_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::integer_vector::IntegerVector;
10use core::ops::Neg;
11use malachite_base::num::arithmetic::traits::NegAssign;
12
13impl Neg for IntegerVector {
14 type Output = Self;
15
16 /// Negates an [`IntegerVector`], 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_nz::integer_vector::IntegerVector;
35 ///
36 /// let v = IntegerVector::from_str("(1, -3, 2)").unwrap();
37 /// assert_eq!((-v).to_string(), "(-1, 3, -2)");
38 /// assert_eq!((-IntegerVector::from_str("()").unwrap()).to_string(), "()");
39 /// ```
40 ///
41 /// This is equivalent to `_fmpz_vec_neg` from `fmpz_vec/neg.c`, FLINT 3.6.0.
42 #[inline]
43 fn neg(mut self) -> Self {
44 self.neg_assign();
45 self
46 }
47}
48
49impl Neg for &IntegerVector {
50 type Output = IntegerVector;
51
52 /// Negates an [`IntegerVector`], taking it by reference.
53 ///
54 /// Every element is negated, so the dimension is unchanged.
55 ///
56 /// $$
57 /// f(v) = -v.
58 /// $$
59 ///
60 /// # Worst-case complexity
61 /// $T(n) = O(n)$
62 ///
63 /// $M(n) = O(n)$
64 ///
65 /// where $T$ is time, $M$ is additional memory, and $n$ is the total number of bits of the
66 /// elements.
67 ///
68 /// # Examples
69 /// ```
70 /// use core::str::FromStr;
71 /// use malachite_nz::integer_vector::IntegerVector;
72 ///
73 /// let v = IntegerVector::from_str("(1, -3, 2)").unwrap();
74 /// assert_eq!((-&v).to_string(), "(-1, 3, -2)");
75 /// ```
76 ///
77 /// This is equivalent to `_fmpz_vec_neg` from `fmpz_vec/neg.c`, FLINT 3.6.0.
78 #[inline]
79 fn neg(self) -> IntegerVector {
80 IntegerVector {
81 elements: self.elements.iter().map(|x| -x).collect(),
82 }
83 }
84}
85
86impl NegAssign for IntegerVector {
87 /// Negates an [`IntegerVector`] in place.
88 ///
89 /// $$
90 /// v \gets -v.
91 /// $$
92 ///
93 /// # Worst-case complexity
94 /// $T(n) = O(n)$
95 ///
96 /// $M(n) = O(1)$
97 ///
98 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.dimension()`.
99 ///
100 /// # Examples
101 /// ```
102 /// use core::str::FromStr;
103 /// use malachite_base::num::arithmetic::traits::NegAssign;
104 /// use malachite_nz::integer_vector::IntegerVector;
105 ///
106 /// let mut v = IntegerVector::from_str("(1, -3, 2)").unwrap();
107 /// v.neg_assign();
108 /// assert_eq!(v.to_string(), "(-1, 3, -2)");
109 /// ```
110 #[inline]
111 fn neg_assign(&mut self) {
112 for x in &mut self.elements {
113 x.neg_assign();
114 }
115 }
116}