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
// 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_polynomial::NaturalPolynomial;
use malachite_base::num::conversion::traits::PowerOf2Digits;
use malachite_base::polynomial::BitUnpack;
impl BitUnpack<Natural> for NaturalPolynomial {
/// Unpacks a [`NaturalPolynomial`] from the `bits`-bit fields of a [`Natural`], taking it by
/// value. The coefficients are the [`Natural`]'s digits in base $2^b$, so the result $p$
/// satisfies $p(2^b) = n$.
///
/// $$
/// f(n, b) = \sum_i \left ( \left \lfloor \frac{n}{2^{ib}} \right \rfloor \bmod 2^b \right )
/// x^i.
/// $$
///
/// This inverts [`bit_pack`](malachite_base::polynomial::BitPack::bit_pack) on polynomials
/// whose coefficients are less than $2^b$.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `n.significant_bits()`.
///
/// # Panics
/// Panics if `bits` is 0.
///
/// # Examples
/// ```
/// use malachite_base::num::basic::traits::Zero;
/// use malachite_base::polynomial::BitUnpack;
/// use malachite_nz::natural::Natural;
/// use malachite_nz::natural_polynomial::NaturalPolynomial;
///
/// assert_eq!(
/// NaturalPolynomial::bit_unpack(Natural::from(197121u32), 8).to_string(),
/// "3*x^2+2*x+1"
/// );
/// assert_eq!(
/// NaturalPolynomial::bit_unpack(Natural::ZERO, 8),
/// NaturalPolynomial::ZERO
/// );
/// // 257000 = 1000 * 2^8 + 1000 = 3 * 2^16 + 235 * 2^8 + 232
/// assert_eq!(
/// NaturalPolynomial::bit_unpack(Natural::from(257000u32), 8).to_string(),
/// "3*x^2+235*x+232"
/// );
/// ```
///
/// This is equivalent to `fmpz_poly_bit_unpack_unsigned` from `fmpz_poly/bit_unpack.c`, FLINT
/// 3.6.0, except that it panics when `bits` is 0, where FLINT gives the zero polynomial.
#[inline]
fn bit_unpack(n: Natural, bits: u64) -> Self {
Self::bit_unpack(&n, bits)
}
}
impl BitUnpack<&Natural> for NaturalPolynomial {
/// Unpacks a [`NaturalPolynomial`] from the `bits`-bit fields of a [`Natural`], taking it by
/// reference. The coefficients are the [`Natural`]'s digits in base $2^b$, so the result $p$
/// satisfies $p(2^b) = n$.
///
/// $$
/// f(n, b) = \sum_i \left ( \left \lfloor \frac{n}{2^{ib}} \right \rfloor \bmod 2^b \right )
/// x^i.
/// $$
///
/// This inverts [`bit_pack`](malachite_base::polynomial::BitPack::bit_pack) on polynomials
/// whose coefficients are less than $2^b$.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `n.significant_bits()`.
///
/// # Panics
/// Panics if `bits` is 0.
///
/// # Examples
/// ```
/// use malachite_base::num::basic::traits::Zero;
/// use malachite_base::polynomial::BitUnpack;
/// use malachite_nz::natural::Natural;
/// use malachite_nz::natural_polynomial::NaturalPolynomial;
///
/// assert_eq!(
/// NaturalPolynomial::bit_unpack(&Natural::from(197121u32), 8).to_string(),
/// "3*x^2+2*x+1"
/// );
/// assert_eq!(
/// NaturalPolynomial::bit_unpack(&Natural::ZERO, 8),
/// NaturalPolynomial::ZERO
/// );
/// // 257000 = 1000 * 2^8 + 1000 = 3 * 2^16 + 235 * 2^8 + 232
/// assert_eq!(
/// NaturalPolynomial::bit_unpack(&Natural::from(257000u32), 8).to_string(),
/// "3*x^2+235*x+232"
/// );
/// ```
///
/// This is equivalent to `fmpz_poly_bit_unpack_unsigned` from `fmpz_poly/bit_unpack.c`, FLINT
/// 3.6.0, except that it panics when `bits` is 0, where FLINT gives the zero polynomial.
fn bit_unpack(n: &Natural, bits: u64) -> Self {
assert_ne!(bits, 0, "Cannot unpack a polynomial from fields of 0 bits");
// The digits of a nonzero number have a nonzero last digit, so this is normalized.
Self {
coefficients: PowerOf2Digits::<Natural>::to_power_of_2_digits_asc(n, bits),
}
}
}