malachite_nz/integer_polynomial/conversion/natural_polynomial_from_integer_polynomial.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_polynomial::IntegerPolynomial;
10use crate::natural::Natural;
11use crate::natural_polynomial::NaturalPolynomial;
12use alloc::vec::Vec;
13use malachite_base::num::conversion::traits::ConvertibleFrom;
14use malachite_base::polynomial::Polynomial;
15
16/// The error returned when an [`IntegerPolynomial`] with a negative coefficient is converted to a
17/// [`NaturalPolynomial`].
18#[derive(Clone, Copy, Debug, Eq, PartialEq)]
19pub struct NaturalPolynomialFromIntegerPolynomialError;
20
21impl TryFrom<IntegerPolynomial> for NaturalPolynomial {
22 type Error = NaturalPolynomialFromIntegerPolynomialError;
23
24 /// Converts an [`IntegerPolynomial`] to a [`NaturalPolynomial`], taking the
25 /// [`IntegerPolynomial`] by value and returning an error if any coefficient is negative.
26 ///
27 /// Each coefficient's limbs are reused rather than copied. A successful conversion keeps the
28 /// degree.
29 ///
30 /// # Worst-case complexity
31 /// $T(n) = O(n)$
32 ///
33 /// $M(n) = O(n)$
34 ///
35 /// where $T$ is time, $M$ is additional memory, and $n$ is the number of coefficients.
36 ///
37 /// # Examples
38 /// See [here](super::natural_polynomial_from_integer_polynomial#try_from).
39 fn try_from(p: IntegerPolynomial) -> Result<Self, Self::Error> {
40 Ok(Self::from_coefficients_asc(
41 p.coefficients
42 .into_iter()
43 .map(|c| {
44 Natural::try_from(c).map_err(|_| NaturalPolynomialFromIntegerPolynomialError)
45 })
46 .collect::<Result<Vec<Natural>, _>>()?,
47 ))
48 }
49}
50
51impl TryFrom<&IntegerPolynomial> for NaturalPolynomial {
52 type Error = NaturalPolynomialFromIntegerPolynomialError;
53
54 /// Converts an [`IntegerPolynomial`] to a [`NaturalPolynomial`], taking the
55 /// [`IntegerPolynomial`] by reference and returning an error if any coefficient is negative.
56 ///
57 /// A successful conversion keeps the degree.
58 ///
59 /// # Worst-case complexity
60 /// $T(n) = O(n)$
61 ///
62 /// $M(n) = O(n)$
63 ///
64 /// where $T$ is time, $M$ is additional memory, and $n$ is the total number of bits in the
65 /// coefficients.
66 ///
67 /// # Examples
68 /// See [here](super::natural_polynomial_from_integer_polynomial#try_from).
69 fn try_from(p: &IntegerPolynomial) -> Result<Self, Self::Error> {
70 Ok(Self::from_coefficients_asc(
71 p.coefficients
72 .iter()
73 .map(|c| {
74 Natural::try_from(c).map_err(|_| NaturalPolynomialFromIntegerPolynomialError)
75 })
76 .collect::<Result<Vec<Natural>, _>>()?,
77 ))
78 }
79}
80
81impl ConvertibleFrom<&IntegerPolynomial> for NaturalPolynomial {
82 /// Determines whether an [`IntegerPolynomial`] can be converted to a [`NaturalPolynomial`]
83 /// (when none of its coefficients is negative). Takes the [`IntegerPolynomial`] by reference.
84 ///
85 /// Unlike checking whether [`TryFrom`] succeeds, this allocates nothing.
86 ///
87 /// # Worst-case complexity
88 /// $T(n) = O(n)$
89 ///
90 /// $M(n) = O(1)$
91 ///
92 /// where $T$ is time, $M$ is additional memory, and $n$ is the number of coefficients.
93 ///
94 /// # Examples
95 /// See [here](super::natural_polynomial_from_integer_polynomial#convertible_from).
96 #[inline]
97 fn convertible_from(p: &IntegerPolynomial) -> bool {
98 p.coefficients.iter().all(Natural::convertible_from)
99 }
100}