forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmedian.cpp
More file actions
109 lines (95 loc) · 3.61 KB
/
median.cpp
File metadata and controls
109 lines (95 loc) · 3.61 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
/*******************************************************
* 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 <af/array.h>
#include <af/arith.h>
#include <af/data.h>
#include <testHelpers.hpp>
using namespace std;
using namespace af;
template<typename To, typename Ti, bool flat>
void median0(int nx, int ny=1, int nz=1, int nw=1)
{
if (noDoubleTests<Ti>()) return;
array a = randu(nx, ny, nz, nw, (af::dtype)dtype_traits<Ti>::af_type);
array sa = sort(a);
Ti *h_sa = sa.host<Ti>();
To *h_b = NULL;
To val = 0;
if (flat) {
val = median<To>(a);
h_b = &val;
} else {
array b = median(a);
h_b = b.host<To>();
}
for (int w = 0; w < nw; w++) {
for (int z = 0; z < nz; z++) {
for (int y = 0; y < ny; y++) {
int off = (y + ny * (z + nz * w));
int id = nx / 2;
if (nx & 2) {
ASSERT_EQ(h_sa[id + off * nx], h_b[off]);
} else {
To left = h_sa[id + off * nx - 1];
To right = h_sa[id + off * nx];
ASSERT_NEAR((left + right) / 2, h_b[off], 1e-8);
}
}
}
}
delete[] h_sa;
if (!flat) delete[] h_b;
}
#define MEDIAN0(To, Ti) \
TEST(median0, Ti##_1D_even) \
{ \
median0<To, Ti, false>(1000); \
} \
TEST(median0, Ti##_2D_even) \
{ \
median0<To, Ti, false>(1000, 100); \
} \
TEST(median0, Ti##_3D_even) \
{ \
median0<To, Ti, false>(1000, 25, 4); \
} \
TEST(median0, Ti##_4D_even) \
{ \
median0<To, Ti, false>(1000, 25, 2, 2); \
} \
TEST(median0, Ti##_flat_even) \
{ \
median0<To, Ti, true>(1000); \
} \
TEST(median0, Ti##_1D_odd) \
{ \
median0<To, Ti, false>(783); \
} \
TEST(median0, Ti##_2D_odd) \
{ \
median0<To, Ti, false>(783, 100); \
} \
TEST(median0, Ti##_3D_odd) \
{ \
median0<To, Ti, false>(783, 25, 4); \
} \
TEST(median0, Ti##_4D_odd) \
{ \
median0<To, Ti, false>(783, 25, 2, 2); \
} \
TEST(median0, Ti##_flat_odd) \
{ \
median0<To, Ti, true>(783); \
} \
MEDIAN0(float, float)
MEDIAN0(float, int)
MEDIAN0(float, uint)
MEDIAN0(float, uchar)
MEDIAN0(double, double)