gmean

Computes the geometric mean of a slice.

  1. auto gmean(S slice)
  2. auto gmean(S slice, Seed seed)
    nothrow @nogc deprecated pure nothrow @safe @nogc
    gmean
    (
    S
    Seed
    )
    (,
    Seed seed
    )
    if (
    isSlice!S
    )

Parameters

slice S

input slice

seed Seed

seed used to calculate product (should be 1 in most cases)

Return Value

Type: auto

geometric mean of slice

Examples

Geometric mean of vector

import mir.ndslice.slice : sliced;
import std.math : approxEqual;

static immutable x = [1.1, 0.99, 1.01, 1.2, 0.9, 1.05];

auto y = x.sliced.gmean;
assert(approxEqual(y, 1.037513));

Geometric mean of matrix

import mir.ndslice.slice : sliced;
import std.math : approxEqual;

static immutable x = [1.1, 0.99, 1.01, 1.2, 0.9, 1.05];

auto y = x.sliced(2, 3).gmean;
assert(approxEqual(y, 1.037513));

Column geometric mean of matrix

import mir.ndslice.slice : sliced;
import mir.ndslice.topology : alongDim, byDim, map;
import numir : approxEqual;

static immutable x = [1.1, 0.99, 1.01, 1.2, 0.9, 1.05];
static immutable y = [1.148913, 0.943928, 1.029806];
// Use byDim or alongDim with map to compute mean of row/column.
assert(approxEqual(x.sliced(2, 3).byDim!1.map!gmean, y.sliced));
assert(approxEqual(x.sliced(2, 3).alongDim!0.map!gmean, y.sliced));

Geometric mean of vector with seed

import mir.ndslice.slice : sliced;
import std.math : approxEqual;

enum l = 2.0 ^^ (double.max_exp - 1);
enum s = 2.0 ^^ -(double.max_exp - 1);
static immutable x = [l, l, l, s, s, s, 0.8 * 2.0 ^^ 10];

real seed = 1;
auto y = x.sliced.gmean(seed);
assert(approxEqual(y, (0.8 * 2.0 ^^ 10) ^^ (1.0 / 7.0)));

Meta