-
Notifications
You must be signed in to change notification settings - Fork 550
Expand file tree
/
Copy pathcomplex.cpp
More file actions
199 lines (182 loc) · 10.3 KB
/
complex.cpp
File metadata and controls
199 lines (182 loc) · 10.3 KB
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
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <gtest/gtest.h>
#include <testHelpers.hpp>
#include <af/arith.h>
#include <af/array.h>
#include <af/data.h>
#include <af/device.h>
#include <af/random.h>
using std::endl;
using namespace af;
const int num = 10;
#define CPLX(TYPE) af_c##TYPE
#define COMPLEX_TESTS(Ta, Tb, Tc) \
TEST(ComplexTests, Test_##Ta##_##Tb) { \
SUPPORTED_TYPE_CHECK(Ta); \
SUPPORTED_TYPE_CHECK(Tb); \
SUPPORTED_TYPE_CHECK(Tc); \
\
af_dtype ta = (af_dtype)dtype_traits<Ta>::af_type; \
af_dtype tb = (af_dtype)dtype_traits<Tb>::af_type; \
array a = randu(num, ta); \
array b = randu(num, tb); \
array c = complex(a, b); \
Ta *h_a = a.host<Ta>(); \
Tb *h_b = b.host<Tb>(); \
CPLX(Tc) *h_c = c.host<CPLX(Tc)>(); \
for (int i = 0; i < num; i++) \
ASSERT_EQ(h_c[i], CPLX(Tc)(h_a[i], h_b[i])) \
<< "for values: " << h_a[i] << "," << h_b[i] << endl; \
freeHost(h_a); \
freeHost(h_b); \
freeHost(h_c); \
} \
TEST(ComplexTests, Test_cplx_##Ta##_##Tb##_left) { \
SUPPORTED_TYPE_CHECK(Ta); \
SUPPORTED_TYPE_CHECK(Tb); \
\
af_dtype ta = (af_dtype)dtype_traits<Ta>::af_type; \
array a = randu(num, ta); \
Tb h_b = 0.3; \
array c = complex(a, h_b); \
Ta *h_a = a.host<Ta>(); \
CPLX(Ta) *h_c = c.host<CPLX(Ta)>(); \
for (int i = 0; i < num; i++) \
ASSERT_EQ(h_c[i], CPLX(Ta)(h_a[i], h_b)) \
<< "for values: " << h_a[i] << "," << h_b << endl; \
freeHost(h_a); \
freeHost(h_c); \
} \
\
TEST(ComplexTests, Test_cplx_##Ta##_##Tb##_right) { \
SUPPORTED_TYPE_CHECK(Ta); \
SUPPORTED_TYPE_CHECK(Tb); \
\
af_dtype tb = (af_dtype)dtype_traits<Tb>::af_type; \
Ta h_a = 0.3; \
array b = randu(num, tb); \
array c = complex(h_a, b); \
Tb *h_b = b.host<Tb>(); \
CPLX(Tb) *h_c = c.host<CPLX(Tb)>(); \
for (int i = 0; i < num; i++) \
ASSERT_EQ(h_c[i], CPLX(Tb)(h_a, h_b[i])) \
<< "for values: " << h_a << "," << h_b[i] << endl; \
freeHost(h_b); \
freeHost(h_c); \
} \
TEST(ComplexTests, Test_##Ta##_##Tb##_Real) { \
SUPPORTED_TYPE_CHECK(Ta); \
SUPPORTED_TYPE_CHECK(Tb); \
SUPPORTED_TYPE_CHECK(Tc); \
\
af_dtype ta = (af_dtype)dtype_traits<Ta>::af_type; \
af_dtype tb = (af_dtype)dtype_traits<Tb>::af_type; \
array a = randu(num, ta); \
array b = randu(num, tb); \
array c = complex(a, b); \
array d = real(c); \
Ta *h_a = a.host<Ta>(); \
Tc *h_d = d.host<Tc>(); \
for (int i = 0; i < num; i++) \
ASSERT_EQ(h_d[i], h_a[i]) << "at: " << i << endl; \
freeHost(h_a); \
freeHost(h_d); \
} \
TEST(ComplexTests, Test_##Ta##_##Tb##_Imag) { \
SUPPORTED_TYPE_CHECK(Ta); \
SUPPORTED_TYPE_CHECK(Tb); \
SUPPORTED_TYPE_CHECK(Tc); \
\
af_dtype ta = (af_dtype)dtype_traits<Ta>::af_type; \
af_dtype tb = (af_dtype)dtype_traits<Tb>::af_type; \
array a = randu(num, ta); \
array b = randu(num, tb); \
array c = complex(a, b); \
array d = imag(c); \
Tb *h_b = b.host<Tb>(); \
Tc *h_d = d.host<Tc>(); \
for (int i = 0; i < num; i++) \
ASSERT_EQ(h_d[i], h_b[i]) << "at: " << i << endl; \
freeHost(h_b); \
freeHost(h_d); \
} \
TEST(ComplexTests, Test_##Ta##_##Tb##_Conj) { \
SUPPORTED_TYPE_CHECK(Ta); \
SUPPORTED_TYPE_CHECK(Tb); \
SUPPORTED_TYPE_CHECK(Tc); \
\
af_dtype ta = (af_dtype)dtype_traits<Ta>::af_type; \
af_dtype tb = (af_dtype)dtype_traits<Tb>::af_type; \
array a = randu(num, ta); \
array b = randu(num, tb); \
array c = complex(a, b); \
array d = conjg(c); \
CPLX(Tc) *h_c = c.host<CPLX(Tc)>(); \
CPLX(Tc) *h_d = d.host<CPLX(Tc)>(); \
for (int i = 0; i < num; i++) \
ASSERT_EQ(conj(h_c[i]), h_d[i]) << "at: " << i << endl; \
freeHost(h_c); \
freeHost(h_d); \
}
COMPLEX_TESTS(float, float, float)
COMPLEX_TESTS(double, double, double)
COMPLEX_TESTS(float, double, double)
TEST(Complex, SNIPPET_arith_func_complex) {
//! [ex_arith_func_complex]
//!
// Create a, a 2x3 array
array a = iota(dim4(2, 3)); // a = [0, 2, 4,
// 1, 3, 5]
// Create b from a single real array, returning zeros for the imaginary
// component
array b = complex(a); // b = [(0, 0), (2, 0), (4, 0),
// (1, 0), (3, 0), (5, 0)]
// Create c from two real arrays, one for the real component and one for the
// imaginary component
array c = complex(a, a); // c = [(0, 0), (2, 2), (4, 4),
// (1, 1), (3, 3), (5, 5)]
// Create d from a single real array for the real component and a single
// scalar for each imaginary component
array d = complex(a, 2); // d = [(0, 2), (2, 2), (4, 2),
// (1, 2), (3, 2), (5, 2)]
// Create e from a single scalar for each real component and a single real
// array for the imaginary component
array e = complex(2, a); // e = [(2, 0), (2, 2), (2, 4),
// (2, 1), (2, 3), (2, 5)]
//! [ex_arith_func_complex]
using std::complex;
using std::vector;
vector<float> ha(a.elements());
a.host(ha.data());
vector<cfloat> gold_b(a.elements());
for (int i = 0; i < a.elements(); i++) {
gold_b[i].real = ha[i];
gold_b[i].imag = 0;
}
ASSERT_VEC_ARRAY_EQ(gold_b, a.dims(), b);
vector<cfloat> gold_c(a.elements());
for (int i = 0; i < a.elements(); i++) {
gold_c[i].real = ha[i];
gold_c[i].imag = ha[i];
}
ASSERT_VEC_ARRAY_EQ(gold_c, a.dims(), c);
vector<cfloat> gold_d(a.elements());
for (int i = 0; i < a.elements(); i++) {
gold_d[i].real = ha[i];
gold_d[i].imag = 2;
}
ASSERT_VEC_ARRAY_EQ(gold_d, a.dims(), d);
vector<cfloat> gold_e(a.elements());
for (int i = 0; i < a.elements(); i++) {
gold_e[i].real = 2;
gold_e[i].imag = ha[i];
}
ASSERT_VEC_ARRAY_EQ(gold_e, a.dims(), e);
}