logsumexp

Computes the log of the sum of exponentials of input elements.

  1. auto logsumexp(A a)
    template logsumexp(sumTemplateArgs...)
    nothrow @nogc nothrow @nogc
    logsumexp
    (
    A
    )
    (
    A a
    )
    if (
    isSlice!A
    )
  2. auto logsumexp(A a, B b)

Members

Functions

logsumexp
auto logsumexp(A a)

Params a = input slice

logsumexp
auto logsumexp(A a, B b)

Params a = input slice b = scaling factor slice for exp(a) must have the same shape to b

Parameters

sumTemplateArgs

template arguments to pass to mir.math.sum (to compute the mean & standard deviation of the slice)

Examples

// logsumexp example from doctest https://github.com/scipy/scipy/blob/maintenance/1.0.x/scipy/special/_logsumexp.py
import mir.ndslice : iota, map, sliced, as;
import mir.math.common : log, exp;
import mir.math.sum;
import std.math : approxEqual;
{
    auto x = iota(10).as!double;
    assert(logsumexp(x).approxEqual(9.4586297444267107));
    assert(log(sum(map!exp(x))).approxEqual(logsumexp(x)));
}
{
    auto x = iota(0).as!double;
    assert(sum(x) == 0);
    assert(logsumexp(x) == -double.infinity);
    assert(log(sum(map!exp(x))).approxEqual(logsumexp(x)));
}
{
    auto a = iota(10).as!double;
    auto b = iota([10], 10, -1).as!double;
    assert(logsumexp(a, b).approxEqual(9.9170178533034665));
    assert(log(sum(map!exp(a) * b)).approxEqual(logsumexp(a, b)));
}
{
    auto a = iota(0).as!double;
    auto b = iota(0).as!double;
    assert(logsumexp(a, b) == -double.infinity);
    assert(log(sum(map!exp(a) * b)).approxEqual(logsumexp(a, b)));
}

See Also

Meta