The OpenD Programming Language

mean

Computes the 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.

Parameters

F

controls type of output

summation

algorithm for calculating sums (default: Summation.appropriate)

Return Value

The mean of all the elements in the input, must be floating point or complex type

Examples

import mir.ndslice.slice: sliced;
import mir.complex;
alias C = Complex!double;

assert(mean([1.0, 2, 3]) == 2);
assert(mean([C(1, 3), C(2), C(3)]) == C(2, 1));

assert(mean!float([0, 1, 2, 3, 4, 5].sliced(3, 2)) == 2.5);

static assert(is(typeof(mean!float([1, 2, 3])) == float));

Mean of vector

import mir.ndslice.slice: sliced;

auto 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].sliced;
assert(x.mean == 29.25 / 12);

Mean of matrix

import mir.ndslice.fuse: fuse;

auto 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]
].fuse;

assert(x.mean == 29.25 / 12);

Column mean of matrix

import mir.ndslice.fuse: fuse;
import mir.ndslice.topology: alongDim, byDim, map;
import mir.algorithm.iteration: all;
import mir.math.common: approxEqual;

auto 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]
].fuse;
auto result = [1, 4.25, 3.25, 1.5, 2.5, 2.125];

// Use byDim or alongDim with map to compute mean of row/column.
assert(x.byDim!1.map!mean.all!approxEqual(result));
assert(x.alongDim!0.map!mean.all!approxEqual(result));

// FIXME
// Without using map, computes the mean of the whole slice
// assert(x.byDim!1.mean == x.sliced.mean);
// assert(x.alongDim!0.mean == x.sliced.mean);

Can also set algorithm or output type

import mir.ndslice.slice: sliced;
import mir.ndslice.topology: repeat;

//Set sum algorithm or output type

auto a = [1, 1e100, 1, -1e100].sliced;

auto x = a * 10_000;

assert(x.mean!"kbn" == 20_000 / 4);
assert(x.mean!"kb2" == 20_000 / 4);
assert(x.mean!"precise" == 20_000 / 4);
assert(x.mean!(double, "precise") == 20_000.0 / 4);

auto y = uint.max.repeat(3);
assert(y.mean!ulong == 12884901885 / 3);

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 = [0, 1, 1, 2, 4, 4,
          2, 7, 5, 1, 2, 0].sliced;

auto y = x.mean;
assert(y.approxEqual(29.0 / 12, 1.0e-10));
static assert(is(typeof(y) == double));

assert(x.mean!float.approxEqual(29f / 12, 1.0e-10));

Mean works for complex numbers and other user-defined types (provided they can be converted to a floating point or complex type)

import mir.complex.math: approxEqual;
import mir.ndslice.slice: sliced;
import mir.complex;
alias C = Complex!double;

auto x = [C(1.0, 2), C(2, 3), C(3, 4), C(4, 5)].sliced;
assert(x.mean.approxEqual(C(2.5, 3.5)));

Compute mean tensors along specified dimention of tensors

import mir.ndslice: alongDim, iota, as, map;
/++
  [[0,1,2],
   [3,4,5]]
 +/
auto x = iota(2, 3).as!double;
assert(x.mean == (5.0 / 2.0));

auto m0 = [(0.0+3.0)/2.0, (1.0+4.0)/2.0, (2.0+5.0)/2.0];
assert(x.alongDim!0.map!mean == m0);
assert(x.alongDim!(-2).map!mean == m0);

auto m1 = [(0.0+1.0+2.0)/3.0, (3.0+4.0+5.0)/3.0];
assert(x.alongDim!1.map!mean == m1);
assert(x.alongDim!(-1).map!mean == m1);

assert(iota(2, 3, 4, 5).as!double.alongDim!0.map!mean == iota([3, 4, 5], 3 * 4 * 5 / 2));

Arbitrary mean

assert(mean(1.0, 2, 3) == 2);
assert(mean!float(1, 2, 3) == 2);

See Also

$(MATHREF_ALT sum, Summation)

Meta