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