MCPcopy Create free account
hub / github.com/arrayfire/arrayfire / meanvar

Function meanvar

src/api/c/var.cpp:104–142  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

102
103template<typename inType, typename outType>
104static tuple<Array<outType>, Array<outType>> meanvar(
105 const Array<inType>& in,
106 const Array<typename baseOutType<outType>::type>& weights,
107 const af_var_bias bias, const dim_t dim) {
108 using weightType = typename baseOutType<outType>::type;
109 Array<outType> input = cast<outType>(in);
110 dim4 iDims = input.dims();
111
112 Array<outType> meanArr = createEmptyArray<outType>({0});
113 Array<outType> normArr = createEmptyArray<outType>({0});
114 if (weights.isEmpty()) {
115 meanArr = mean<outType, weightType, outType>(input, dim);
116 auto val = 1.0 / static_cast<double>(bias == AF_VARIANCE_POPULATION
117 ? iDims[dim]
118 : iDims[dim] - 1);
119 normArr =
120 createValueArray<outType>(meanArr.dims(), scalar<outType>(val));
121 } else {
122 meanArr = mean<outType, weightType>(input, weights, dim);
123 Array<outType> wtsSum = cast<outType>(
124 reduce<af_add_t, weightType, weightType>(weights, dim));
125 Array<outType> ones =
126 createValueArray<outType>(wtsSum.dims(), scalar<outType>(1));
127 if (bias == AF_VARIANCE_SAMPLE) {
128 wtsSum = arithOp<outType, af_sub_t>(wtsSum, ones, ones.dims());
129 }
130 normArr = arithOp<outType, af_div_t>(ones, wtsSum, meanArr.dims());
131 }
132
133 Array<outType> diff =
134 arithOp<outType, af_sub_t>(input, meanArr, input.dims());
135 Array<outType> diffSq = arithOp<outType, af_mul_t>(diff, diff, diff.dims());
136 Array<outType> redDiff = reduce<af_add_t, outType, outType>(diffSq, dim);
137
138 Array<outType> variance =
139 arithOp<outType, af_mul_t>(normArr, redDiff, redDiff.dims());
140
141 return make_tuple(meanArr, variance);
142}
143
144template<typename inType, typename outType>
145static tuple<af_array, af_array> meanvar(const af_array& in,

Callers 1

Calls 3

isEmptyMethod · 0.80
getHandleFunction · 0.70
dimsMethod · 0.45

Tested by

no test coverage detected