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
// 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::num::arithmetic::traits::{ModIsReduced, ModMulAssign, ModProduct};
use crate::num::basic::traits::Zero;
use crate::num::basic::unsigneds::PrimitiveUnsigned;
use crate::polynomial::Polynomial;
use crate::unsigned_polynomial::UnsignedPolynomial;
fn assert_reduced<T: PrimitiveUnsigned>(p: &UnsignedPolynomial<T>, m: T) {
assert!(
p.mod_is_reduced(&m),
"every polynomial must be reduced mod m, but {p} has a coefficient >= {m}"
);
}
// Multiplies the polynomials together one at a time. Once the partial product is zero it stays
// zero, so the factors after the one that made it zero are not examined; this happens more often
// than for numbers, since modulo a power of 2 or a composite number two nonzero polynomials can
// multiply to zero.
fn mod_product_owned<T: PrimitiveUnsigned, I: Iterator<Item = UnsignedPolynomial<T>>>(
xs: I,
m: T,
) -> UnsignedPolynomial<T> {
assert_ne!(m, T::ZERO, "m cannot be zero");
// The constant 1, reduced: the zero polynomial when the modulus is 1.
let mut product = if m == T::ONE {
UnsignedPolynomial::ZERO
} else {
UnsignedPolynomial::one()
};
for p in xs {
assert_reduced(&p, m);
product.mod_mul_assign(p, m);
if product == UnsignedPolynomial::ZERO {
break;
}
}
product
}
fn mod_product_ref<'a, T: PrimitiveUnsigned + 'a, I: Iterator<Item = &'a UnsignedPolynomial<T>>>(
xs: I,
m: T,
) -> UnsignedPolynomial<T> {
assert_ne!(m, T::ZERO, "m cannot be zero");
// The constant 1, reduced: the zero polynomial when the modulus is 1.
let mut product = if m == T::ONE {
UnsignedPolynomial::ZERO
} else {
UnsignedPolynomial::one()
};
for p in xs {
assert_reduced(p, m);
product.mod_mul_assign(p, m);
if product == UnsignedPolynomial::ZERO {
break;
}
}
product
}
impl<T: PrimitiveUnsigned> ModProduct<Self, T> for UnsignedPolynomial<T> {
/// Multiplies together all the [`UnsignedPolynomial`]s in an iterator modulo another number
/// $m$, taking the polynomials by value. The coefficients must already be reduced.
///
/// The product of no polynomials is the constant 1, reduced: the zero polynomial when $m = 1$.
///
/// $f((p_i)_ {i=0}^{n-1}) = q$, where every coefficient of $q$ is less than $m$ and $\prod_
/// {i=0}^{n-1} p_i \equiv q \mod m$, coefficient by coefficient.
///
/// # Worst-case complexity
/// $T(n) = O(n^2)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is the total number of coefficients of
/// all the polynomials.
///
/// # Panics
/// Panics if `m` is zero or if any coefficient is greater than or equal to `m`. As soon as the
/// partial product is zero, the remaining polynomials are not examined, so an unreduced
/// coefficient after that point is not detected.
///
/// # Examples
/// ```
/// use core::str::FromStr;
/// use malachite_base::num::arithmetic::traits::ModProduct;
/// use malachite_base::unsigned_polynomial::UnsignedPolynomial;
///
/// let ps = ["x+3", "2*x+1", "x^2+5"].map(|s| UnsignedPolynomial::<u8>::from_str(s).unwrap());
/// assert_eq!(
/// UnsignedPolynomial::mod_product(ps.into_iter(), 9).to_string(),
/// "2*x^4+7*x^3+4*x^2+8*x+6"
/// );
/// ```
#[inline]
fn mod_product<I: Iterator<Item = Self>>(xs: I, m: T) -> Self {
mod_product_owned(xs, m)
}
}
impl<'a, T: PrimitiveUnsigned> ModProduct<&'a Self, T> for UnsignedPolynomial<T> {
/// Multiplies together all the [`UnsignedPolynomial`]s in an iterator modulo another number
/// $m$, taking the polynomials by reference. The coefficients must already be reduced.
///
/// The product of no polynomials is the constant 1, reduced: the zero polynomial when $m = 1$.
///
/// $f((p_i)_ {i=0}^{n-1}) = q$, where every coefficient of $q$ is less than $m$ and $\prod_
/// {i=0}^{n-1} p_i \equiv q \mod m$, coefficient by coefficient.
///
/// # Worst-case complexity
/// $T(n) = O(n^2)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is the total number of coefficients of
/// all the polynomials.
///
/// # Panics
/// Panics if `m` is zero or if any coefficient is greater than or equal to `m`. As soon as the
/// partial product is zero, the remaining polynomials are not examined, so an unreduced
/// coefficient after that point is not detected.
///
/// # Examples
/// ```
/// use core::str::FromStr;
/// use malachite_base::num::arithmetic::traits::ModProduct;
/// use malachite_base::unsigned_polynomial::UnsignedPolynomial;
///
/// let ps = ["x+3", "2*x+1", "x^2+5"].map(|s| UnsignedPolynomial::<u8>::from_str(s).unwrap());
/// assert_eq!(
/// UnsignedPolynomial::mod_product(ps.iter(), 9).to_string(),
/// "2*x^4+7*x^3+4*x^2+8*x+6"
/// );
/// ```
#[inline]
fn mod_product<I: Iterator<Item = &'a Self>>(xs: I, m: T) -> Self {
mod_product_ref(xs, m)
}
}