Skip to main content

malachite_q/rational_vector/conversion/
from_natural_vector.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::Rational;
10use crate::rational_vector::RationalVector;
11use malachite_nz::natural_vector::NaturalVector;
12
13impl From<NaturalVector> for RationalVector {
14    /// Converts a [`NaturalVector`] to a [`RationalVector`].
15    ///
16    /// Every [`Natural`](malachite_nz::natural::Natural) is a [`Rational`], so nothing is lost and
17    /// nothing can fail. The elements are converted one by one, each with denominator 1, so the
18    /// dimension is unchanged.
19    ///
20    /// # Worst-case complexity
21    /// $T(n) = O(n)$
22    ///
23    /// $M(n) = O(n)$
24    ///
25    /// where $T$ is time, $M$ is additional memory, and $n$ is `v.dimension()`.
26    ///
27    /// # Examples
28    /// ```
29    /// use core::str::FromStr;
30    /// use malachite_nz::natural_vector::NaturalVector;
31    /// use malachite_q::rational_vector::RationalVector;
32    ///
33    /// let v = NaturalVector::from_str("(1, 2, 3)").unwrap();
34    /// assert_eq!(RationalVector::from(v).to_string(), "(1, 2, 3)");
35    ///
36    /// assert_eq!(
37    ///     RationalVector::from(NaturalVector::from_str("()").unwrap()).to_string(),
38    ///     "()"
39    /// );
40    /// ```
41    #[inline]
42    fn from(v: NaturalVector) -> Self {
43        Self {
44            elements: v.elements.into_iter().map(Rational::from).collect(),
45        }
46    }
47}