forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmean.cpp
More file actions
183 lines (160 loc) · 6.4 KB
/
mean.cpp
File metadata and controls
183 lines (160 loc) · 6.4 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
/*******************************************************
* 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 inType, typename outType>
static outType mean(const af_array &in)
{
Array<outType> input = cast<outType>(getArray<inType>(in));
outType result = mean<outType>(input); /* defined in stats.h */
return result;
}
template<typename inType, typename outType>
static outType mean(const af_array &in, const af_array &weights)
{
typedef typename baseOutType<outType>::type bType;
Array<outType> input = cast<outType>(getArray<inType>(in));
Array<outType> wts = cast<outType>(getArray<bType>(weights));
outType result = mean<outType, bType>(input, getArray<bType>(weights)); /* defined in stats.h */
return result;
}
template<typename inType, typename outType>
static af_array mean(const af_array &in, dim_type dim)
{
Array<outType> input = cast<outType>(getArray<inType>(in));
Array<outType> result= mean<outType>(input, dim); /* defined in stats.h */
return getHandle<outType>(result);
}
template<typename inType, typename outType>
static af_array mean(const af_array &in, const af_array &weights, dim_type dim)
{
typedef typename baseOutType<outType>::type bType;
Array<outType> input = cast<outType>(getArray<inType>(in));
Array<outType> wts = cast<outType>(getArray<bType>(weights));
Array<outType> retVal= mean<outType>(input, wts, dim); /* defined in stats.h */
return getHandle<outType>(retVal);
}
af_err af_mean(af_array *out, const af_array in, dim_type 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<uint , 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, dim_type 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, double>(in, weights, dim); break;
case f32: output = mean<float , float >(in, weights, dim); break;
case s32: output = mean<int , float >(in, weights, dim); break;
case u32: output = mean<uint , float >(in, weights, dim); break;
case u8: output = mean<uchar , float >(in, weights, dim); break;
case b8: output = mean<char , float >(in, weights, dim); break;
case c32: output = mean<cfloat, cfloat>(in, weights, dim); break;
case c64: output = mean<cdouble,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<uint , 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, double>(in, weights); break;
case f32: *realVal = mean<float , float>(in, weights); break;
case s32: *realVal = mean<int , float>(in, weights); break;
case u32: *realVal = mean<uint , float>(in, weights); break;
case u8: *realVal = mean<uchar , float>(in, weights); break;
case b8: *realVal = mean<char , float>(in, weights); 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, iType);
}
}
CATCHALL;
return AF_SUCCESS;
}