forked from xtensor-stack/xtensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_xnan_functions.cpp
354 lines (295 loc) · 15.1 KB
/
test_xnan_functions.cpp
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/***************************************************************************
* Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
* Copyright (c) QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#include "test_common_macros.hpp"
#if (defined(__GNUC__) && !defined(__clang__))
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#include "xtensor/xmath.hpp"
#pragma GCC diagnostic pop
#endif
#include "xtensor/xarray.hpp"
#include "xtensor/xtensor.hpp"
#include "xtensor/xmath.hpp"
#include "xtensor/xstrided_view.hpp"
#include <xtensor/xindex_view.hpp>
#include "xtl/xtype_traits.hpp"
namespace xt
{
using namespace std::complex_literals;
static const double nanv = std::nan("0");
static const double d_min = std::numeric_limits<double>::min();
static const double d_max = std::numeric_limits<double>::max();
#define NAN_SENSITIVE_EQ(E1, E2, PLACE_HOLDER) \
EXPECT_EQ(xt::isnan(E1), xt::equal(E2, PLACE_HOLDER)); \
EXPECT_EQ(xt::filter(E1, !xt::isnan(E1)), xt::filter(E2, xt::not_equal(E2, PLACE_HOLDER)));
namespace nantest
{
xarray<double> aN = {{ nanv, nanv, 123, 3 }, { 1, 2, nanv, 3 }, { 1, 1, nanv, 3 }};
xarray<double> aR = {{ 0, 0, 123 , 3 }, { 1, 2, 0 , 3}, { 1, 1, 0, 3 }};
xarray<double> aP = {{ 1, 1, 123 , 3}, { 1, 2, 1, 3 }, { 1, 1, 1, 3 }};
xarray<double> aI = {{ d_max, d_max, 123 , 3}, { 1, 2, d_max, 3 }, { 1, 1, d_max, 3 }};
xarray<double> aA = {{ d_min, d_min, 123 , 3}, { 1, 2, d_min, 3 }, { 1, 1, d_min, 3 }};
xarray<double> xN = {{{nanv, nanv}, {1, 2}}, {{3, nanv}, {nanv, 5}}};
xarray<double> xR = {{{0, 0}, {1,2}}, {{3, 0}, {0, 5}}};
xarray<double> xP = {{{1, 1}, {1,2}}, {{3, 1}, {1, 5}}};
xarray<double> xI = {{{d_max, d_max}, {1, 2}}, {{3, d_max}, {d_max, 5}}};
xarray<double> xA = {{{d_min, d_min}, {1, 2}}, {{3, d_min}, {d_min, 5}}};
xarray<std::complex<double>> cN = {
{1.0 + 1.0i, 1.0 + 1.0i, nanv },
{1.0 - 1.0i, 1.0 , 3.0 + 2.0i}
};
}
TEST(xnanfunctions, count_nonnan)
{
xarray<double> a = {{0, 1, 2, 3}, {nanv, nanv, nanv, nanv}, {3, nanv, 1, nanv}};
std::size_t as = count_nonnan(a)();
std::size_t ase = count_nonnan(a, evaluation_strategy::immediate)();
EXPECT_EQ(as, 6u);
EXPECT_EQ(ase, 6u);
xarray<std::size_t> ea0 = {2, 1, 2, 1};
xarray<std::size_t> ea1 = {4, 0, 2};
EXPECT_EQ(count_nonnan(a, {0}), ea0);
EXPECT_EQ(count_nonnan(a, {1}), ea1);
EXPECT_EQ(count_nonnan(a, {0}, evaluation_strategy::immediate), ea0);
EXPECT_EQ(count_nonnan(a, {1}, evaluation_strategy::immediate), ea1);
}
TEST(xnanfunctions, nan_to_num)
{
double neg_inf = -std::numeric_limits<double>::infinity();
double inf = std::numeric_limits<double>::infinity();
xarray<double> a = {{ nanv, nanv, 123}, {0.5123, neg_inf, inf}};
auto expr = nan_to_num(a);
EXPECT_EQ(expr(0, 0), 0);
EXPECT_EQ(expr(0, 1), 0);
EXPECT_EQ(expr(0, 2), 123);
EXPECT_EQ(expr(1, 0), 0.5123);
EXPECT_EQ(expr(1, 1), std::numeric_limits<double>::lowest());
EXPECT_TRUE(expr(1, 1) < 0);
EXPECT_EQ(expr(1, 2), std::numeric_limits<double>::max());
xarray<double> exp = {{ 0, 0, 123 }, {0.5123, std::numeric_limits<double>::lowest(), std::numeric_limits<double>::max() }};
xarray<double> assigned = exp;
EXPECT_EQ(assigned, exp);
}
TEST(xnanfunctions, nanmin)
{
EXPECT_EQ(nanmin(nantest::aN), amin(nantest::aI));
EXPECT_EQ(nanmin(nantest::aN, {0}), amin(nantest::aI, {0}));
EXPECT_EQ(nanmin(nantest::aN, {1}), amin(nantest::aI, {1}));
for (size_t i = 0; i < 3; ++i)
{
auto ret = nanmin(nantest::xN, {i});
auto reference = amin(nantest::xI, {i});
NAN_SENSITIVE_EQ(ret, reference, d_max)
}
}
TEST(xnanfunctions, nanmax)
{
EXPECT_EQ(nanmax(nantest::aN), amax(nantest::aA));
EXPECT_EQ(nanmax(nantest::aN, {0}), amax(nantest::aA, {0}));
EXPECT_EQ(nanmax(nantest::aN, {1}), amax(nantest::aA, {1}));
for (size_t i = 0; i < 3; ++i)
{
auto ret = nanmax(nantest::xN, {i});
auto reference = amax(nantest::xA, {i});
NAN_SENSITIVE_EQ(ret, reference, d_min)
}
}
TEST(xnanfunctions, nansum)
{
xarray<double> res = nansum(nantest::aN);
xarray<double> res0 = sum(nantest::aR);
EXPECT_EQ(res(0), 137);
EXPECT_EQ(nansum(nantest::aN, {0}), sum(nantest::aR, {0}));
EXPECT_EQ(nansum(nantest::aN, {1}), sum(nantest::aR, {1}));
EXPECT_EQ(nansum(nantest::xN, {0}), sum(nantest::xR, {0}));
EXPECT_EQ(nansum(nantest::xN, {1}), sum(nantest::xR, {1}));
EXPECT_EQ(nansum(nantest::xN, {2}), sum(nantest::xR, {2}));
}
TEST(xnanfunctions, nanprod)
{
xarray<double> res = nanprod(nantest::aN);
xarray<double> res0 = prod(nantest::aP);
EXPECT_EQ(res(0), 6642);
EXPECT_EQ(nanprod(nantest::aN, {0}), prod(nantest::aP, {0}));
EXPECT_EQ(nanprod(nantest::aN, {1}), prod(nantest::aP, {1}));
EXPECT_EQ(nanprod(nantest::xN, {0}), prod(nantest::xP, {0}));
EXPECT_EQ(nanprod(nantest::xN, {1}), prod(nantest::xP, {1}));
EXPECT_EQ(nanprod(nantest::xN, {2}), prod(nantest::xP, {2}));
}
TEST(xnanfunctions, nancumsum)
{
EXPECT_EQ(nancumsum(nantest::aN), cumsum(nantest::aR));
EXPECT_EQ(nancumsum(nantest::aN, 0), cumsum(nantest::aR, 0));
EXPECT_EQ(nancumsum(nantest::aN, 1), cumsum(nantest::aR, 1));
EXPECT_EQ(nancumsum(nantest::xN, 0), cumsum(nantest::xR, 0));
EXPECT_EQ(nancumsum(nantest::xN, 1), cumsum(nantest::xR, 1));
EXPECT_EQ(nancumsum(nantest::xN, 2), cumsum(nantest::xR, 2));
}
TEST(xnanfunctions, multid)
{
xarray<double> arr = xt::arange(3 * 4 * 2 * 8 * 7);
arr.reshape({3, 4, 2, 8, 7});
xarray<double> carr = arr;
strided_view(arr, {0, xt::ellipsis()}) = nanv;
strided_view(carr, {0, xt::ellipsis()}) = 0;
EXPECT_EQ(nancumsum(arr, 0), cumsum(carr, 0));
EXPECT_EQ(nancumsum(arr, 1), cumsum(carr, 1));
EXPECT_EQ(nancumsum(arr, 2), cumsum(carr, 2));
EXPECT_EQ(nancumsum(arr, 3), cumsum(carr, 3));
EXPECT_EQ(nancumsum(arr, 4), cumsum(carr, 4));
}
TEST(xnanfunctions, nancumprod)
{
EXPECT_EQ(nancumprod(nantest::aN), cumprod(nantest::aP));
EXPECT_EQ(nancumprod(nantest::aN, 0), cumprod(nantest::aP, 0));
EXPECT_EQ(nancumprod(nantest::aN, 1), cumprod(nantest::aP, 1));
EXPECT_EQ(nancumprod(nantest::xN, 0), cumprod(nantest::xP, 0));
EXPECT_EQ(nancumprod(nantest::xN, 1), cumprod(nantest::xP, 1));
EXPECT_EQ(nancumprod(nantest::xN, 2), cumprod(nantest::xP, 2));
}
TEST(xnanfunctions, nanmean)
{
auto as = nanmean(nantest::aN)();
auto ase = nanmean(nantest::aN, evaluation_strategy::immediate)();
EXPECT_DOUBLE_EQ(as, 17.125);
EXPECT_DOUBLE_EQ(ase, 17.125);
xarray<double> eaN0 = {1.0, 1.5, 123, 3};
xarray<double> eaN1 = {63.0, 2.0, 5.0/3.0};
EXPECT_TENSOR_EQ(nanmean(nantest::aN, {0}), eaN0);
EXPECT_TENSOR_EQ(nanmean(nantest::aN, {1}), eaN1);
std::array<std::size_t, 1> axis{0};
EXPECT_EQ(nanmean(nantest::aN, axis), eaN0);
EXPECT_TENSOR_EQ(nanmean(nantest::aN, {0}, evaluation_strategy::immediate), eaN0);
EXPECT_TENSOR_EQ(nanmean(nantest::aN, {1}, evaluation_strategy::immediate), eaN1);
auto cs = nanmean(nantest::cN)();
auto cse = nanmean(nantest::cN, evaluation_strategy::immediate)();
EXPECT_DOUBLE_EQ(cs, std::complex<double>(1.4, 0.6));
EXPECT_DOUBLE_EQ(cse, std::complex<double>(1.4, 0.6));
xarray<std::complex<double>> ecN0 = {1.0 + 0.0i, 1.0+0.5i, 3.0+2.0i};
xarray<std::complex<double>> ecN1 = {1.0 + 1.0i, (5.0 + 1.0i) / 3.0};
EXPECT_TENSOR_EQ(nanmean(nantest::cN, {0}), ecN0);
EXPECT_TENSOR_EQ(nanmean(nantest::cN, {1}), ecN1);
EXPECT_TENSOR_EQ(nanmean(nantest::cN, {0}, evaluation_strategy::immediate), ecN0);
EXPECT_TENSOR_EQ(nanmean(nantest::cN, {1}, evaluation_strategy::immediate), ecN1);
}
TEST(xnanfunctions, nanvar)
{
auto as = nanvar(nantest::aN)();
auto ase = nanvar(nantest::aN, evaluation_strategy::immediate)();
EXPECT_EQ(as, 1602.109375);
EXPECT_EQ(ase, 1602.109375);
xarray<double> eaN0 = {0.0, 0.25, 0.0, 0.0};
xarray<double> eaN1 = {3600.0, 2.0/3.0, 8.0/9.0};
EXPECT_EQ(nanvar(nantest::aN, {0}), eaN0);
EXPECT_TRUE(allclose(nanvar(nantest::aN, {1}), eaN1));
std::array<std::size_t, 1> axis{0};
EXPECT_EQ(nanvar(nantest::aN, axis), eaN0);
EXPECT_EQ(nanvar(nantest::aN, {0}, evaluation_strategy::immediate), eaN0);
EXPECT_TRUE(allclose(nanvar(nantest::aN, {1}, evaluation_strategy::immediate), eaN1));
}
using shape_type = dynamic_shape<size_t>;
/*******************
* type conversion *
*******************/
#define CHECK_RESULT_TYPE(EXPRESSION, EXPECTED_TYPE) \
{ \
using result_type = typename std::decay_t<decltype(EXPRESSION)>::value_type; \
EXPECT_TRUE((std::is_same<result_type, EXPECTED_TYPE>::value)); \
}
TEST(xnanfunctions, result_type) {
shape_type shape = {4, 3, 2};
xarray<short> ashort(shape);
xarray<unsigned short> aushort(shape);
xarray<int> aint(shape);
xarray<unsigned int> auint(shape);
xarray<long long> along(shape);
xarray<unsigned long long> aulong(shape);
xarray<float> afloat(shape);
xarray<double> adouble(shape);
#define CHECK_RESULT_TYPE_FOR_ALL(INPUT, RESULT_TYPE, MINMAX_TYPE) \
CHECK_RESULT_TYPE(nansum(INPUT, {1, 2}), RESULT_TYPE); \
CHECK_RESULT_TYPE(nanmean(INPUT, {1, 2}), double); \
CHECK_RESULT_TYPE(nanvar(INPUT, {1, 2}), double); \
CHECK_RESULT_TYPE(nanstd(INPUT, {1, 2}), double); \
CHECK_RESULT_TYPE(nanmin(INPUT, {1, 2}), MINMAX_TYPE); \
CHECK_RESULT_TYPE(nanmax(INPUT, {1, 2}), MINMAX_TYPE);
#define CHECK_TEMPLATED_RESULT_TYPE_FOR_ALL(INPUT, TEMPLATE_TYPE, RESULT_TYPE, STD_TYPE, MINMAX_TYPE) \
CHECK_RESULT_TYPE(nansum<TEMPLATE_TYPE>(INPUT, {1, 2}), RESULT_TYPE) \
CHECK_RESULT_TYPE(nanmean<TEMPLATE_TYPE>(INPUT, {1, 2}), RESULT_TYPE) \
CHECK_RESULT_TYPE(nanvar<TEMPLATE_TYPE>(INPUT, {1, 2}), RESULT_TYPE) \
CHECK_RESULT_TYPE(nanstd<TEMPLATE_TYPE>(INPUT, {1, 2}), STD_TYPE) \
CHECK_RESULT_TYPE(nanmin<TEMPLATE_TYPE>(INPUT, {1, 2}), MINMAX_TYPE) \
CHECK_RESULT_TYPE(nanmax<TEMPLATE_TYPE>(INPUT, {1, 2}), MINMAX_TYPE)
/*********
* short *
*********/
CHECK_RESULT_TYPE_FOR_ALL(ashort, int, short);
CHECK_TEMPLATED_RESULT_TYPE_FOR_ALL(ashort, short, int, double, short);
CHECK_TEMPLATED_RESULT_TYPE_FOR_ALL(ashort, int, int, double, int);
CHECK_TEMPLATED_RESULT_TYPE_FOR_ALL(ashort, double, double, double, double);
/******************
* unsigned short *
******************/
CHECK_RESULT_TYPE_FOR_ALL(aushort, int, unsigned short);
CHECK_TEMPLATED_RESULT_TYPE_FOR_ALL(aushort, unsigned short, int, double, unsigned short);
CHECK_TEMPLATED_RESULT_TYPE_FOR_ALL(aushort, int, int, double, int);
CHECK_TEMPLATED_RESULT_TYPE_FOR_ALL(aushort, double, double, double, double);
/*********
* int *
*********/
CHECK_RESULT_TYPE_FOR_ALL(aint, int, int);
CHECK_TEMPLATED_RESULT_TYPE_FOR_ALL(aint, unsigned short, int, double, int);
CHECK_TEMPLATED_RESULT_TYPE_FOR_ALL(aint, int, int, double, int);
CHECK_TEMPLATED_RESULT_TYPE_FOR_ALL(aint, double, double, double, double);
/****************
* unsigned int *
****************/
CHECK_RESULT_TYPE_FOR_ALL(auint, unsigned int, unsigned int);
CHECK_TEMPLATED_RESULT_TYPE_FOR_ALL(auint, unsigned int, unsigned int, double, unsigned int);
CHECK_TEMPLATED_RESULT_TYPE_FOR_ALL(auint, unsigned int, unsigned int, double, unsigned int);
CHECK_TEMPLATED_RESULT_TYPE_FOR_ALL(auint, double, double, double, double);
/**********************
* long long *
**********************/
#ifndef SKIP_ON_WERROR
// intermediate computation done in double may imply precision loss
CHECK_RESULT_TYPE_FOR_ALL(along, signed long long, signed long long);
#endif
CHECK_TEMPLATED_RESULT_TYPE_FOR_ALL(along, int, signed long long, double, signed long long);
CHECK_TEMPLATED_RESULT_TYPE_FOR_ALL(along, long, signed long long, double, signed long long);
CHECK_TEMPLATED_RESULT_TYPE_FOR_ALL(along, signed long long, signed long long, double, signed long long);
/**********************
* unsigned long long *
**********************/
#ifndef SKIP_ON_WERROR
// intermediate computation done in double may imply precision loss
CHECK_RESULT_TYPE_FOR_ALL(aulong, unsigned long long, unsigned long long);
#endif
CHECK_TEMPLATED_RESULT_TYPE_FOR_ALL(aulong, unsigned int, unsigned long long, double, unsigned long long);
CHECK_TEMPLATED_RESULT_TYPE_FOR_ALL(aulong, unsigned long, unsigned long long, double, unsigned long long);
CHECK_TEMPLATED_RESULT_TYPE_FOR_ALL(aulong, unsigned long long, unsigned long long, double, unsigned long long);
/*********
* float *
*********/
CHECK_RESULT_TYPE_FOR_ALL(afloat, float, float);
#ifndef SKIP_ON_WERROR
// final conversion to int may imply conversion loss
CHECK_TEMPLATED_RESULT_TYPE_FOR_ALL(afloat, int, float, float, float);
#endif
CHECK_TEMPLATED_RESULT_TYPE_FOR_ALL(afloat, float, float, float, float);
CHECK_TEMPLATED_RESULT_TYPE_FOR_ALL(afloat, double, double, double, double);
/**********
* double *
**********/
CHECK_RESULT_TYPE_FOR_ALL(adouble, double, double);
CHECK_TEMPLATED_RESULT_TYPE_FOR_ALL(adouble, float, double, double, double);
CHECK_TEMPLATED_RESULT_TYPE_FOR_ALL(adouble, double, double, double, double);
CHECK_TEMPLATED_RESULT_TYPE_FOR_ALL(adouble, long double, long double, long double, long double);
}
}