Skip to main content

malachite_nz/integer/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::Integer;
10use malachite_base::num::arithmetic::traits::{AbsSquared, AbsSquaredAssign, Square, SquareAssign};
11
12impl AbsSquared for Integer {
13    type Output = Self;
14
15    /// Computes the squared absolute value of a [`Integer`], taking it by value. For real types
16    /// this is the same as squaring.
17    ///
18    /// $$
19    /// f(x) = |x|^2 = x^2.
20    /// $$
21    ///
22    /// # Worst-case complexity
23    /// $T(n) = O(n \log n \log\log n)$
24    ///
25    /// $M(n) = O(n \log n)$
26    ///
27    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
28    ///
29    /// # Examples
30    /// ```
31    /// use malachite_base::num::arithmetic::traits::AbsSquared;
32    /// use malachite_base::num::basic::traits::Zero;
33    /// use malachite_nz::integer::Integer;
34    ///
35    /// assert_eq!(Integer::ZERO.abs_squared(), 0);
36    /// assert_eq!(Integer::from(123).abs_squared(), 15129);
37    /// assert_eq!(Integer::from(-123).abs_squared(), 15129);
38    /// ```
39    #[inline]
40    fn abs_squared(self) -> Self {
41        self.square()
42    }
43}
44
45impl AbsSquared for &Integer {
46    type Output = Integer;
47
48    /// Computes the squared absolute value of a [`Integer`], taking it by reference. For real types
49    /// this is the same as squaring.
50    ///
51    /// $$
52    /// f(x) = |x|^2 = x^2.
53    /// $$
54    ///
55    /// # Worst-case complexity
56    /// $T(n) = O(n \log n \log\log n)$
57    ///
58    /// $M(n) = O(n \log n)$
59    ///
60    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
61    ///
62    /// # Examples
63    /// ```
64    /// use malachite_base::num::arithmetic::traits::AbsSquared;
65    /// use malachite_base::num::basic::traits::Zero;
66    /// use malachite_nz::integer::Integer;
67    ///
68    /// assert_eq!((&Integer::ZERO).abs_squared(), 0);
69    /// assert_eq!((&Integer::from(123)).abs_squared(), 15129);
70    /// assert_eq!((&Integer::from(-123)).abs_squared(), 15129);
71    /// ```
72    #[inline]
73    fn abs_squared(self) -> Integer {
74        self.square()
75    }
76}
77
78impl AbsSquaredAssign for Integer {
79    /// Replaces a [`Integer`] with its squared absolute value. For real types this is the same as
80    /// squaring in place.
81    ///
82    /// $$
83    /// x \gets |x|^2 = x^2.
84    /// $$
85    ///
86    /// # Worst-case complexity
87    /// $T(n) = O(n \log n \log\log n)$
88    ///
89    /// $M(n) = O(n \log n)$
90    ///
91    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
92    ///
93    /// # Examples
94    /// ```
95    /// use malachite_base::num::arithmetic::traits::AbsSquaredAssign;
96    /// use malachite_nz::integer::Integer;
97    ///
98    /// let mut x = Integer::from(-123);
99    /// x.abs_squared_assign();
100    /// assert_eq!(x, 15129);
101    /// ```
102    #[inline]
103    fn abs_squared_assign(&mut self) {
104        self.square_assign();
105    }
106}