1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright © 2026 Mikhail Hogrefe
//
// This file is part of Malachite.
//
// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
use crate::rational_polynomial::RationalPolynomial;
use core::ops::{Shl, ShlAssign, Shr, ShrAssign};
use malachite_base::num::arithmetic::traits::UnsignedAbs;
use malachite_base::num::basic::signeds::PrimitiveSigned;
use malachite_base::num::basic::traits::Zero;
use malachite_base::num::conversion::traits::ExactFrom;
use malachite_nz::integer_polynomial::IntegerPolynomial;
// Multiplies a polynomial by 2 to the power of `bits`. The denominator's factors of 2 are cancelled
// first, and only what is left shifts the numerator; since the numerator's content was coprime to
// the denominator, it stays coprime to what is left of it.
fn shl_assign_u64(p: &mut RationalPolynomial, bits: u64) {
if bits == 0 || p.numerator == IntegerPolynomial::ZERO {
return;
}
let denominator_zeros = p.denominator.trailing_zeros().unwrap();
if denominator_zeros >= bits {
p.denominator >>= bits;
} else {
p.denominator >>= denominator_zeros;
p.numerator <<= bits - denominator_zeros;
}
}
fn shl_ref_u64(p: &RationalPolynomial, bits: u64) -> RationalPolynomial {
if bits == 0 || p.numerator == IntegerPolynomial::ZERO {
return p.clone();
}
let denominator_zeros = p.denominator.trailing_zeros().unwrap();
if denominator_zeros >= bits {
RationalPolynomial {
numerator: p.numerator.clone(),
denominator: &p.denominator >> bits,
}
} else {
RationalPolynomial {
numerator: &p.numerator << (bits - denominator_zeros),
denominator: &p.denominator >> denominator_zeros,
}
}
}
fn shl_ref_signed<'a, U, S: PrimitiveSigned + UnsignedAbs<Output = U>>(
p: &'a RationalPolynomial,
bits: S,
) -> RationalPolynomial
where
&'a RationalPolynomial:
Shl<U, Output = RationalPolynomial> + Shr<U, Output = RationalPolynomial>,
{
if bits >= S::ZERO {
p << bits.unsigned_abs()
} else {
p >> bits.unsigned_abs()
}
}
fn shl_assign_signed<U, S: PrimitiveSigned + UnsignedAbs<Output = U>>(
p: &mut RationalPolynomial,
bits: S,
) where
RationalPolynomial: ShlAssign<U> + ShrAssign<U>,
{
if bits >= S::ZERO {
*p <<= bits.unsigned_abs();
} else {
*p >>= bits.unsigned_abs();
}
}
macro_rules! impl_rational_polynomial_shl_unsigned {
($t:ident) => {
impl Shl<$t> for RationalPolynomial {
type Output = RationalPolynomial;
/// Left-shifts a [`RationalPolynomial`] (multiplies it by a power of 2), taking it by
/// value.
///
/// $$
/// f(p, k) = 2^kp.
/// $$
///
/// Factors of 2 in the denominator are cancelled first, and only the rest of the power
/// shifts the numerator's coefficients, so the result stays in lowest terms.
///
/// # Worst-case complexity
/// $T(n, m, k) = O(n + km)$
///
/// $M(n, m, k) = O(n + km)$
///
/// where $T$ is time, $M$ is additional memory, $n$ is the total number of bits of the
/// numerator's coefficients and the denominator, $m$ is `bits`, and $k$ is
/// `self.len()`.
///
/// # Examples
/// See [here](super::shl#shl).
#[inline]
fn shl(mut self, bits: $t) -> RationalPolynomial {
self <<= bits;
self
}
}
impl Shl<$t> for &RationalPolynomial {
type Output = RationalPolynomial;
/// Left-shifts a [`RationalPolynomial`] (multiplies it by a power of 2), taking it by
/// reference.
///
/// $$
/// f(p, k) = 2^kp.
/// $$
///
/// Factors of 2 in the denominator are cancelled first, and only the rest of the power
/// shifts the numerator's coefficients, so the result stays in lowest terms.
///
/// # Worst-case complexity
/// $T(n, m, k) = O(n + km)$
///
/// $M(n, m, k) = O(n + km)$
///
/// where $T$ is time, $M$ is additional memory, $n$ is the total number of bits of the
/// numerator's coefficients and the denominator, $m$ is `bits`, and $k$ is
/// `self.len()`.
///
/// # Examples
/// See [here](super::shl#shl).
#[inline]
fn shl(self, bits: $t) -> RationalPolynomial {
shl_ref_u64(self, u64::exact_from(bits))
}
}
impl ShlAssign<$t> for RationalPolynomial {
/// Left-shifts a [`RationalPolynomial`] (multiplies it by a power of 2), in place.
///
/// $$
/// p \gets 2^kp.
/// $$
///
/// Factors of 2 in the denominator are cancelled first, and only the rest of the power
/// shifts the numerator's coefficients, so the result stays in lowest terms.
///
/// # Worst-case complexity
/// $T(n, m, k) = O(n + km)$
///
/// $M(n, m, k) = O(n + km)$
///
/// where $T$ is time, $M$ is additional memory, $n$ is the total number of bits of the
/// numerator's coefficients and the denominator, $m$ is `bits`, and $k$ is
/// `self.len()`.
///
/// # Examples
/// See [here](super::shl#shl_assign).
#[inline]
fn shl_assign(&mut self, bits: $t) {
shl_assign_u64(self, u64::exact_from(bits));
}
}
};
}
apply_to_unsigneds!(impl_rational_polynomial_shl_unsigned);
macro_rules! impl_rational_polynomial_shl_signed {
($t:ident) => {
impl Shl<$t> for RationalPolynomial {
type Output = RationalPolynomial;
/// Left-shifts a [`RationalPolynomial`] (multiplies it or divides it by a power of 2),
/// taking it by value.
///
/// $$
/// f(p, k) = 2^kp.
/// $$
///
/// A negative `bits` divides by $2^{-k}$: factors of 2 shared by the numerator's
/// coefficients are cancelled first, and only the rest of the power shifts the
/// denominator. A positive `bits` cancels factors of 2 in the denominator first. Either
/// way, the result stays in lowest terms.
///
/// # Worst-case complexity
/// $T(n, m, k) = O(n + km)$
///
/// $M(n, m, k) = O(n + km)$
///
/// where $T$ is time, $M$ is additional memory, $n$ is the total number of bits of the
/// numerator's coefficients and the denominator, $m$ is `bits.unsigned_abs()`, and $k$
/// is `self.len()`.
///
/// # Examples
/// See [here](super::shl#shl).
#[inline]
fn shl(mut self, bits: $t) -> RationalPolynomial {
self <<= bits;
self
}
}
impl Shl<$t> for &RationalPolynomial {
type Output = RationalPolynomial;
/// Left-shifts a [`RationalPolynomial`] (multiplies it or divides it by a power of 2),
/// taking it by reference.
///
/// $$
/// f(p, k) = 2^kp.
/// $$
///
/// A negative `bits` divides by $2^{-k}$: factors of 2 shared by the numerator's
/// coefficients are cancelled first, and only the rest of the power shifts the
/// denominator. A positive `bits` cancels factors of 2 in the denominator first. Either
/// way, the result stays in lowest terms.
///
/// # Worst-case complexity
/// $T(n, m, k) = O(n + km)$
///
/// $M(n, m, k) = O(n + km)$
///
/// where $T$ is time, $M$ is additional memory, $n$ is the total number of bits of the
/// numerator's coefficients and the denominator, $m$ is `bits.unsigned_abs()`, and $k$
/// is `self.len()`.
///
/// # Examples
/// See [here](super::shl#shl).
#[inline]
fn shl(self, bits: $t) -> RationalPolynomial {
shl_ref_signed(self, bits)
}
}
impl ShlAssign<$t> for RationalPolynomial {
/// Left-shifts a [`RationalPolynomial`] (multiplies it or divides it by a power of 2),
/// in place.
///
/// $$
/// p \gets 2^kp.
/// $$
///
/// A negative `bits` divides by $2^{-k}$: factors of 2 shared by the numerator's
/// coefficients are cancelled first, and only the rest of the power shifts the
/// denominator. A positive `bits` cancels factors of 2 in the denominator first. Either
/// way, the result stays in lowest terms.
///
/// # Worst-case complexity
/// $T(n, m, k) = O(n + km)$
///
/// $M(n, m, k) = O(n + km)$
///
/// where $T$ is time, $M$ is additional memory, $n$ is the total number of bits of the
/// numerator's coefficients and the denominator, $m$ is `bits.unsigned_abs()`, and $k$
/// is `self.len()`.
///
/// # Examples
/// See [here](super::shl#shl_assign).
#[inline]
fn shl_assign(&mut self, bits: $t) {
shl_assign_signed(self, bits);
}
}
};
}
apply_to_signeds!(impl_rational_polynomial_shl_signed);