template arguments to pass to mir.math.sum (to compute the mean of the slice and the relevant moment)
2nd central moment of vector
import std.math : approxEqual; import mir.ndslice.slice : sliced; static immutable x = [0.0, 1.0, 1.5, 2.0, 3.5, 4.25, 2.0, 7.5, 5.0, 1.0, 1.5, 0.0]; // FIXME assert(approxEqual(x.sliced.moment(2), 54.76563 / 12)); assert(approxEqual(x.sliced.moment(2.0), 54.76563 / 12));
2nd central moment of matrix
import mir.ndslice.slice : sliced; import std.math : approxEqual; static immutable x = [0.0, 1.0, 1.5, 2.0, 3.5, 4.25, 2.0, 7.5, 5.0, 1.0, 1.5, 0.0]; assert(approxEqual(x.sliced(2, 6).moment(2.0), 54.76563 / 12));
Row 2nd central moment of matrix
import mir.ndslice.slice : sliced; import mir.ndslice.topology : byDim, alongDim, map; import std.math : approxEqual; static immutable x = [0.0, 1.0, 1.5, 2.0, 3.5, 4.25, 2.0, 7.5, 5.0, 1.0, 1.5, 0.0]; static immutable y = [2.092014, 6.722222]; // Use byDim or alongDim with map to compute moment of row/column. assert(approxEqual(x.sliced(2, 6).byDim!0.map!(a => a.moment(2.0)), y.sliced)); assert(approxEqual(x.sliced(2, 6).alongDim!1.map!(a => a.moment(2.0)), y.sliced));
$(MATHREF sum, sum)
Computes the n-th central moment of a slice.