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