malachite_base/unsigned_vector/arithmetic/mod_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::num::arithmetic::traits::ModElementProduct;
10use crate::num::basic::unsigneds::PrimitiveUnsigned;
11use crate::unsigned_vector::UnsignedVector;
12
13impl<T: PrimitiveUnsigned> ModElementProduct<T> for UnsignedVector<T> {
14 type Output = T;
15
16 /// Returns the product of the elements of an [`UnsignedVector`] modulo another number $m$,
17 /// taking the vector by reference. The elements must already be reduced.
18 ///
19 /// The 0-dimensional vector has no elements, and their product is 1, reduced: 0 when $m = 1$.
20 ///
21 /// $$
22 /// f(v, m) = \prod_i v_i \bmod m.
23 /// $$
24 ///
25 /// # Worst-case complexity
26 /// $T(n) = O(n)$
27 ///
28 /// $M(n) = O(1)$
29 ///
30 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.dimension()`.
31 ///
32 /// # Panics
33 /// Panics if `m` is zero, or if any element is greater than or equal to `m`. As soon as the
34 /// partial product is zero, the remaining elements are not examined, so an unreduced element
35 /// after that point is not detected.
36 ///
37 /// # Examples
38 /// ```
39 /// use core::str::FromStr;
40 /// use malachite_base::num::arithmetic::traits::ModElementProduct;
41 /// use malachite_base::unsigned_vector::UnsignedVector;
42 ///
43 /// let v = UnsignedVector::<u8>::from_str("(3, 5, 7)").unwrap();
44 /// assert_eq!(v.to_mod_element_product(10), 5);
45 /// let v = UnsignedVector::<u8>::from_str("()").unwrap();
46 /// assert_eq!(v.to_mod_element_product(10), 1);
47 /// ```
48 #[inline]
49 fn to_mod_element_product(&self, m: T) -> T {
50 T::mod_product(self.elements.iter(), m)
51 }
52
53 /// Returns the product of the elements of an [`UnsignedVector`] modulo another number $m$,
54 /// taking the vector by value. The elements must already be reduced.
55 ///
56 /// The 0-dimensional vector has no elements, and their product is 1, reduced: 0 when $m = 1$.
57 ///
58 /// $$
59 /// f(v, m) = \prod_i v_i \bmod m.
60 /// $$
61 ///
62 /// # Worst-case complexity
63 /// $T(n) = O(n)$
64 ///
65 /// $M(n) = O(1)$
66 ///
67 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.dimension()`.
68 ///
69 /// # Panics
70 /// Panics if `m` is zero, or if any element is greater than or equal to `m`. As soon as the
71 /// partial product is zero, the remaining elements are not examined, so an unreduced element
72 /// after that point is not detected.
73 ///
74 /// # Examples
75 /// ```
76 /// use core::str::FromStr;
77 /// use malachite_base::num::arithmetic::traits::ModElementProduct;
78 /// use malachite_base::unsigned_vector::UnsignedVector;
79 ///
80 /// let v = UnsignedVector::<u8>::from_str("(3, 5, 7)").unwrap();
81 /// assert_eq!(v.into_mod_element_product(10), 5);
82 /// let v = UnsignedVector::<u8>::from_str("()").unwrap();
83 /// assert_eq!(v.into_mod_element_product(10), 1);
84 /// ```
85 #[inline]
86 fn into_mod_element_product(self, m: T) -> T {
87 T::mod_product(self.elements.into_iter(), m)
88 }
89}