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
// 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::natural::Natural;
use crate::natural_vector::NaturalVector;
use malachite_base::num::arithmetic::traits::{
EntrywiseShlRound, EntrywiseShlRoundAssign, ShlRound, ShlRoundAssign,
};
use malachite_base::rounding_modes::RoundingMode;
// Shifts every element left by `bits`, rounding each according to `rm`.
fn entrywise_shl_round_ref<T: Copy>(v: &NaturalVector, bits: T, rm: RoundingMode) -> NaturalVector
where
for<'a> &'a Natural: ShlRound<T, Output = Natural>,
{
NaturalVector {
elements: v.elements.iter().map(|x| x.shl_round(bits, rm).0).collect(),
}
}
// Shifts every element left by `bits`, rounding each according to `rm`, in place.
fn entrywise_shl_round_assign<T: Copy>(v: &mut NaturalVector, bits: T, rm: RoundingMode)
where
Natural: ShlRoundAssign<T>,
{
for x in &mut v.elements {
x.shl_round_assign(bits, rm);
}
}
macro_rules! impl_natural_vector_entrywise_shl_round_signed {
($t:ident) => {
impl EntrywiseShlRound<$t> for NaturalVector {
type Output = NaturalVector;
/// Left-shifts a [`NaturalVector`] (multiplies or divides it by a power of 2), taking
/// it by value, and rounds every element according to the specified rounding mode.
///
/// Each element is rounded as by
/// [`ShlRound`](malachite_base::num::arithmetic::traits::ShlRound), but no
/// [`Ordering`](core::cmp::Ordering) is returned, since with `Nearest` different
/// elements may be rounded in different directions. Passing `Floor` or `Down` is
/// equivalent to using `<<`.
///
/// Rounding might only be necessary if `bits` is negative.
///
/// $f(v, k, r)_i = \operatorname{round}(2^kv_i)$, where $\operatorname{round}$ rounds
/// according to `rm`.
///
/// # 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
/// elements, $m$ is `bits.unsigned_abs()`, and $k$ is `self.dimension()`.
///
/// # Panics
/// Panics if `rm` is `Exact`, `bits` is negative, and some element is not divisible by
/// $2^{-k}$.
///
/// # Examples
/// See [here](super::entrywise_shl_round#entrywise_shl_round).
#[inline]
fn entrywise_shl_round(mut self, bits: $t, rm: RoundingMode) -> NaturalVector {
self.entrywise_shl_round_assign(bits, rm);
self
}
}
impl EntrywiseShlRound<$t> for &NaturalVector {
type Output = NaturalVector;
/// Left-shifts a [`NaturalVector`] (multiplies or divides it by a power of 2), taking
/// it by reference, and rounds every element according to the specified rounding mode.
///
/// Each element is rounded as by
/// [`ShlRound`](malachite_base::num::arithmetic::traits::ShlRound), but no
/// [`Ordering`](core::cmp::Ordering) is returned, since with `Nearest` different
/// elements may be rounded in different directions. Passing `Floor` or `Down` is
/// equivalent to using `<<`.
///
/// Rounding might only be necessary if `bits` is negative.
///
/// $f(v, k, r)_i = \operatorname{round}(2^kv_i)$, where $\operatorname{round}$ rounds
/// according to `rm`.
///
/// # 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
/// elements, $m$ is `bits.unsigned_abs()`, and $k$ is `self.dimension()`.
///
/// # Panics
/// Panics if `rm` is `Exact`, `bits` is negative, and some element is not divisible by
/// $2^{-k}$.
///
/// # Examples
/// See [here](super::entrywise_shl_round#entrywise_shl_round).
#[inline]
fn entrywise_shl_round(self, bits: $t, rm: RoundingMode) -> NaturalVector {
entrywise_shl_round_ref(self, bits, rm)
}
}
impl EntrywiseShlRoundAssign<$t> for NaturalVector {
/// Left-shifts a [`NaturalVector`] (multiplies or divides it by a power of 2), in
/// place, and rounds every element according to the specified rounding mode.
///
/// Each element is rounded as by
/// [`ShlRound`](malachite_base::num::arithmetic::traits::ShlRound), but no
/// [`Ordering`](core::cmp::Ordering) is returned, since with `Nearest` different
/// elements may be rounded in different directions. Passing `Floor` or `Down` is
/// equivalent to using `<<`.
///
/// Rounding might only be necessary if `bits` is negative.
///
/// $v_i \gets \operatorname{round}(2^kv_i)$, where $\operatorname{round}$ rounds
/// according to `rm`.
///
/// # 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
/// elements, $m$ is `bits.unsigned_abs()`, and $k$ is `self.dimension()`.
///
/// # Panics
/// Panics if `rm` is `Exact`, `bits` is negative, and some element is not divisible by
/// $2^{-k}$.
///
/// # Examples
/// See [here](super::entrywise_shl_round#entrywise_shl_round_assign).
#[inline]
fn entrywise_shl_round_assign(&mut self, bits: $t, rm: RoundingMode) {
entrywise_shl_round_assign(self, bits, rm);
}
}
};
}
apply_to_signeds!(impl_natural_vector_entrywise_shl_round_signed);