Skip to main content

malachite_base/unsigned_vector/arithmetic/
scalar_div.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::basic::unsigneds::PrimitiveUnsigned;
10use crate::unsigned_vector::UnsignedVector;
11use core::ops::{Div, DivAssign};
12
13impl<T: PrimitiveUnsigned> Div<T> for UnsignedVector<T> {
14    type Output = Self;
15
16    /// Divides every element of an [`UnsignedVector`] by a `T`, taking it by value.
17    ///
18    /// The quotient is taken element by element, so the result has the same dimension as the
19    /// vector.
20    ///
21    /// Each quotient is rounded down.
22    ///
23    /// $$
24    /// f(v, c)_i = \left\lfloor\frac{v_i}{c}\right\rfloor.
25    /// $$
26    ///
27    /// # Worst-case complexity
28    /// $T(n) = O(n)$
29    ///
30    /// $M(n) = O(1)$
31    ///
32    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.dimension()`.
33    ///
34    /// # Panics
35    /// Panics if `c` is zero.
36    ///
37    /// # Examples
38    /// See [here](super::scalar_div#div).
39    #[inline]
40    fn div(mut self, c: T) -> Self {
41        self /= c;
42        self
43    }
44}
45
46impl<T: PrimitiveUnsigned> Div<T> for &UnsignedVector<T> {
47    type Output = UnsignedVector<T>;
48
49    /// Divides every element of an [`UnsignedVector`] by a `T`, taking it by reference.
50    ///
51    /// The quotient is taken element by element, so the result has the same dimension as the
52    /// vector.
53    ///
54    /// Each quotient is rounded down.
55    ///
56    /// $$
57    /// f(v, c)_i = \left\lfloor\frac{v_i}{c}\right\rfloor.
58    /// $$
59    ///
60    /// # Worst-case complexity
61    /// $T(n) = O(n)$
62    ///
63    /// $M(n) = O(n)$
64    ///
65    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.dimension()`.
66    ///
67    /// # Panics
68    /// Panics if `c` is zero.
69    ///
70    /// # Examples
71    /// See [here](super::scalar_div#div).
72    fn div(self, c: T) -> UnsignedVector<T> {
73        assert!(c != T::ZERO, "division by zero");
74        UnsignedVector {
75            elements: if c == T::ONE {
76                self.elements.clone()
77            } else {
78                self.elements.iter().map(|&x| x / c).collect()
79            },
80        }
81    }
82}
83
84impl<T: PrimitiveUnsigned> DivAssign<T> for UnsignedVector<T> {
85    /// Divides every element of an [`UnsignedVector`] by a `T`, in place.
86    ///
87    /// Every element is divided by `c`.
88    ///
89    /// # Worst-case complexity
90    /// $T(n) = O(n)$
91    ///
92    /// $M(n) = O(1)$
93    ///
94    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.dimension()`.
95    ///
96    /// # Panics
97    /// Panics if `c` is zero.
98    ///
99    /// # Examples
100    /// See [here](super::scalar_div#div_assign).
101    fn div_assign(&mut self, c: T) {
102        assert!(c != T::ZERO, "division by zero");
103        if c != T::ONE {
104            for x in &mut self.elements {
105                *x /= c;
106            }
107        }
108    }
109}