File: complex_math.hpp

package info (click to toggle)
blis 1.1-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 43,020 kB
  • sloc: ansic: 360,997; fortran: 21,831; sh: 8,565; cpp: 7,086; makefile: 1,563; asm: 1,516; python: 701
file content (267 lines) | stat: -rw-r--r-- 6,443 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include <cmath>
#include <algorithm>
#include <type_traits>

#include "blis.h"

template <typename T>
struct is_complex : std::false_type {};

template <>
struct is_complex<scomplex> : std::true_type {};

template <>
struct is_complex<dcomplex> : std::true_type {};

template <typename T>
struct is_real : std::integral_constant<bool,!is_complex<T>::value> {};

template <typename T> struct make_complex;

template <> struct make_complex<float   > { using type = scomplex; };
template <> struct make_complex<double  > { using type = dcomplex; };
template <> struct make_complex<scomplex> { using type = scomplex; };
template <> struct make_complex<dcomplex> { using type = dcomplex; };

template <typename T>
using make_complex_t = typename make_complex<T>::type;

template <typename T> struct make_real;

template <> struct make_real<float   > { using type = float; };
template <> struct make_real<double  > { using type = double; };
template <> struct make_real<scomplex> { using type = float; };
template <> struct make_real<dcomplex> { using type = double; };

template <typename T>
using make_real_t = typename make_real<T>::type;

template <typename T, bool Cond>
struct make_complex_if : std::conditional<Cond,make_complex_t<T>,make_real_t<T>> {};

template <typename T, bool Cond>
using make_complex_if_t = typename make_complex_if<T,Cond>::type;

template <typename T>
struct real_imag_part
{
    real_imag_part& operator=(T) { return *this; }

    operator T() const { return T(); }
};

template <typename T>
std::enable_if_t<std::is_arithmetic<typename std::remove_cv<T>::type>::value,T&> real(T& x) { return x; }

template <typename T>
std::enable_if_t<std::is_arithmetic<T>::value,real_imag_part<T>> imag(T x) { return {}; }

inline float& real(scomplex& x) { return x.real; }

inline float& imag(scomplex& x) { return x.imag; }

inline double& real(dcomplex& x) { return x.real; }

inline double& imag(dcomplex& x) { return x.imag; }

inline const float& real(const scomplex& x) { return x.real; }

inline const float& imag(const scomplex& x) { return x.imag; }

inline const double& real(const dcomplex& x) { return x.real; }

inline const double& imag(const dcomplex& x) { return x.imag; }

template <typename T>
std::enable_if_t<is_real<T>::value,T> conj(T x) { return x; }

template <typename T>
std::enable_if_t<is_complex<T>::value,T> conj(const T& x) { return {x.real, -x.imag}; }

template <typename T, typename U, typename=void>
struct convert_impl;

template <typename T, typename U>
struct convert_impl<T, U, std::enable_if_t<is_real<T>::value && is_real<U>::value>>
{
    void operator()(T x, U& y) const { y = x; }
};

template <typename T, typename U>
struct convert_impl<T, U, std::enable_if_t<is_real<T>::value && is_complex<U>::value>>
{
    void operator()(T x, U& y) const { y.real = x; y.imag = 0; }
};

template <typename T, typename U>
struct convert_impl<T, U, std::enable_if_t<is_complex<T>::value && is_real<U>::value>>
{
    void operator()(T x, U& y) const { y = x.real; }
};

template <typename T, typename U>
struct convert_impl<T, U, std::enable_if_t<is_complex<T>::value && is_complex<U>::value>>
{
    void operator()(T x, U& y) const { y.real = x.real; y.imag = x.imag; }
};

template <typename U, typename T>
U convert(T x)
{
    U y;
    convert_impl<T,U>{}(x,y);
    return y;
}

template <typename U, typename T>
auto convert_prec(T x) -> make_complex_if_t<U,is_complex<T>::value>
{
    return convert<make_complex_if_t<U,is_complex<T>::value>>(x);
}

