Skip to main content

malachite_base/unsigned_polynomial/arithmetic/
canonicalize_unit.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::arithmetic::traits::{CanonicalizeUnit, CanonicalizeUnitAssign};
10use crate::num::basic::unsigneds::PrimitiveUnsigned;
11use crate::unsigned_polynomial::UnsignedPolynomial;
12
13impl<T: PrimitiveUnsigned> CanonicalizeUnit for UnsignedPolynomial<T> {
14    type Output = Self;
15
16    /// Brings an [`UnsignedPolynomial`] into canonical unit form, taking it by value.
17    ///
18    /// The coefficients are non-negative, so the leading coefficient already is, and the polynomial
19    /// is its own canonical associate: this is the identity, as it is for the coefficient type.
20    ///
21    /// # Worst-case complexity
22    /// Constant time and additional memory.
23    ///
24    /// # Examples
25    /// ```
26    /// use core::str::FromStr;
27    /// use malachite_base::num::arithmetic::traits::CanonicalizeUnit;
28    /// use malachite_base::num::basic::traits::Zero;
29    /// use malachite_base::unsigned_polynomial::UnsignedPolynomial;
30    ///
31    /// assert_eq!(
32    ///     UnsignedPolynomial::<u8>::from_str("3*x^2+2")
33    ///         .unwrap()
34    ///         .canonicalize_unit()
35    ///         .to_string(),
36    ///     "3*x^2+2"
37    /// );
38    /// assert_eq!(
39    ///     UnsignedPolynomial::<u8>::ZERO.canonicalize_unit(),
40    ///     UnsignedPolynomial::<u8>::ZERO
41    /// );
42    /// ```
43    #[inline]
44    fn canonicalize_unit(self) -> Self {
45        self
46    }
47}
48
49impl<T: PrimitiveUnsigned> CanonicalizeUnit for &UnsignedPolynomial<T> {
50    type Output = UnsignedPolynomial<T>;
51
52    /// Brings an [`UnsignedPolynomial`] into canonical unit form, taking it by reference.
53    ///
54    /// The coefficients are non-negative, so the leading coefficient already is, and the polynomial
55    /// is its own canonical associate: this is the identity, as it is for the coefficient type.
56    ///
57    /// # Worst-case complexity
58    /// $T(n) = O(n)$
59    ///
60    /// $M(n) = O(n)$
61    ///
62    /// where $T$ is time, $M$ is additional memory, and $n$ is the total size of the coefficients.
63    ///
64    /// # Examples
65    /// ```
66    /// use core::str::FromStr;
67    /// use malachite_base::num::arithmetic::traits::CanonicalizeUnit;
68    /// use malachite_base::num::basic::traits::Zero;
69    /// use malachite_base::unsigned_polynomial::UnsignedPolynomial;
70    ///
71    /// assert_eq!(
72    ///     (&UnsignedPolynomial::<u8>::from_str("3*x^2+2").unwrap())
73    ///         .canonicalize_unit()
74    ///         .to_string(),
75    ///     "3*x^2+2"
76    /// );
77    /// assert_eq!(
78    ///     (&UnsignedPolynomial::<u8>::ZERO).canonicalize_unit(),
79    ///     UnsignedPolynomial::<u8>::ZERO
80    /// );
81    /// ```
82    #[inline]
83    fn canonicalize_unit(self) -> UnsignedPolynomial<T> {
84        self.clone()
85    }
86}
87
88impl<T: PrimitiveUnsigned> CanonicalizeUnitAssign for UnsignedPolynomial<T> {
89    /// Brings an [`UnsignedPolynomial`] into canonical unit form, in place.
90    ///
91    /// See [`canonicalize_unit`](CanonicalizeUnit::canonicalize_unit).
92    ///
93    /// # Worst-case complexity
94    /// Constant time and additional memory.
95    ///
96    /// # Examples
97    /// ```
98    /// use core::str::FromStr;
99    /// use malachite_base::num::arithmetic::traits::CanonicalizeUnitAssign;
100    /// use malachite_base::unsigned_polynomial::UnsignedPolynomial;
101    ///
102    /// let mut p = UnsignedPolynomial::<u8>::from_str("3*x^2+2").unwrap();
103    /// p.canonicalize_unit_assign();
104    /// assert_eq!(p.to_string(), "3*x^2+2");
105    /// ```
106    #[inline]
107    fn canonicalize_unit_assign(&mut self) {}
108}