malachite_base/num/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::num::arithmetic::traits::{AbsSquared, AbsSquaredAssign, Square, SquareAssign};
10
11macro_rules! impl_abs_squared {
12 ($t:ident) => {
13 impl AbsSquared for $t {
14 type Output = $t;
15
16 /// Computes the squared absolute value of a number. For real types this is the same as
17 /// squaring.
18 ///
19 /// $f(x) = |x|^2 = x^2$.
20 ///
21 /// # Worst-case complexity
22 /// Constant time and additional memory.
23 ///
24 /// # Examples
25 /// See [here](super::abs_squared#abs_squared).
26 #[inline]
27 fn abs_squared(self) -> $t {
28 self.square()
29 }
30 }
31
32 impl AbsSquaredAssign for $t {
33 /// Replaces a number with its squared absolute value. For real types this is the same
34 /// as squaring in place.
35 ///
36 /// $x \gets |x|^2 = x^2$.
37 ///
38 /// # Worst-case complexity
39 /// Constant time and additional memory.
40 ///
41 /// # Examples
42 /// See [here](super::abs_squared#abs_squared_assign).
43 #[inline]
44 fn abs_squared_assign(&mut self) {
45 self.square_assign();
46 }
47 }
48 };
49}
50apply_to_primitive_ints!(impl_abs_squared);
51apply_to_primitive_floats!(impl_abs_squared);