Skip to main content

malachite_nz/integer_polynomial/conversion/
from_integer.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::integer::Integer;
10use crate::integer_polynomial::IntegerPolynomial;
11use alloc::vec;
12use malachite_base::polynomial::Polynomial;
13
14impl<T: Into<Integer>> From<T> for IntegerPolynomial {
15    /// Converts a value to a constant [`IntegerPolynomial`].
16    ///
17    /// This works for anything a [`Integer`] can be converted from, and for a [`Integer`] itself.
18    /// The polynomial is the constant one, whose only coefficient is the value; zero becomes the
19    /// zero polynomial, which has no coefficients at all.
20    ///
21    /// $f(x) = x$, read on the left as a number and on the right as a polynomial.
22    ///
23    /// # Worst-case complexity
24    /// Same as the time and additional memory complexity of converting the value to a [`Integer`].
25    ///
26    /// # Examples
27    /// ```
28    /// use malachite_base::num::arithmetic::traits::Pow;
29    /// use malachite_nz::integer::Integer;
30    /// use malachite_nz::integer_polynomial::IntegerPolynomial;
31    ///
32    /// assert_eq!(IntegerPolynomial::from(123u32).to_string(), "123");
33    /// assert_eq!(IntegerPolynomial::from(true).to_string(), "1");
34    /// assert_eq!(
35    ///     IntegerPolynomial::from(Integer::from(10u32).pow(20)).to_string(),
36    ///     "100000000000000000000"
37    /// );
38    ///
39    /// // Zero is the zero polynomial, which has no coefficients.
40    /// assert_eq!(IntegerPolynomial::from(0u32).to_string(), "0");
41    /// ```
42    #[inline]
43    fn from(x: T) -> Self {
44        Self::from_coefficients_asc(vec![x.into()])
45    }
46}