malachite_nz/integer_vector/arithmetic/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::integer_vector::IntegerVector;
10use crate::integer_vector::arithmetic::dot_general::vec_dot_general;
11use crate::natural::Natural;
12use malachite_base::num::arithmetic::traits::AbsSquared;
13
14impl AbsSquared for IntegerVector {
15 type Output = Natural;
16
17 /// Computes the squared norm of an [`IntegerVector`], the sum of the squares of its elements,
18 /// taking the vector by value.
19 ///
20 /// This is the dot product of the vector with itself, $\|v\|^2$. The squared norm of the
21 /// 0-dimensional vector is 0. The squared norm is non-negative, so it is returned as a
22 /// [`Natural`].
23 ///
24 /// $$
25 /// f(v) = \|v\|^2 = \sum_{i=0}^{n-1} v_i^2,
26 /// $$
27 ///
28 /// where $n$ is the dimension of $v$.
29 ///
30 /// # Worst-case complexity
31 /// $T(n, m) = O(nm \log m \log\log m)$
32 ///
33 /// $M(m) = O(m \log m)$
34 ///
35 /// where $T$ is time, $M$ is additional memory, $n$ is the dimension of the vector, and $m$ is
36 /// the largest number of significant bits of any element.
37 ///
38 /// # Examples
39 /// ```
40 /// use core::str::FromStr;
41 /// use malachite_base::num::arithmetic::traits::AbsSquared;
42 /// use malachite_nz::integer_vector::IntegerVector;
43 ///
44 /// let v = IntegerVector::from_str("(3, -4, 12)").unwrap();
45 /// assert_eq!(v.abs_squared(), 169);
46 /// let v = IntegerVector::from_str("()").unwrap();
47 /// assert_eq!(v.abs_squared(), 0);
48 /// ```
49 ///
50 /// This is equivalent to `_fmpz_vec_dot` from `fmpz_vec/dot.c`, FLINT 3.6.0, with both vectors
51 /// the same, which squares each element instead of multiplying it by itself.
52 #[inline]
53 fn abs_squared(self) -> Natural {
54 vec_dot_general(None, false, &self.elements, &self.elements, false).abs
55 }
56}
57
58impl AbsSquared for &IntegerVector {
59 type Output = Natural;
60
61 /// Computes the squared norm of an [`IntegerVector`], the sum of the squares of its elements,
62 /// taking the vector by reference.
63 ///
64 /// This is the dot product of the vector with itself, $\|v\|^2$. The squared norm of the
65 /// 0-dimensional vector is 0. The squared norm is non-negative, so it is returned as a
66 /// [`Natural`].
67 ///
68 /// $$
69 /// f(v) = \|v\|^2 = \sum_{i=0}^{n-1} v_i^2,
70 /// $$
71 ///
72 /// where $n$ is the dimension of $v$.
73 ///
74 /// # Worst-case complexity
75 /// $T(n, m) = O(nm \log m \log\log m)$
76 ///
77 /// $M(m) = O(m \log m)$
78 ///
79 /// where $T$ is time, $M$ is additional memory, $n$ is the dimension of the vector, and $m$ is
80 /// the largest number of significant bits of any element.
81 ///
82 /// # Examples
83 /// ```
84 /// use core::str::FromStr;
85 /// use malachite_base::num::arithmetic::traits::AbsSquared;
86 /// use malachite_nz::integer_vector::IntegerVector;
87 ///
88 /// let v = IntegerVector::from_str("(3, -4, 12)").unwrap();
89 /// assert_eq!((&v).abs_squared(), 169);
90 /// let v = IntegerVector::from_str("()").unwrap();
91 /// assert_eq!((&v).abs_squared(), 0);
92 /// ```
93 ///
94 /// This is equivalent to `_fmpz_vec_dot` from `fmpz_vec/dot.c`, FLINT 3.6.0, with both vectors
95 /// the same, which squares each element instead of multiplying it by itself.
96 #[inline]
97 fn abs_squared(self) -> Natural {
98 vec_dot_general(None, false, &self.elements, &self.elements, false).abs
99 }
100}