forked from hunter-packages/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmean.cpp
More file actions
186 lines (169 loc) · 6.93 KB
/
mean.cpp
File metadata and controls
186 lines (169 loc) · 6.93 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
/*******************************************************
* 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 <af/dim4.hpp>
#include <af/statistics.h>
#include <af/defines.h>
#include <err_common.hpp>
#include <backend.hpp>
#include <handle.hpp>
#include <reduce.hpp>
#include <arith.hpp>
#include <math.hpp>
#include <cast.hpp>
#include "stats.h"
using namespace detail;
template<typename Ti, typename To>
static To mean(const af_array &in)
{
/* following function is defined in stats.h */
return mean<Ti, To>(getArray<Ti>(in)); /* defined in stats.h */
}
template<typename T>
static T mean(const af_array &in, const af_array &weights)
{
typedef typename baseOutType<T>::type Tw;
/* following function is defined in stats.h */
return mean<T, Tw>(castArray<T>(in), castArray<Tw>(weights));
}
template<typename Ti, typename To>
static af_array mean(const af_array &in, const dim_t dim)
{
/* following function is defined in stats.h */
return getHandle<To>(mean<Ti, To>(getArray<Ti>(in), dim));
}
template<typename T>
static af_array mean(const af_array &in, const af_array &weights, const dim_t dim)
{
/* following function is defined in stats.h */
return getHandle<T>(mean<T>(castArray<T>(in), castArray<T>(weights), dim));
}
af_err af_mean(af_array *out, const af_array in, const dim_t dim)
{
try {
ARG_ASSERT(2, (dim>=0 && dim<=3));
af_array output = 0;
ArrayInfo info = getInfo(in);
af_dtype type = info.getType();
switch(type) {
case f64: output = mean<double , double>(in, dim); break;
case f32: output = mean<float , float >(in, dim); break;
case s32: output = mean<int , float >(in, dim); break;
case u32: output = mean<unsigned, float >(in, dim); break;
case s64: output = mean<intl , double>(in, dim); break;
case u64: output = mean<uintl , double>(in, dim); break;
case s16: output = mean<short , float >(in, dim); break;
case u16: output = mean<ushort , float >(in, dim); break;
case u8: output = mean<uchar , float >(in, dim); break;
case b8: output = mean<char , float >(in, dim); break;
case c32: output = mean<cfloat , cfloat>(in, dim); break;
case c64: output = mean<cdouble , cdouble>(in, dim); break;
default : TYPE_ERROR(1, type);
}
std::swap(*out, output);
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_mean_weighted(af_array *out, const af_array in, const af_array weights, const dim_t dim)
{
try {
ARG_ASSERT(2, (dim>=0 && dim<=3));
af_array output = 0;
ArrayInfo iInfo = getInfo(in);
ArrayInfo wInfo = getInfo(weights);
af_dtype iType = iInfo.getType();
af_dtype wType = wInfo.getType();
ARG_ASSERT(2, (wType==f32 || wType==f64)); /* verify that weights are non-complex real numbers */
switch(iType) {
case f64: output = mean< double>(in, weights, dim); break;
case f32: output = mean< float >(in, weights, dim); break;
case s32: output = mean< float >(in, weights, dim); break;
case u32: output = mean< float >(in, weights, dim); break;
case s64: output = mean< double>(in, weights, dim); break;
case u64: output = mean< double>(in, weights, dim); break;
case s16: output = mean< float >(in, weights, dim); break;
case u16: output = mean< float >(in, weights, dim); break;
case u8: output = mean< float >(in, weights, dim); break;
case b8: output = mean< float >(in, weights, dim); break;
case c32: output = mean< cfloat>(in, weights, dim); break;
case c64: output = mean<cdouble>(in, weights, dim); break;
default : TYPE_ERROR(1, iType);
}
std::swap(*out, output);
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_mean_all(double *realVal, double *imagVal, const af_array in)
{
try {
ArrayInfo info = getInfo(in);
af_dtype type = info.getType();
switch(type) {
case f64: *realVal = mean<double , double>(in); break;
case f32: *realVal = mean<float , float >(in); break;
case s32: *realVal = mean<int , float >(in); break;
case u32: *realVal = mean<unsigned, float >(in); break;
case s64: *realVal = mean<intl , double>(in); break;
case u64: *realVal = mean<uintl , double>(in); break;
case s16: *realVal = mean<short , float >(in); break;
case u16: *realVal = mean<ushort , float >(in); break;
case u8: *realVal = mean<uchar , float >(in); break;
case b8: *realVal = mean<char , float >(in); break;
case c32: {
cfloat tmp = mean<cfloat, cfloat>(in);
*realVal = real(tmp);
*imagVal = imag(tmp);
} break;
case c64: {
cdouble tmp = mean<cdouble, cdouble>(in);
*realVal = real(tmp);
*imagVal = imag(tmp);
} break;
default : TYPE_ERROR(1, type);
}
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_mean_all_weighted(double *realVal, double *imagVal, const af_array in, const af_array weights)
{
try {
ArrayInfo iInfo = getInfo(in);
ArrayInfo wInfo = getInfo(weights);
af_dtype iType = iInfo.getType();
af_dtype wType = wInfo.getType();
ARG_ASSERT(3, (wType==f32 || wType==f64)); /* verify that weights are non-complex real numbers */
switch(iType) {
case f64: *realVal = mean<double>(in, weights); break;
case f32: *realVal = mean< float>(in, weights); break;
case s32: *realVal = mean< float>(in, weights); break;
case u32: *realVal = mean< float>(in, weights); break;
case s64: *realVal = mean<double>(in, weights); break;
case u64: *realVal = mean<double>(in, weights); break;
case s16: *realVal = mean< float>(in, weights); break;
case u16: *realVal = mean< float>(in, weights); break;
case u8: *realVal = mean< float>(in, weights); break;
case b8: *realVal = mean< float>(in, weights); break;
case c32: {
cfloat tmp = mean<cfloat>(in, weights);
*realVal = real(tmp);
*imagVal = imag(tmp);
} break;
case c64: {
cdouble tmp = mean<cdouble>(in, weights);
*realVal = real(tmp);
*imagVal = imag(tmp);
} break;
default : TYPE_ERROR(1, iType);
}
}
CATCHALL;
return AF_SUCCESS;
}