cMHN 1.2
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
complex.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef pRC_CORE_COMPLEX_COMPLEX_H
4#define pRC_CORE_COMPLEX_COMPLEX_H
5
17
18namespace pRC
19{
20 template<IsValue T>
21 class Complex
22 {
23 public:
24 using Type = T;
25 template<class C>
27
28 public:
29 ~Complex() = default;
30 constexpr Complex(Complex const &) = default;
31 constexpr Complex(Complex &&) = default;
32 constexpr Complex &operator=(Complex const &) & = default;
33 constexpr Complex &operator=(Complex &&) & = default;
34 Complex() = default;
35
36 template<IsConvertible<T> R>
37 Complex(Complex<R> const &other)
38 : mReal(other.real())
39 , mImag(other.imag())
40 {
41 }
42
43 template<IsConvertible<T> R>
44 constexpr Complex(R const &real, R const &imag)
45 : mReal(real)
46 , mImag(imag)
47 {
48 }
49
50 template<IsConvertible<T> R>
51 Complex(R const &real)
52 : mReal(real)
53 , mImag(zero())
54 {
55 }
56
57 constexpr Complex(Zero<> const)
58 : Complex(zero<Complex>())
59 {
60 }
61
62 constexpr Complex(Unit<> const)
63 : Complex(unit<Complex>())
64 {
65 }
66
67 constexpr Complex(Identity<> const)
69 {
70 }
71
72 template<IsConvertible<T> R>
73 constexpr auto &operator=(Complex<R> const &rhs) &
74 {
75 mReal = rhs.real();
76 mImag = rhs.imag();
77 return *this;
78 }
79
80 template<IsConvertible<T> R>
81 constexpr auto &operator=(R const &real) &
82 {
83 mReal = real;
84 mImag = zero();
85 return *this;
86 }
87
88 constexpr auto &operator=(Zero<> const) &
89 {
90 return *this = zero<Complex>();
91 }
92
93 constexpr auto &operator=(Unit<> const) &
94 {
95 return *this = unit<Complex>();
96 }
97
98 constexpr auto &operator=(Identity<> const) &
99 {
100 return *this = identity<Complex>();
101 }
102
103 constexpr decltype(auto) real() &&
104 {
105 return move(mReal);
106 }
107
108 constexpr decltype(auto) real() const &&
109 {
110 return move(mReal);
111 }
112
113 constexpr auto &real() &
114 {
115 return mReal;
116 }
117
118 constexpr auto &real() const &
119 {
120 return mReal;
121 }
122
123 constexpr decltype(auto) imag() &&
124 {
125 return move(mImag);
126 }
127
128 constexpr decltype(auto) imag() const &&
129 {
130 return move(mImag);
131 }
132
133 constexpr auto &imag() &
134 {
135 return mImag;
136 }
137
138 constexpr auto &imag() const &
139 {
140 return mImag;
141 }
142
143 template<class X>
145 constexpr auto &operator+=(X &&rhs) &
146 {
147 return *this = *this + forward<X>(rhs);
148 }
149
150 template<class X>
152 constexpr auto &operator-=(X &&rhs) &
153 {
154 return *this = *this - forward<X>(rhs);
155 }
156
157 template<class X>
159 constexpr auto &operator*=(X &&rhs) &
160 {
161 return *this = *this * forward<X>(rhs);
162 }
163
164 template<class X>
166 constexpr auto &operator/=(X &&rhs) &
167 {
168 return *this = *this / forward<X>(rhs);
169 }
170
171 constexpr operator T() const
172 {
173 return real();
174 }
175
176 private:
177 T mReal;
178 T mImag;
179 };
180
181 template<IsFloat T>
182 Complex(T const &) -> Complex<T>;
183
184 template<IsFloat R, IsFloat I>
185 Complex(R const &, I const &) -> Complex<Common<R, I>>;
186
187 template<class T>
188 concept IsComplex = !IsReference<T> && requires {
189 {
190 []<class U>(Complex<U> const &&)
191 {
192 }(std::declval<T>())
193 };
194 };
195
196 template<class T>
198 IsSame<ResultOf<Eval, T>, ResultOf<Eval, ResultOf<Complexify, T>>>;
199
200 template<class T>
202}
203#endif // pRC_CORE_COMPLEX_COMPLEX_H
Definition complex.hpp:22
constexpr auto & real() const &
Definition complex.hpp:118
constexpr auto & imag() &
Definition complex.hpp:133
constexpr auto & real() &
Definition complex.hpp:113
constexpr Complex(Zero<> const)
Definition complex.hpp:57
constexpr Complex & operator=(Complex const &) &=default
constexpr Complex(Complex &&)=default
constexpr Complex(R const &real, R const &imag)
Definition complex.hpp:44
constexpr auto & operator=(Identity<> const) &
Definition complex.hpp:98
constexpr decltype(auto) real() &&
Definition complex.hpp:103
constexpr auto & operator=(Zero<> const) &
Definition complex.hpp:88
constexpr auto & operator*=(X &&rhs) &
Definition complex.hpp:159
constexpr auto & operator/=(X &&rhs) &
Definition complex.hpp:166
constexpr Complex & operator=(Complex &&) &=default
~Complex()=default
Complex(R const &real)
Definition complex.hpp:51
Complex()=default
constexpr auto & operator+=(X &&rhs) &
Definition complex.hpp:145
constexpr Complex(Complex const &)=default
constexpr decltype(auto) real() const &&
Definition complex.hpp:108
constexpr decltype(auto) imag() const &&
Definition complex.hpp:128
constexpr auto & imag() const &
Definition complex.hpp:138
constexpr Complex(Identity<> const)
Definition complex.hpp:67
constexpr auto & operator-=(X &&rhs) &
Definition complex.hpp:152
constexpr Complex(Unit<> const)
Definition complex.hpp:62
Complex(Complex< R > const &other)
Definition complex.hpp:37
constexpr decltype(auto) imag() &&
Definition complex.hpp:123
constexpr auto & operator=(Complex< R > const &rhs) &
Definition complex.hpp:73
constexpr auto & operator=(R const &real) &
Definition complex.hpp:81
constexpr auto & operator=(Unit<> const) &
Definition complex.hpp:93
Definition value.hpp:12
Definition complex.hpp:188
Definition complex.hpp:197
Definition concepts.hpp:31
Definition concepts.hpp:19
Definition concepts.hpp:28
pRC::Float<> T
Definition externs_nonTT.hpp:1
Definition cholesky.hpp:10
static constexpr auto unit()
Definition unit.hpp:13
Complex(T const &) -> Complex< T >
RemoveConst< RemoveReference< T > > RemoveConstReference
Definition basics.hpp:47
RemoveConstReference< ResultOf< Eval, ResultOf< Real, T > > > NonComplex
Definition complex.hpp:201
static constexpr auto identity()
Definition identity.hpp:13
static constexpr auto zero()
Definition zero.hpp:12
Definition identity.hpp:11
Definition unit.hpp:11
Definition zero.hpp:11