malachite_q/rational_vector/arithmetic/shr.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 core::ops::{Shr, ShrAssign};
12
13// Shifts every element right by `bits`.
14fn shr_ref<T: Copy>(v: &RationalVector, bits: T) -> RationalVector
15where
16 for<'a> &'a Rational: Shr<T, Output = Rational>,
17{
18 RationalVector {
19 elements: v.elements.iter().map(|x| x >> bits).collect(),
20 }
21}
22
23// Shifts every element right by `bits`, in place.
24fn shr_assign<T: Copy>(v: &mut RationalVector, bits: T)
25where
26 Rational: ShrAssign<T>,
27{
28 for x in &mut v.elements {
29 *x >>= bits;
30 }
31}
32
33macro_rules! impl_rational_vector_shr_unsigned {
34 ($t:ident) => {
35 impl Shr<$t> for RationalVector {
36 type Output = RationalVector;
37
38 /// Right-shifts a [`RationalVector`] (divides it by a power of 2), taking it by value.
39 /// Every element is shifted, and the dimension is unchanged.
40 ///
41 /// $f(v, k) = v/2^k$.
42 ///
43 /// # Worst-case complexity
44 /// $T(n, m, k) = O(n + km)$
45 ///
46 /// $M(n, m, k) = O(n + km)$
47 ///
48 /// where $T$ is time, $M$ is additional memory, $n$ is the total number of bits of the
49 /// elements, $m$ is `bits`, and $k$ is `self.dimension()`.
50 ///
51 /// # Examples
52 /// See [here](super::shr#shr).
53 #[inline]
54 fn shr(mut self, bits: $t) -> RationalVector {
55 self >>= bits;
56 self
57 }
58 }
59
60 impl Shr<$t> for &RationalVector {
61 type Output = RationalVector;
62
63 /// Right-shifts a [`RationalVector`] (divides it by a power of 2), taking it by
64 /// reference. Every element is shifted, and the dimension is unchanged.
65 ///
66 /// $f(v, k) = v/2^k$.
67 ///
68 /// # Worst-case complexity
69 /// $T(n, m, k) = O(n + km)$
70 ///
71 /// $M(n, m, k) = O(n + km)$
72 ///
73 /// where $T$ is time, $M$ is additional memory, $n$ is the total number of bits of the
74 /// elements, $m$ is `bits`, and $k$ is `self.dimension()`.
75 ///
76 /// # Examples
77 /// See [here](super::shr#shr).
78 #[inline]
79 fn shr(self, bits: $t) -> RationalVector {
80 shr_ref(self, bits)
81 }
82 }
83
84 impl ShrAssign<$t> for RationalVector {
85 /// Right-shifts a [`RationalVector`] (divides it by a power of 2), in place. Every
86 /// element is shifted, and the dimension is unchanged.
87 ///
88 /// $v \gets v/2^k$.
89 ///
90 /// # Worst-case complexity
91 /// $T(n, m, k) = O(n + km)$
92 ///
93 /// $M(n, m, k) = O(n + km)$
94 ///
95 /// where $T$ is time, $M$ is additional memory, $n$ is the total number of bits of the
96 /// elements, $m$ is `bits`, and $k$ is `self.dimension()`.
97 ///
98 /// # Examples
99 /// See [here](super::shr#shr_assign).
100 #[inline]
101 fn shr_assign(&mut self, bits: $t) {
102 shr_assign(self, bits);
103 }
104 }
105 };
106}
107apply_to_unsigneds!(impl_rational_vector_shr_unsigned);
108
109macro_rules! impl_rational_vector_shr_signed {
110 ($t:ident) => {
111 impl Shr<$t> for RationalVector {
112 type Output = RationalVector;
113
114 /// Right-shifts a [`RationalVector`] (divides it by a power of 2), taking it by value.
115 /// Every element is shifted, and the dimension is unchanged.
116 ///
117 /// $f(v, k) = v/2^k$.
118 ///
119 /// A negative `bits` shifts left, multiplying every element by $2^{-k}$.
120 ///
121 /// # Worst-case complexity
122 /// $T(n, m, k) = O(n + km)$
123 ///
124 /// $M(n, m, k) = O(n + km)$
125 ///
126 /// where $T$ is time, $M$ is additional memory, $n$ is the total number of bits of the
127 /// elements, $m$ is `bits.unsigned_abs()`, and $k$ is `self.dimension()`.
128 ///
129 /// # Examples
130 /// See [here](super::shr#shr).
131 #[inline]
132 fn shr(mut self, bits: $t) -> RationalVector {
133 self >>= bits;
134 self
135 }
136 }
137
138 impl Shr<$t> for &RationalVector {
139 type Output = RationalVector;
140
141 /// Right-shifts a [`RationalVector`] (divides it by a power of 2), taking it by
142 /// reference. Every element is shifted, and the dimension is unchanged.
143 ///
144 /// $f(v, k) = v/2^k$.
145 ///
146 /// A negative `bits` shifts left, multiplying every element by $2^{-k}$.
147 ///
148 /// # Worst-case complexity
149 /// $T(n, m, k) = O(n + km)$
150 ///
151 /// $M(n, m, k) = O(n + km)$
152 ///
153 /// where $T$ is time, $M$ is additional memory, $n$ is the total number of bits of the
154 /// elements, $m$ is `bits.unsigned_abs()`, and $k$ is `self.dimension()`.
155 ///
156 /// # Examples
157 /// See [here](super::shr#shr).
158 #[inline]
159 fn shr(self, bits: $t) -> RationalVector {
160 shr_ref(self, bits)
161 }
162 }
163
164 impl ShrAssign<$t> for RationalVector {
165 /// Right-shifts a [`RationalVector`] (divides it by a power of 2), in place. Every
166 /// element is shifted, and the dimension is unchanged.
167 ///
168 /// $v \gets v/2^k$.
169 ///
170 /// A negative `bits` shifts left, multiplying every element by $2^{-k}$.
171 ///
172 /// # Worst-case complexity
173 /// $T(n, m, k) = O(n + km)$
174 ///
175 /// $M(n, m, k) = O(n + km)$
176 ///
177 /// where $T$ is time, $M$ is additional memory, $n$ is the total number of bits of the
178 /// elements, $m$ is `bits.unsigned_abs()`, and $k$ is `self.dimension()`.
179 ///
180 /// # Examples
181 /// See [here](super::shr#shr_assign).
182 #[inline]
183 fn shr_assign(&mut self, bits: $t) {
184 shr_assign(self, bits);
185 }
186 }
187 };
188}
189apply_to_signeds!(impl_rational_vector_shr_signed);