cMHN 1.1
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
16
17namespace pRC
18{
19 template<class T, If<IsFloat<T>> = 0>
20 Complex(T const &) -> Complex<T>;
21 template<class TA, class TB, If<All<IsFloat<TA>, IsFloat<TB>>> = 0>
22 Complex(TA const &, TB const &) -> Complex<Common<TA, TB>>;
23
24 template<class T>
25 class Complex
26 {
27 static_assert(IsFloat<T>(), "Complex<T>: T has to be of type Float.");
28
29 public:
30 using Type = T;
31 template<class C, If<IsFloat<C>> = 0>
33
34 using Value = typename T::Value;
35 template<class V, If<IsValue<V>> = 0>
37
38 using Signed = typename T::Signed;
39 template<Bool R>
41
42 using Width = typename T::Width;
43 template<Size Q>
45
48 using NonComplex = T;
49
50 public:
51 ~Complex() = default;
52 constexpr Complex(Complex const &) = default;
53 constexpr Complex(Complex &&) = default;
54 constexpr Complex &operator=(Complex const &) & = default;
55 constexpr Complex &operator=(Complex &&) & = default;
56 Complex() = default;
57
58 template<class R, If<IsConvertible<R, T>> = 0>
60 : mReal(other.real())
61 , mImag(other.imag())
62 {
63 }
64
65 template<class R, If<IsConvertible<R, T>> = 0>
66 constexpr Complex(R const &real, R const &imag)
67 : mReal(real)
68 , mImag(imag)
69 {
70 }
71
72 template<class R, If<IsConvertible<R, T>> = 0>
73 Complex(R const &real)
74 : mReal(real)
75 , mImag(zero())
76 {
77 }
78
79 constexpr Complex(Zero<> const)
80 : Complex(zero<Complex>())
81 {
82 }
83
84 constexpr Complex(Unit<> const)
85 : Complex(unit<Complex>())
86 {
87 }
88
89 constexpr Complex(Identity<> const)
91 {
92 }
93
94 template<class R, If<IsConvertible<R, T>> = 0>
95 constexpr auto &operator=(Complex<R> const &rhs) &
96 {
97 mReal = rhs.real();
98 mImag = rhs.imag();
99 return *this;
100 }
101
102 template<class R, If<IsConvertible<R, T>> = 0>
103 constexpr auto &operator=(R const &real) &
104 {
105 mReal = real;
106 mImag = zero();
107 return *this;
108 }
109
110 constexpr auto &operator=(Zero<> const) &
111 {
112 return *this = zero<Complex>();
113 }
114
115 constexpr auto &operator=(Unit<> const) &
116 {
117 return *this = unit<Complex>();
118 }
119
120 constexpr auto &operator=(Identity<> const) &
121 {
122 return *this = identity<Complex>();
123 }
124
125 constexpr decltype(auto) real() &&
126 {
127 return move(mReal);
128 }
129
130 constexpr decltype(auto) real() const &&
131 {
132 return move(mReal);
133 }
134
135 constexpr auto &real() &
136 {
137 return mReal;
138 }
139
140 constexpr auto &real() const &
141 {
142 return mReal;
143 }
144
145 constexpr decltype(auto) imag() &&
146 {
147 return move(mImag);
148 }
149
150 constexpr decltype(auto) imag() const &&
151 {
152 return move(mImag);
153 }
154
155 constexpr auto &imag() &
156 {
157 return mImag;
158 }
159
160 constexpr auto &imag() const &
161 {
162 return mImag;
163 }
164
165 template<class X, If<IsInvocable<Add, Complex &, X>> = 0>
166 constexpr auto &operator+=(X &&rhs) &
167 {
168 return *this = *this + forward<X>(rhs);
169 }
170
171 template<class X, If<IsInvocable<Sub, Complex &, X>> = 0>
172 constexpr auto &operator-=(X &&rhs) &
173 {
174 return *this = *this - forward<X>(rhs);
175 }
176
177 template<class X, If<IsInvocable<Mul, Complex &, X>> = 0>
178 constexpr auto &operator*=(X &&rhs) &
179 {
180 return *this = *this * forward<X>(rhs);
181 }
182
183 template<class X, If<IsInvocable<Div, Complex &, X>> = 0>
184 constexpr auto &operator/=(X &&rhs) &
185 {
186 return *this = *this / forward<X>(rhs);
187 }
188
189 private:
190 T mReal;
191 T mImag;
192 };
193}
194#endif // pRC_CORE_COMPLEX_COMPLEX_H
Definition complex.hpp:26
constexpr auto & real() const &
Definition complex.hpp:140
constexpr auto & imag() &
Definition complex.hpp:155
constexpr auto & operator=(R const &real) &
Definition complex.hpp:103
constexpr auto & real() &
Definition complex.hpp:135
constexpr Complex(Zero<> const)
Definition complex.hpp:79
constexpr auto & operator-=(X &&rhs) &
Definition complex.hpp:172
constexpr Complex & operator=(Complex const &) &=default
typename T::Value Value
Definition complex.hpp:34
constexpr Complex(Complex &&)=default
constexpr auto & operator=(Identity<> const) &
Definition complex.hpp:120
Complex(R const &real)
Definition complex.hpp:73
constexpr decltype(auto) real() &&
Definition complex.hpp:125
typename T::Signed Signed
Definition complex.hpp:38
constexpr auto & operator=(Zero<> const) &
Definition complex.hpp:110
constexpr auto & operator*=(X &&rhs) &
Definition complex.hpp:178
constexpr auto & operator+=(X &&rhs) &
Definition complex.hpp:166
constexpr Complex & operator=(Complex &&) &=default
~Complex()=default
Complex(Complex< R > const &other)
Definition complex.hpp:59
Complex()=default
constexpr auto & operator=(Complex< R > const &rhs) &
Definition complex.hpp:95
constexpr Complex(Complex const &)=default
constexpr decltype(auto) real() const &&
Definition complex.hpp:130
constexpr decltype(auto) imag() const &&
Definition complex.hpp:150
constexpr auto & imag() const &
Definition complex.hpp:160
constexpr auto & operator/=(X &&rhs) &
Definition complex.hpp:184
constexpr Complex(Identity<> const)
Definition complex.hpp:89
typename T::Width Width
Definition complex.hpp:42
constexpr Complex(R const &real, R const &imag)
Definition complex.hpp:66
constexpr Complex(Unit<> const)
Definition complex.hpp:84
constexpr decltype(auto) imag() &&
Definition complex.hpp:145
constexpr auto & operator=(Unit<> const) &
Definition complex.hpp:115
pRC::Constant< Size, W > Width
Definition float.hpp:39
Float< W > Value
Definition float.hpp:31
True<> Signed
Definition float.hpp:35
pRC::Float<> T
Definition externs_nonTT.hpp:1
Definition cholesky.hpp:18
static constexpr auto makeConstantSequence()
Definition sequence.hpp:402
static constexpr auto zero()
Definition zero.hpp:12
static constexpr auto unit()
Definition unit.hpp:12
Complex(T const &) -> Complex< T >
static constexpr auto identity()
Definition identity.hpp:12
Definition type_traits.hpp:262
Definition type_traits.hpp:16
Definition type_traits.hpp:265
Definition type_traits.hpp:268