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
// 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_vector::NaturalVector;
use core::borrow::Borrow;
use malachite_base::num::arithmetic::traits::{
ModPowerOf2Assign, ModPowerOf2IsReduced, ModPowerOf2Sum,
};
fn assert_reduced(v: &NaturalVector, pow: u64) {
assert!(
v.mod_power_of_2_is_reduced(pow),
"every vector must be reduced mod 2^pow, but {v} has an element >= 2^{pow}"
);
}
fn assert_same_dimension(v: &NaturalVector, w: &NaturalVector) {
assert_eq!(
v.elements.len(),
w.elements.len(),
"cannot sum vectors of different dimensions"
);
}
const EMPTY: &str = "cannot sum an empty iterator of vectors: the dimension is unknown";
// Adds the remaining vectors to `sum`, the first, entry by entry and exactly, and then reduces each
// entry once: the running sums exceed `pow` bits by only about the logarithm of the number of
// vectors, so this is cheaper than reducing after every addition.
fn finish<B: Borrow<NaturalVector>, I: Iterator<Item = B>>(
mut sum: NaturalVector,
xs: I,
pow: u64,
) -> NaturalVector {
assert_reduced(&sum, pow);
for v in xs {
let v = v.borrow();
assert_same_dimension(v, &sum);
assert_reduced(v, pow);
for (s, x) in sum.elements.iter_mut().zip(&v.elements) {
*s += x;
}
}
for s in &mut sum.elements {
s.mod_power_of_2_assign(pow);
}
sum
}
impl ModPowerOf2Sum for NaturalVector {
/// Adds up all the [`NaturalVector`]s in an iterator, entry by entry, modulo $2^k$. The vectors
/// must all have the same dimension, and their elements must already be reduced modulo $2^k$.
///
/// $f((v_i)_ {i=0}^{n-1}, k) = w$, where every element of $w$ is less than $2^k$ and $\sum_
/// {i=0}^{n-1} v_i \equiv w \mod 2^k$, entry by entry.
///
/// # Worst-case complexity
/// $T(n, k) = O(nk)$
///
/// $M(m, k) = O(mk)$
///
/// where $T$ is time, $M$ is additional memory, $n$ is the total number of elements of all the
/// vectors, $m$ is the dimension of the vectors, and $k$ is `pow`.
///
/// # Panics
/// Panics if the iterator is empty, if the vectors do not all have the same dimension, or if
/// any element is greater than or equal to $2^k$.
///
/// # Examples
/// ```
/// use core::str::FromStr;
/// use malachite_base::num::arithmetic::traits::ModPowerOf2Sum;
/// use malachite_nz::natural_vector::NaturalVector;
///
/// let vs =
/// ["(5, 1, 3)", "(2, 7, 4)", "(6, 6, 6)"].map(|s| NaturalVector::from_str(s).unwrap());
/// assert_eq!(
/// NaturalVector::mod_power_of_2_sum(vs.into_iter(), 3).to_string(),
/// "(5, 6, 5)"
/// );
///
/// let vs = [
/// "(18446744073709551615, 1)",
/// "(18446744073709551615, 18446744073709551616)",
/// "(3, 36893488147419103231)",
/// ]
/// .map(|s| NaturalVector::from_str(s).unwrap());
/// assert_eq!(
/// NaturalVector::mod_power_of_2_sum(vs.into_iter(), 65).to_string(),
/// "(1, 18446744073709551616)"
/// );
/// ```
fn mod_power_of_2_sum<I: Iterator<Item = Self>>(mut xs: I, pow: u64) -> Self {
let first = xs.next().expect(EMPTY);
finish(first, xs, pow)
}
}
impl<'a> ModPowerOf2Sum<&'a Self> for NaturalVector {
/// Adds up all the [`NaturalVector`]s in an iterator of references, entry by entry, modulo
/// $2^k$. The vectors must all have the same dimension, and their elements must already be
/// reduced modulo $2^k$.
///
/// $f((v_i)_ {i=0}^{n-1}, k) = w$, where every element of $w$ is less than $2^k$ and $\sum_
/// {i=0}^{n-1} v_i \equiv w \mod 2^k$, entry by entry.
///
/// # Worst-case complexity
/// $T(n, k) = O(nk)$
///
/// $M(m, k) = O(mk)$
///
/// where $T$ is time, $M$ is additional memory, $n$ is the total number of elements of all the
/// vectors, $m$ is the dimension of the vectors, and $k$ is `pow`.
///
/// # Panics
/// Panics if the iterator is empty, if the vectors do not all have the same dimension, or if
/// any element is greater than or equal to $2^k$.
///
/// # Examples
/// ```
/// use core::str::FromStr;
/// use malachite_base::num::arithmetic::traits::ModPowerOf2Sum;
/// use malachite_nz::natural_vector::NaturalVector;
///
/// let vs =
/// ["(5, 1, 3)", "(2, 7, 4)", "(6, 6, 6)"].map(|s| NaturalVector::from_str(s).unwrap());
/// assert_eq!(
/// NaturalVector::mod_power_of_2_sum(vs.iter(), 3).to_string(),
/// "(5, 6, 5)"
/// );
///
/// let vs = [
/// "(18446744073709551615, 1)",
/// "(18446744073709551615, 18446744073709551616)",
/// "(3, 36893488147419103231)",
/// ]
/// .map(|s| NaturalVector::from_str(s).unwrap());
/// assert_eq!(
/// NaturalVector::mod_power_of_2_sum(vs.iter(), 65).to_string(),
/// "(1, 18446744073709551616)"
/// );
/// ```
fn mod_power_of_2_sum<I: Iterator<Item = &'a Self>>(mut xs: I, pow: u64) -> Self {
let first = xs.next().expect(EMPTY);
finish(first.clone(), xs, pow)
}
}