harmonic mean of all the elements of the input, must be floating point or complex type
Harmonic mean of vector
import mir.math.common: approxEqual; import mir.ndslice.slice: sliced; auto x = [20.0, 100.0, 2000.0, 10.0, 5.0, 2.0].sliced; assert(x.hmean.approxEqual(6.97269));
Harmonic mean of matrix
import mir.math.common: approxEqual; import mir.ndslice.fuse: fuse; auto x = [ [20.0, 100.0, 2000.0], [10.0, 5.0, 2.0] ].fuse; assert(x.hmean.approxEqual(6.97269));
Column harmonic mean of matrix
import mir.algorithm.iteration: all; import mir.math.common: approxEqual; import mir.ndslice: fuse; import mir.ndslice.topology: alongDim, byDim, map; auto x = [ [20.0, 100.0, 2000.0], [ 10.0, 5.0, 2.0] ].fuse; auto y = [13.33333, 9.52381, 3.996004]; // Use byDim or alongDim with map to compute mean of row/column. assert(x.byDim!1.map!hmean.all!approxEqual(y)); assert(x.alongDim!0.map!hmean.all!approxEqual(y));
Can also pass arguments to hmean
import mir.math.common: approxEqual; import mir.ndslice.topology: repeat; import mir.ndslice.slice: sliced; //Set sum algorithm or output type auto x = [1, 1e-100, 1, -1e-100].sliced; assert(x.hmean!"kb2".approxEqual(2)); assert(x.hmean!"precise".approxEqual(2)); assert(x.hmean!(double, "precise").approxEqual(2)); //Provide the summation type assert(float.max.repeat(3).hmean!double.approxEqual(float.max));
For integral slices, pass output type as template parameter to ensure output type is correct.
import mir.math.common: approxEqual; import mir.ndslice.slice: sliced; auto x = [20, 100, 2000, 10, 5, 2].sliced; auto y = x.hmean; assert(y.approxEqual(6.97269)); static assert(is(typeof(y) == double)); assert(x.hmean!float.approxEqual(6.97269));
hmean works for complex numbers and other user-defined types (provided they can be converted to a floating point or complex type)
import mir.math.common: approxEqual; import mir.ndslice.slice: sliced; auto x = [1.0 + 2i, 2 + 3i, 3 + 4i, 4 + 5i].sliced; assert(x.hmean.approxEqual(1.97110904 + 3.14849332i));
Arbitrary harmonic mean
import mir.math.common: approxEqual; import mir.ndslice.slice: sliced; auto x = hmean(20.0, 100, 2000, 10, 5, 2); assert(x.approxEqual(6.97269)); auto y = hmean!float(20, 100, 2000, 10, 5, 2); assert(y.approxEqual(6.97269));
Computes the harmonic mean of the input.
By default, if F is not floating point type or complex type, then the result will have a double type if F is implicitly convertible to a floating point type or a type for which isComplex!F is true.