malachite_q/rational_vector/arithmetic/l1_norm.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;
10use crate::rational_vector::RationalVector;
11use malachite_base::num::arithmetic::traits::{Abs, L1Norm};
12use malachite_base::num::logic::traits::SignificantBits;
13
14impl L1Norm for RationalVector {
15 type Output = Rational;
16
17 /// Returns the $\ell^1$ norm of a [`RationalVector`]: the sum of the absolute values of its
18 /// elements, taking the vector by reference.
19 ///
20 /// The 0-dimensional vector has no elements, and its norm is 0.
21 ///
22 /// $$
23 /// f(v) = \|v\|_1 = \sum_i |v_i|.
24 /// $$
25 ///
26 /// # Worst-case complexity
27 /// $T(n) = O(n)$
28 ///
29 /// $M(n) = O(n)$
30 ///
31 /// where $T$ is time, $M$ is additional memory, and $n$ is the total number of bits of the
32 /// elements.
33 ///
34 /// # Examples
35 /// ```
36 /// use core::str::FromStr;
37 /// use malachite_base::num::arithmetic::traits::L1Norm;
38 /// use malachite_q::rational_vector::RationalVector;
39 ///
40 /// assert_eq!(
41 /// RationalVector::from_str("(1/2, -1/3)")
42 /// .unwrap()
43 /// .to_l1_norm()
44 /// .to_string(),
45 /// "5/6"
46 /// );
47 /// assert_eq!(RationalVector::from_str("()").unwrap().to_l1_norm(), 0);
48 /// ```
49 #[inline]
50 fn to_l1_norm(&self) -> Rational {
51 self.elements.iter().map(Abs::abs).sum()
52 }
53
54 /// Returns the $\ell^1$ norm of a [`RationalVector`]: the sum of the absolute values of its
55 /// elements, taking the vector by value.
56 ///
57 /// # Worst-case complexity
58 /// $T(n) = O(n)$
59 ///
60 /// $M(n) = O(n)$
61 ///
62 /// where $T$ is time, $M$ is additional memory, and $n$ is the total number of bits of the
63 /// elements.
64 ///
65 /// # Examples
66 /// ```
67 /// use core::str::FromStr;
68 /// use malachite_base::num::arithmetic::traits::L1Norm;
69 /// use malachite_q::rational_vector::RationalVector;
70 ///
71 /// assert_eq!(
72 /// RationalVector::from_str("(1/2, -1/3)")
73 /// .unwrap()
74 /// .into_l1_norm()
75 /// .to_string(),
76 /// "5/6"
77 /// );
78 /// assert_eq!(RationalVector::from_str("()").unwrap().into_l1_norm(), 0);
79 /// ```
80 #[inline]
81 fn into_l1_norm(self) -> Rational {
82 self.elements.into_iter().map(Abs::abs).sum()
83 }
84
85 /// Returns the number of significant bits of the $\ell^1$ norm of a [`RationalVector`].
86 ///
87 /// As for a single [`Rational`], the count is the sum of the numbers of significant bits of the
88 /// norm's numerator and denominator. So a norm of 0, which is $0/1$, has 1 significant bit,
89 /// even for the 0-dimensional vector.
90 ///
91 /// # Worst-case complexity
92 /// $T(n) = O(n)$
93 ///
94 /// $M(n) = O(n)$
95 ///
96 /// where $T$ is time, $M$ is additional memory, and $n$ is the total number of bits of the
97 /// elements.
98 ///
99 /// # Examples
100 /// ```
101 /// use core::str::FromStr;
102 /// use malachite_base::num::arithmetic::traits::L1Norm;
103 /// use malachite_q::rational_vector::RationalVector;
104 ///
105 /// assert_eq!(
106 /// RationalVector::from_str("(1/2, -1/3)")
107 /// .unwrap()
108 /// .l1_norm_significant_bits(),
109 /// 6
110 /// );
111 /// assert_eq!(
112 /// RationalVector::from_str("()")
113 /// .unwrap()
114 /// .l1_norm_significant_bits(),
115 /// 1
116 /// );
117 /// ```
118 #[inline]
119 fn l1_norm_significant_bits(&self) -> u64 {
120 self.to_l1_norm().significant_bits()
121 }
122}