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
// 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::{Height, ModPowerOf2IsReduced};
use crate::num::basic::unsigneds::PrimitiveUnsigned;
use crate::unsigned_polynomial::UnsignedPolynomial;
impl<T: PrimitiveUnsigned> ModPowerOf2IsReduced for UnsignedPolynomial<T> {
/// Returns whether a [`UnsignedPolynomial`] is reduced modulo $2^k$; in other words, whether
/// every one of its coefficients has no more than $k$ significant bits.
///
/// Asking that of every coefficient is asking it of the largest, so this is the number of
/// significant bits of the polynomial's [`Height`](Height::to_height) — which bit length
/// being monotone means is the largest of the coefficients' bit lengths, so the height itself
/// never has to be built. The zero polynomial has no coefficients and is reduced modulo every
/// power of 2, including $2^0$.
///
/// $f(p, k) = (\max_i p_i < 2^k)$.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(1)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is the number of coefficients.
///
/// # Examples
/// ```
/// use core::str::FromStr;
/// use malachite_base::num::arithmetic::traits::ModPowerOf2IsReduced;
/// use malachite_base::unsigned_polynomial::UnsignedPolynomial;
///
/// // The largest coefficient is 3, which needs two bits.
/// let p = UnsignedPolynomial::<u64>::from_str("x^2+3*x+2").unwrap();
/// assert_eq!(p.mod_power_of_2_is_reduced(2), true);
/// assert_eq!(p.mod_power_of_2_is_reduced(1), false);
///
/// // The zero polynomial is reduced modulo every power of 2.
/// assert_eq!(
/// UnsignedPolynomial::<u64>::from_str("0")
/// .unwrap()
/// .mod_power_of_2_is_reduced(0),
/// true
/// );
/// ```
#[inline]
fn mod_power_of_2_is_reduced(&self, pow: u64) -> bool {
self.height_significant_bits() <= pow
}
}