malachite_nz/integer_polynomial/arithmetic/shl.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::Integer;
10use crate::integer_polynomial::IntegerPolynomial;
11use core::ops::{Shl, ShlAssign};
12
13// Shifts every coefficient left by `bits`. A nonzero coefficient stays nonzero, and keeps its sign,
14// so the result is normalized.
15//
16// This is equivalent to `fmpz_poly_scalar_mul_2exp` from `fmpz_poly/scalar.c`, FLINT 3.6.0.
17fn shl_ref<T: Copy>(p: &IntegerPolynomial, bits: T) -> IntegerPolynomial
18where
19 for<'a> &'a Integer: Shl<T, Output = Integer>,
20{
21 IntegerPolynomial {
22 coefficients: p.coefficients.iter().map(|c| c << bits).collect(),
23 }
24}
25
26// Shifts every coefficient left by `bits`, in place.
27//
28// This is equivalent to `fmpz_poly_scalar_mul_2exp` from `fmpz_poly/scalar.c`, FLINT 3.6.0.
29fn shl_assign<T: Copy>(p: &mut IntegerPolynomial, bits: T)
30where
31 Integer: ShlAssign<T>,
32{
33 for c in &mut p.coefficients {
34 *c <<= bits;
35 }
36}
37
38macro_rules! impl_integer_polynomial_shl_unsigned {
39 ($t:ident) => {
40 impl Shl<$t> for IntegerPolynomial {
41 type Output = IntegerPolynomial;
42
43 /// Left-shifts an [`IntegerPolynomial`] (multiplies it by a power of 2), taking it by
44 /// value. Every coefficient is shifted.
45 ///
46 /// $f(p, k) = 2^kp$.
47 ///
48 /// # Worst-case complexity
49 /// $T(n, m, k) = O(n + km)$
50 ///
51 /// $M(n, m, k) = O(n + km)$
52 ///
53 /// where $T$ is time, $M$ is additional memory, $n$ is the total number of bits of the
54 /// coefficients, $m$ is `bits`, and $k$ is `self.len()`.
55 ///
56 /// # Examples
57 /// See [here](super::shl#shl).
58 #[inline]
59 fn shl(mut self, bits: $t) -> IntegerPolynomial {
60 self <<= bits;
61 self
62 }
63 }
64
65 impl Shl<$t> for &IntegerPolynomial {
66 type Output = IntegerPolynomial;
67
68 /// Left-shifts an [`IntegerPolynomial`] (multiplies it by a power of 2), taking it by
69 /// reference. Every coefficient is shifted.
70 ///
71 /// $f(p, k) = 2^kp$.
72 ///
73 /// # Worst-case complexity
74 /// $T(n, m, k) = O(n + km)$
75 ///
76 /// $M(n, m, k) = O(n + km)$
77 ///
78 /// where $T$ is time, $M$ is additional memory, $n$ is the total number of bits of the
79 /// coefficients, $m$ is `bits`, and $k$ is `self.len()`.
80 ///
81 /// # Examples
82 /// See [here](super::shl#shl).
83 #[inline]
84 fn shl(self, bits: $t) -> IntegerPolynomial {
85 shl_ref(self, bits)
86 }
87 }
88
89 impl ShlAssign<$t> for IntegerPolynomial {
90 /// Left-shifts an [`IntegerPolynomial`] (multiplies it by a power of 2), in place.
91 /// Every coefficient is shifted.
92 ///
93 /// $p \gets 2^kp$.
94 ///
95 /// # Worst-case complexity
96 /// $T(n, m, k) = O(n + km)$
97 ///
98 /// $M(n, m, k) = O(n + km)$
99 ///
100 /// where $T$ is time, $M$ is additional memory, $n$ is the total number of bits of the
101 /// coefficients, $m$ is `bits`, and $k$ is `self.len()`.
102 ///
103 /// # Examples
104 /// See [here](super::shl#shl_assign).
105 #[inline]
106 fn shl_assign(&mut self, bits: $t) {
107 shl_assign(self, bits);
108 }
109 }
110 };
111}
112apply_to_unsigneds!(impl_integer_polynomial_shl_unsigned);