malachite_base/unsigned_polynomial/arithmetic/content.rs
1// Copyright © 2026 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::num::basic::unsigneds::PrimitiveUnsigned;
10use crate::polynomial::{Content, ContentAndPrimitivePart, PrimitivePart, PrimitivePartAssign};
11use crate::unsigned_polynomial::UnsignedPolynomial;
12
13// The GCD of the coefficients. It stops as soon as it reaches 1, since nothing can lower it
14// further.
15fn content<T: PrimitiveUnsigned>(coefficients: &[T]) -> T {
16 let mut gcd = T::ZERO;
17 for &c in coefficients {
18 gcd.gcd_assign(c);
19 if gcd == T::ONE {
20 break;
21 }
22 }
23 gcd
24}
25
26// Divides every coefficient by the content, which divides each of them exactly.
27fn divide_by_content<T: PrimitiveUnsigned>(coefficients: &mut [T], content: T) {
28 if content > T::ONE {
29 for c in coefficients {
30 c.div_exact_assign(content);
31 }
32 }
33}
34
35impl<T: PrimitiveUnsigned> Content for UnsignedPolynomial<T> {
36 type Output = T;
37
38 /// Computes the content of an [`UnsignedPolynomial`], the GCD of its coefficients, taking the
39 /// polynomial by value.
40 ///
41 /// The content of the zero polynomial is zero. The GCD is taken coefficient by coefficient,
42 /// stopping early once it reaches 1.
43 ///
44 /// $$
45 /// f(p) = \gcd(c_0, c_1, \ldots, c_{n-1}),
46 /// $$
47 ///
48 /// where $c_i$ is the coefficient of $x^i$ in $p$ and $n$ is its length.
49 ///
50 /// # Worst-case complexity
51 /// $T(n) = O(n)$
52 ///
53 /// $M(n) = O(1)$
54 ///
55 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.len()`.
56 ///
57 /// # Examples
58 /// ```
59 /// use core::str::FromStr;
60 /// use malachite_base::num::basic::traits::Zero;
61 /// use malachite_base::polynomial::Content;
62 /// use malachite_base::unsigned_polynomial::UnsignedPolynomial;
63 ///
64 /// let p = UnsignedPolynomial::<u8>::from_str("6*x^2+4*x+10").unwrap();
65 /// assert_eq!(p.clone().content(), 2);
66 /// assert_eq!(UnsignedPolynomial::<u8>::ZERO.content(), 0);
67 /// ```
68 ///
69 /// This is equivalent to `fmpz_poly_content` from `fmpz_poly/content.c`, FLINT 3.6.0.
70 #[inline]
71 fn content(self) -> T {
72 content(&self.coefficients)
73 }
74}
75
76impl<T: PrimitiveUnsigned> Content for &UnsignedPolynomial<T> {
77 type Output = T;
78
79 /// Computes the content of an [`UnsignedPolynomial`], the GCD of its coefficients, taking the
80 /// polynomial by reference.
81 ///
82 /// The content of the zero polynomial is zero. The GCD is taken coefficient by coefficient,
83 /// stopping early once it reaches 1.
84 ///
85 /// $$
86 /// f(p) = \gcd(c_0, c_1, \ldots, c_{n-1}),
87 /// $$
88 ///
89 /// where $c_i$ is the coefficient of $x^i$ in $p$ and $n$ is its length.
90 ///
91 /// # Worst-case complexity
92 /// $T(n) = O(n)$
93 ///
94 /// $M(n) = O(1)$
95 ///
96 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.len()`.
97 ///
98 /// # Examples
99 /// ```
100 /// use core::str::FromStr;
101 /// use malachite_base::num::basic::traits::Zero;
102 /// use malachite_base::polynomial::Content;
103 /// use malachite_base::unsigned_polynomial::UnsignedPolynomial;
104 ///
105 /// let p = UnsignedPolynomial::<u8>::from_str("6*x^2+4*x+10").unwrap();
106 /// assert_eq!((&p).content(), 2);
107 /// assert_eq!((&UnsignedPolynomial::<u8>::ZERO).content(), 0);
108 /// ```
109 ///
110 /// This is equivalent to `fmpz_poly_content` from `fmpz_poly/content.c`, FLINT 3.6.0.
111 #[inline]
112 fn content(self) -> T {
113 content(&self.coefficients)
114 }
115}
116
117impl<T: PrimitiveUnsigned> PrimitivePart for UnsignedPolynomial<T> {
118 type Output = Self;
119
120 /// Computes the primitive part of an [`UnsignedPolynomial`], the polynomial divided by its
121 /// content, taking the polynomial by value.
122 ///
123 /// The coefficients are non-negative, so no sign needs normalizing and $p =
124 /// \operatorname{cont}(p) \operatorname{pp}(p)$. The primitive part of the zero polynomial is
125 /// zero.
126 ///
127 /// # Worst-case complexity
128 /// $T(n) = O(n)$
129 ///
130 /// $M(n) = O(1)$
131 ///
132 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.len()`.
133 ///
134 /// # Examples
135 /// ```
136 /// use core::str::FromStr;
137 /// use malachite_base::num::basic::traits::Zero;
138 /// use malachite_base::polynomial::PrimitivePart;
139 /// use malachite_base::unsigned_polynomial::UnsignedPolynomial;
140 ///
141 /// let p = UnsignedPolynomial::<u8>::from_str("6*x^2+4*x+10").unwrap();
142 /// assert_eq!(p.clone().primitive_part().to_string(), "3*x^2+2*x+5");
143 /// assert_eq!(
144 /// UnsignedPolynomial::<u8>::ZERO.primitive_part(),
145 /// UnsignedPolynomial::<u8>::ZERO
146 /// );
147 /// ```
148 ///
149 /// This is equivalent to `fmpz_poly_primitive_part` from `fmpz_poly/primitive_part.c`, FLINT
150 /// 3.6.0.
151 #[inline]
152 fn primitive_part(mut self) -> Self {
153 let content = content(&self.coefficients);
154 divide_by_content(&mut self.coefficients, content);
155 self
156 }
157}
158
159impl<T: PrimitiveUnsigned> PrimitivePart for &UnsignedPolynomial<T> {
160 type Output = UnsignedPolynomial<T>;
161
162 /// Computes the primitive part of an [`UnsignedPolynomial`], the polynomial divided by its
163 /// content, taking the polynomial by reference.
164 ///
165 /// The coefficients are non-negative, so no sign needs normalizing and $p =
166 /// \operatorname{cont}(p) \operatorname{pp}(p)$. The primitive part of the zero polynomial is
167 /// zero.
168 ///
169 /// # Worst-case complexity
170 /// $T(n) = O(n)$
171 ///
172 /// $M(n) = O(n)$
173 ///
174 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.len()`.
175 ///
176 /// # Examples
177 /// ```
178 /// use core::str::FromStr;
179 /// use malachite_base::num::basic::traits::Zero;
180 /// use malachite_base::polynomial::PrimitivePart;
181 /// use malachite_base::unsigned_polynomial::UnsignedPolynomial;
182 ///
183 /// let p = UnsignedPolynomial::<u8>::from_str("6*x^2+4*x+10").unwrap();
184 /// assert_eq!((&p).primitive_part().to_string(), "3*x^2+2*x+5");
185 /// assert_eq!(
186 /// (&UnsignedPolynomial::<u8>::ZERO).primitive_part(),
187 /// UnsignedPolynomial::<u8>::ZERO
188 /// );
189 /// ```
190 ///
191 /// This is equivalent to `fmpz_poly_primitive_part` from `fmpz_poly/primitive_part.c`, FLINT
192 /// 3.6.0.
193 #[inline]
194 fn primitive_part(self) -> UnsignedPolynomial<T> {
195 let content = content(&self.coefficients);
196 let mut coefficients = self.coefficients.clone();
197 divide_by_content(&mut coefficients, content);
198 UnsignedPolynomial { coefficients }
199 }
200}
201
202impl<T: PrimitiveUnsigned> PrimitivePartAssign for UnsignedPolynomial<T> {
203 /// Replaces an [`UnsignedPolynomial`] with its primitive part, the polynomial divided by its
204 /// content.
205 ///
206 /// See [`primitive_part`](PrimitivePart::primitive_part).
207 ///
208 /// # Worst-case complexity
209 /// $T(n) = O(n)$
210 ///
211 /// $M(n) = O(1)$
212 ///
213 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.len()`.
214 ///
215 /// # Examples
216 /// ```
217 /// use core::str::FromStr;
218 /// use malachite_base::polynomial::PrimitivePartAssign;
219 /// use malachite_base::unsigned_polynomial::UnsignedPolynomial;
220 ///
221 /// let mut p = UnsignedPolynomial::<u8>::from_str("6*x^2+4*x+10").unwrap();
222 /// p.primitive_part_assign();
223 /// assert_eq!(p.to_string(), "3*x^2+2*x+5");
224 /// ```
225 #[inline]
226 fn primitive_part_assign(&mut self) {
227 let content = content(&self.coefficients);
228 divide_by_content(&mut self.coefficients, content);
229 }
230}
231
232impl<T: PrimitiveUnsigned> ContentAndPrimitivePart for UnsignedPolynomial<T> {
233 type Content = T;
234 type PrimitivePart = Self;
235
236 /// Computes the content and the primitive part of an [`UnsignedPolynomial`] together, taking
237 /// the polynomial by value.
238 ///
239 /// See [`content`](Content::content) and [`primitive_part`](PrimitivePart::primitive_part); the
240 /// content is found once rather than twice.
241 ///
242 /// # Worst-case complexity
243 /// $T(n) = O(n)$
244 ///
245 /// $M(n) = O(1)$
246 ///
247 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.len()`.
248 ///
249 /// # Examples
250 /// ```
251 /// use core::str::FromStr;
252 /// use malachite_base::polynomial::ContentAndPrimitivePart;
253 /// use malachite_base::unsigned_polynomial::UnsignedPolynomial;
254 ///
255 /// let p = UnsignedPolynomial::<u8>::from_str("6*x^2+4*x+10").unwrap();
256 /// let (content, primitive_part) = p.clone().content_and_primitive_part();
257 /// assert_eq!(content, 2);
258 /// assert_eq!(primitive_part.to_string(), "3*x^2+2*x+5");
259 /// ```
260 #[inline]
261 fn content_and_primitive_part(mut self) -> (T, Self) {
262 let content = content(&self.coefficients);
263 divide_by_content(&mut self.coefficients, content);
264 (content, self)
265 }
266}
267
268impl<T: PrimitiveUnsigned> ContentAndPrimitivePart for &UnsignedPolynomial<T> {
269 type Content = T;
270 type PrimitivePart = UnsignedPolynomial<T>;
271
272 /// Computes the content and the primitive part of an [`UnsignedPolynomial`] together, taking
273 /// the polynomial by reference.
274 ///
275 /// See [`content`](Content::content) and [`primitive_part`](PrimitivePart::primitive_part); the
276 /// content is found once rather than twice.
277 ///
278 /// # Worst-case complexity
279 /// $T(n) = O(n)$
280 ///
281 /// $M(n) = O(n)$
282 ///
283 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.len()`.
284 ///
285 /// # Examples
286 /// ```
287 /// use core::str::FromStr;
288 /// use malachite_base::polynomial::ContentAndPrimitivePart;
289 /// use malachite_base::unsigned_polynomial::UnsignedPolynomial;
290 ///
291 /// let p = UnsignedPolynomial::<u8>::from_str("6*x^2+4*x+10").unwrap();
292 /// let (content, primitive_part) = (&p).content_and_primitive_part();
293 /// assert_eq!(content, 2);
294 /// assert_eq!(primitive_part.to_string(), "3*x^2+2*x+5");
295 /// ```
296 #[inline]
297 fn content_and_primitive_part(self) -> (T, UnsignedPolynomial<T>) {
298 let content = content(&self.coefficients);
299 let mut coefficients = self.coefficients.clone();
300 divide_by_content(&mut coefficients, content);
301 (content, UnsignedPolynomial { coefficients })
302 }
303}