#define COMPLEX_MATH_OPS(rtype, ctype) \
\
inline bool operator==(rtype x, ctype y) \
{ \
    return x == y.real && y.imag == 0; \
} \
\
inline bool operator==(ctype x, rtype y) \
{ \
    return y == x.real && x.imag == 0; \
} \
\
inline bool operator==(ctype x, ctype y) \
{ \
    return x.real == y.real && \
           x.imag == y.imag; \
 } \
 \
inline ctype operator-(ctype x) \
{ \
    return {-x.real, -x.imag}; \
} \
\
inline ctype operator+(rtype x, ctype y) \
{ \
    return {x+y.real, y.imag}; \
} \
\
inline ctype operator+(ctype x, rtype y) \
{ \
    return {y+x.real, x.imag}; \
} \
\
inline ctype operator+(ctype x, ctype y) \
{ \
    return {x.real+y.real, x.imag+y.imag}; \
} \
\
inline ctype operator-(rtype x, ctype y) \
{ \
    return {x-y.real, -y.imag}; \
} \
\
inline ctype operator-(ctype x, rtype y) \
{ \
    return {x.real-y, x.imag}; \
} \
\
inline ctype operator-(ctype x, ctype y) \
{ \
    return {x.real-y.real, x.imag-y.imag}; \
} \
\
inline ctype operator*(rtype x, ctype y) \
{ \
    return {x*y.real, x*y.imag}; \
} \
\
inline ctype operator*(ctype x, rtype y) \
{ \
    return {y*x.real, y*x.imag}; \
} \
\
inline ctype operator*(ctype x, ctype y) \
{ \
    return {x.real*y.real - x.imag*y.imag, \
            x.real*y.imag + x.imag*y.real}; \
} \
\
inline ctype operator/(rtype x, ctype y) \
{ \
    auto scale = std::max(std::abs(y.real), std::abs(y.imag)); \
    auto n = std::ilogb(scale); \
    auto yrs = std::scalbn(y.real, -n); \
    auto yis = std::scalbn(y.imag, -n); \
    auto denom = y.real*yrs + y.imag*yis; \
    return {x*yrs/denom, -x*yis/denom}; \
} \
\
inline ctype operator/(ctype x, rtype y) \
{ \
    return {x.real/y, x.imag/y}; \
} \
\
inline ctype operator/(ctype x, ctype y) \
{ \
    auto scale = std::max(std::abs(y.real), std::abs(y.imag)); \
    auto n = std::ilogb(scale); \
    auto yrs = std::scalbn(y.real, -n); \
    auto yis = std::scalbn(y.imag, -n); \
    auto denom = y.real*yrs + y.imag*yis; \
    return {(x.real*yrs + x.imag*yis)/denom, \
            (x.imag*yrs - x.real*yis)/denom}; \
} \
\
inline ctype& operator+=(ctype& x, rtype y) \
{ \
    x.real += y; \
    return x; \
} \
\
inline ctype& operator+=(ctype& x, ctype y) \
{ \
    x.real += y.real; x.imag += y.imag; \
    return x; \
} \
\
inline ctype& operator-=(ctype& x, rtype y) \
{ \
    x.real -= y; \
    return x; \
} \
\
inline ctype& operator-=(ctype& x, ctype y) \
{ \
    x.real -= y.real; x.imag -= y.imag; \
    return x; \
} \
\
inline ctype& operator*=(ctype& x, rtype y) \
{ \
    x.real *= y; x.imag *= y; \
    return x; \
} \
\
inline ctype& operator*=(ctype& x, ctype y) \
{ \
    x = x * y; \
    return x; \
} \
\
inline ctype& operator/=(ctype& x, rtype y) \
{ \
    x.real /= y; x.imag /= y; \
    return x; \
} \
\
inline ctype& operator/=(ctype& x, ctype y) \
{ \
    x = x / y; \
    return x; \
}

COMPLEX_MATH_OPS(float,  scomplex);
COMPLEX_MATH_OPS(double, dcomplex);