Skip to main content

malachite_q/rational_vector/arithmetic/
entrywise_abs.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 malachite_base::num::arithmetic::traits::{Abs, AbsAssign, EntrywiseAbs, EntrywiseAbsAssign};
11
12impl EntrywiseAbs for RationalVector {
13    type Output = Self;
14
15    /// Replaces every element of a [`RationalVector`] by its absolute value, taking the vector by
16    /// value.
17    ///
18    /// The dimension is unchanged. This is not [`Abs`], which for a vector would be its Euclidean
19    /// length.
20    ///
21    /// $$
22    /// f(v) = (|v_0|, |v_1|, \ldots, |v_{n-1}|).
23    /// $$
24    ///
25    /// # Worst-case complexity
26    /// $T(n) = O(n)$
27    ///
28    /// $M(n) = O(1)$
29    ///
30    /// where $T$ is time, $M$ is additional memory, and $n$ is the total number of bits of the
31    /// elements.
32    ///
33    /// # Examples
34    /// ```
35    /// use core::str::FromStr;
36    /// use malachite_base::num::arithmetic::traits::EntrywiseAbs;
37    /// use malachite_q::rational_vector::RationalVector;
38    ///
39    /// let v = RationalVector::from_str("(1/2, -2/3, 0, -3)").unwrap();
40    /// assert_eq!(v.clone().entrywise_abs().to_string(), "(1/2, 2/3, 0, 3)");
41    /// ```
42    #[inline]
43    fn entrywise_abs(mut self) -> Self {
44        self.entrywise_abs_assign();
45        self
46    }
47}
48
49impl EntrywiseAbs for &RationalVector {
50    type Output = RationalVector;
51
52    /// Replaces every element of a [`RationalVector`] by its absolute value, taking the vector by
53    /// reference.
54    ///
55    /// See the documentation for the [`EntrywiseAbs`] implementation on [`RationalVector`] that
56    /// takes the vector by value for details.
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.
65    ///
66    /// # Examples
67    /// ```
68    /// use core::str::FromStr;
69    /// use malachite_base::num::arithmetic::traits::EntrywiseAbs;
70    /// use malachite_q::rational_vector::RationalVector;
71    ///
72    /// let v = RationalVector::from_str("(1/2, -2/3, 0, -3)").unwrap();
73    /// assert_eq!((&v).entrywise_abs().to_string(), "(1/2, 2/3, 0, 3)");
74    /// ```
75    fn entrywise_abs(self) -> RationalVector {
76        RationalVector {
77            elements: self.elements.iter().map(Abs::abs).collect(),
78        }
79    }
80}
81
82impl EntrywiseAbsAssign for RationalVector {
83    /// Replaces every element of a [`RationalVector`] by its absolute value, in place.
84    ///
85    /// See the documentation for the [`EntrywiseAbs`] implementation on [`RationalVector`] that
86    /// takes the vector by value for details.
87    ///
88    /// # Worst-case complexity
89    /// $T(n) = O(n)$
90    ///
91    /// $M(n) = O(1)$
92    ///
93    /// where $T$ is time, $M$ is additional memory, and $n$ is the total number of bits of the
94    /// elements.
95    ///
96    /// # Examples
97    /// ```
98    /// use core::str::FromStr;
99    /// use malachite_base::num::arithmetic::traits::EntrywiseAbsAssign;
100    /// use malachite_q::rational_vector::RationalVector;
101    ///
102    /// let mut v = RationalVector::from_str("(1/2, -2/3, 0, -3)").unwrap();
103    /// v.entrywise_abs_assign();
104    /// assert_eq!(v.to_string(), "(1/2, 2/3, 0, 3)");
105    /// ```
106    fn entrywise_abs_assign(&mut self) {
107        for x in &mut self.elements {
108            x.abs_assign();
109        }
110    }
111}