Skip to main content

malachite_q/rational_vector/arithmetic/
element_product.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::arithmetic::traits::ElementProduct;
12
13impl ElementProduct for RationalVector {
14    type Output = Rational;
15
16    /// Returns the product of the elements of a [`RationalVector`], taking the vector by reference.
17    ///
18    /// The 0-dimensional vector has no elements, and their product is 1. The elements are
19    /// multiplied in a balanced tree, so that the operands of each multiplication have similar
20    /// sizes, and multiplication stops as soon as a zero element is found.
21    ///
22    /// $$
23    /// f(v) = \prod_i v_i.
24    /// $$
25    ///
26    /// # Worst-case complexity
27    /// $T(n) = O(n (\log n)^2 \log\log n)$
28    ///
29    /// $M(n) = O(n \log n)$
30    ///
31    /// where $T$ is time, $M$ is additional memory, and $n$ is the total number of bits of the
32    /// elements.
33    ///
34    /// # Examples
35    /// ```
36    /// use core::str::FromStr;
37    /// use malachite_base::num::arithmetic::traits::ElementProduct;
38    /// use malachite_q::rational_vector::RationalVector;
39    ///
40    /// assert_eq!(
41    ///     RationalVector::from_str("(1/2, -2/3)")
42    ///         .unwrap()
43    ///         .to_element_product()
44    ///         .to_string(),
45    ///     "-1/3"
46    /// );
47    /// assert_eq!(
48    ///     RationalVector::from_str("()").unwrap().to_element_product(),
49    ///     1
50    /// );
51    /// ```
52    #[inline]
53    fn to_element_product(&self) -> Rational {
54        self.elements.iter().product()
55    }
56
57    /// Returns the product of the elements of a [`RationalVector`], taking the vector by value.
58    ///
59    /// The 0-dimensional vector has no elements, and their product is 1. The elements are
60    /// multiplied in a balanced tree, so that the operands of each multiplication have similar
61    /// sizes, and multiplication stops as soon as a zero element is found.
62    ///
63    /// $$
64    /// f(v) = \prod_i v_i.
65    /// $$
66    ///
67    /// # Worst-case complexity
68    /// $T(n) = O(n (\log n)^2 \log\log n)$
69    ///
70    /// $M(n) = O(n \log n)$
71    ///
72    /// where $T$ is time, $M$ is additional memory, and $n$ is the total number of bits of the
73    /// elements.
74    ///
75    /// # Examples
76    /// ```
77    /// use core::str::FromStr;
78    /// use malachite_base::num::arithmetic::traits::ElementProduct;
79    /// use malachite_q::rational_vector::RationalVector;
80    ///
81    /// assert_eq!(
82    ///     RationalVector::from_str("(1/2, -2/3)")
83    ///         .unwrap()
84    ///         .into_element_product()
85    ///         .to_string(),
86    ///     "-1/3"
87    /// );
88    /// assert_eq!(
89    ///     RationalVector::from_str("()")
90    ///         .unwrap()
91    ///         .into_element_product(),
92    ///     1
93    /// );
94    /// ```
95    #[inline]
96    fn into_element_product(self) -> Rational {
97        self.elements.into_iter().product()
98    }
99}