Skip to main content

malachite_base/unsigned_polynomial/arithmetic/
is_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::IsUnit;
10use crate::num::basic::unsigneds::PrimitiveUnsigned;
11use crate::unsigned_polynomial::UnsignedPolynomial;
12
13impl<T: PrimitiveUnsigned> IsUnit for UnsignedPolynomial<T> {
14    /// Determines whether an [`UnsignedPolynomial`] is a unit: whether it is the constant
15    /// polynomial 1, the only polynomial with non-negative integer coefficients that has a
16    /// multiplicative inverse.
17    ///
18    /// No modulus is involved, as with [`IsUnit`] for the unsigned primitive types. Modulo $n$,
19    /// every constant coprime to $n$ is also a unit, and so, when $n$ is composite, are some
20    /// polynomials of positive degree.
21    ///
22    /// # Worst-case complexity
23    /// Constant time and additional memory.
24    ///
25    /// # Examples
26    /// ```
27    /// use core::str::FromStr;
28    /// use malachite_base::num::arithmetic::traits::IsUnit;
29    /// use malachite_base::num::basic::traits::Zero;
30    /// use malachite_base::polynomial::Polynomial;
31    /// use malachite_base::unsigned_polynomial::UnsignedPolynomial;
32    ///
33    /// assert_eq!(UnsignedPolynomial::<u64>::one().is_unit(), true);
34    /// assert_eq!(UnsignedPolynomial::<u64>::ZERO.is_unit(), false);
35    /// assert_eq!(UnsignedPolynomial::<u64>::two().is_unit(), false);
36    /// assert_eq!(UnsignedPolynomial::<u8>::x().is_unit(), false);
37    /// assert_eq!(
38    ///     UnsignedPolynomial::<u8>::from_str("x+1").unwrap().is_unit(),
39    ///     false
40    /// );
41    /// ```
42    #[inline]
43    fn is_unit(&self) -> bool {
44        *self == T::ONE
45    }
46}