malachite_float/float/arithmetic/conjugate.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::Float;
10use malachite_base::num::arithmetic::traits::{Conjugate, ConjugateAssign};
11
12impl Conjugate for Float {
13 type Output = Self;
14
15 /// Computes the complex conjugate of a [`Float`], taking it by value. A real number is its own
16 /// conjugate, so this is the identity; even `NaN` is returned unchanged.
17 ///
18 /// $$
19 /// f(x) = \overline{x} = x.
20 /// $$
21 ///
22 /// # Worst-case complexity
23 /// Constant time and additional memory.
24 ///
25 /// # Examples
26 /// ```
27 /// use malachite_base::num::arithmetic::traits::Conjugate;
28 /// use malachite_float::{ComparableFloat, Float};
29 ///
30 /// assert_eq!(
31 /// ComparableFloat(Float::from(-1.5).conjugate()),
32 /// ComparableFloat(Float::from(-1.5))
33 /// );
34 /// ```
35 #[inline]
36 fn conjugate(self) -> Self {
37 self
38 }
39}
40
41impl Conjugate for &Float {
42 type Output = Float;
43
44 /// Computes the complex conjugate of a [`Float`], taking it by reference. A real number is its
45 /// own conjugate, so this just clones; even `NaN` is returned unchanged.
46 ///
47 /// $$
48 /// f(x) = \overline{x} = x.
49 /// $$
50 ///
51 /// # Worst-case complexity
52 /// $T(n) = O(n)$
53 ///
54 /// $M(n) = O(n)$
55 ///
56 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
57 ///
58 /// # Examples
59 /// ```
60 /// use malachite_base::num::arithmetic::traits::Conjugate;
61 /// use malachite_float::{ComparableFloat, Float};
62 ///
63 /// assert_eq!(
64 /// ComparableFloat((&Float::from(-1.5)).conjugate()),
65 /// ComparableFloat(Float::from(-1.5))
66 /// );
67 /// ```
68 #[inline]
69 fn conjugate(self) -> Float {
70 self.clone()
71 }
72}
73
74impl ConjugateAssign for Float {
75 /// Replaces a [`Float`] with its complex conjugate. A real number is its own conjugate, so this
76 /// does nothing.
77 ///
78 /// $$
79 /// x \gets \overline{x} = x.
80 /// $$
81 ///
82 /// # Worst-case complexity
83 /// Constant time and additional memory.
84 ///
85 /// # Examples
86 /// ```
87 /// use malachite_base::num::arithmetic::traits::ConjugateAssign;
88 /// use malachite_float::{ComparableFloat, Float};
89 ///
90 /// let mut x = Float::from(-1.5);
91 /// x.conjugate_assign();
92 /// assert_eq!(ComparableFloat(x), ComparableFloat(Float::from(-1.5)));
93 /// ```
94 #[inline]
95 fn conjugate_assign(&mut self) {}
96}