template arguments to pass to mir.math.sum (to compute the harmonic mean)
Harmonic mean of vector
import mir.ndslice.slice : sliced; import std.math : approxEqual; static immutable x = [20.0, 100.0, 2000.0, 10.0, 5.0, 2.0]; auto y = x.sliced.hmean; assert(approxEqual(y, 6.97269));
Harmonic mean of matrix
import mir.ndslice.slice : sliced; import std.math : approxEqual; static immutable x = [20.0, 100.0, 2000.0, 10.0, 5.0, 2.0]; auto y = x.sliced(2, 3).hmean; assert(approxEqual(y, 6.97269));
Column harmonic mean of matrix
import mir.ndslice.slice : sliced; import mir.ndslice.topology : alongDim, byDim, map; import numir : approxEqual; static immutable _x = [20.0, 100.0, 2000.0, 10.0, 5.0, 2.0]; auto x = _x.sliced(2, 3); static immutable _y = [13.33333, 9.52381, 3.996004]; auto y = _y.sliced; // Use byDim or alongDim with map to compute mean of row/column. assert(approxEqual(x.byDim!1.map!hmean, y)); assert(approxEqual(x.alongDim!0.map!hmean, y));
Can also pass arguments to hmean
import mir.ndslice.slice: sliced; import mir.ndslice.topology: map, repeat; import std.math : approxEqual; //Set sum algorithm or output type static immutable _x = [1, 1e100, 1, -1e100]; auto x = _x.sliced * 10_000; assert(approxEqual(x.hmean!"kbn", 20_000)); assert(approxEqual(x.hmean!"kb2", 20_000)); assert(approxEqual(x.hmean!"precise", 20_000)); assert(approxEqual(x.hmean!(double, "precise"), 20_000.0));
$(MATHREF sum, sum)
Computes the harmonic mean of a slice.