Skip to main content

malachite_base/unsigned_vector/arithmetic/
mod_abs_squared.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::{ModAbsSquared, ModDot};
10use crate::num::basic::unsigneds::PrimitiveUnsigned;
11use crate::unsigned_vector::UnsignedVector;
12
13impl<T: PrimitiveUnsigned> ModAbsSquared<T> for UnsignedVector<T> {
14    type Output = T;
15
16    /// Computes the squared norm of an [`UnsignedVector`], the sum of the squares of its elements,
17    /// modulo $m$, taking the vector by value. The elements must already be reduced.
18    ///
19    /// The squared norm of a 0-dimensional vector is 0.
20    ///
21    /// $$
22    /// f(v, m) = \sum_{i=0}^{n-1} v_i^2 \bmod m,
23    /// $$
24    ///
25    /// where $n$ is the dimension of $v$.
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 the dimension of the vector.
33    ///
34    /// # Panics
35    /// Panics if `m` is zero or if any element is greater than or equal to `m`.
36    ///
37    /// # Examples
38    /// ```
39    /// use core::str::FromStr;
40    /// use malachite_base::num::arithmetic::traits::ModAbsSquared;
41    /// use malachite_base::unsigned_vector::UnsignedVector;
42    ///
43    /// let v = UnsignedVector::<u8>::from_str("(1, 2, 3)").unwrap();
44    /// assert_eq!(v.clone().mod_abs_squared(10), 4);
45    /// ```
46    #[inline]
47    fn mod_abs_squared(self, m: T) -> T {
48        (&self).mod_dot(&self, m)
49    }
50}
51
52impl<T: PrimitiveUnsigned> ModAbsSquared<T> for &UnsignedVector<T> {
53    type Output = T;
54
55    /// Computes the squared norm of an [`UnsignedVector`], the sum of the squares of its elements,
56    /// modulo $m$, taking the vector by reference. The elements must already be reduced.
57    ///
58    /// The squared norm of a 0-dimensional vector is 0.
59    ///
60    /// $$
61    /// f(v, m) = \sum_{i=0}^{n-1} v_i^2 \bmod m,
62    /// $$
63    ///
64    /// where $n$ is the dimension of $v$.
65    ///
66    /// # Worst-case complexity
67    /// $T(n) = O(n)$
68    ///
69    /// $M(n) = O(1)$
70    ///
71    /// where $T$ is time, $M$ is additional memory, and $n$ is the dimension of the vector.
72    ///
73    /// # Panics
74    /// Panics if `m` is zero or if any element is greater than or equal to `m`.
75    ///
76    /// # Examples
77    /// ```
78    /// use core::str::FromStr;
79    /// use malachite_base::num::arithmetic::traits::ModAbsSquared;
80    /// use malachite_base::unsigned_vector::UnsignedVector;
81    ///
82    /// let v = UnsignedVector::<u8>::from_str("(1, 2, 3)").unwrap();
83    /// assert_eq!((&v).mod_abs_squared(10), 4);
84    /// ```
85    #[inline]
86    fn mod_abs_squared(self, m: T) -> T {
87        self.mod_dot(self, m)
88    }
89}