Summator

Output range for summation. Precise, KB2, KBN and Kahan algorithms are supported.

struct Summator (
T
Summation summation = Summation.Precise
) if (
isMutable!T &&
(
summation == Summation.Precise &&
isFloatingPoint!T
||
summation == Summation.Kahan &&
isSummable!T
||
(
summation == Summation.KBN ||
summation == Summation.KB2
)
&&
(
isFloatingPoint!T ||
isComplex!T
)
)
) {}

Constructors

this
this(T n)

Destructor

~this
~this()

Postblit

this(this)
this(this)
Undocumented in source.

Members

Aliases

F
alias F = T
Undocumented in source.
F
alias F = SummationType!T
Undocumented in source.

Functions

isFinite
bool isFinite()

Returns true if current sum is finite (not infinite or NaN).

isFinite
bool isFinite()

Returns true if current sum is finite (not infinite or NaN).

isInfinity
bool isInfinity()

Returns true if current sum is ±∞.

isInfinity
bool isInfinity()

Returns true if current sum is ±∞.

isNaN
bool isNaN()

Returns true if current sum is a NaN.

isNaN
bool isNaN()

Returns true if current sum is a NaN.

opAssign
void opAssign(T rhs)

Operator overloading.

opCast
C opCast()

Returns Summator with extended internal partial sums.

opCast
C opCast()

cast(C) operator overloading. Returns cast(C)sum(). See also: cast

opOpAssign
void opOpAssign(T rhs)
void opOpAssign(Summator rhs)

Operator overloading.

partialsSum
F partialsSum()
Undocumented in source. Be warned that the author may not have intended to support it.
put
void put(T n)

Adds x to the internal partial sums.

sum
T sum()

Returns the value of the sum.

unsafePut
void unsafePut(F x)

Adds x to the internal partial sums. This operation doesn't re-establish special value semantics across iterations (i.e. handling -inf + inf). Preconditions: isFinite(x).

Variables

c
F c;
Undocumented in source.
c
F c;
Undocumented in source.
ccs
F ccs;
Undocumented in source.
cs
F cs;
Undocumented in source.
s
F s;
Undocumented in source.
s
F s;
Undocumented in source.
s
F s;
Undocumented in source.
t
F t;
Undocumented in source.
y
F y;
Undocumented in source.

Examples

import std.range;
import std.algorithm: swap;

///Moving mean
class MovingAverage
{
    Summator!double summator;
    double[] circularBuffer;
    size_t frontIndex;

    double avg() @property
    {
        return summator.sum() / circularBuffer.length;
    }

    this(double[] buffer)
    {
        assert(!buffer.empty);
        circularBuffer = buffer;
        summator = 0;
        .put(summator, buffer);
    }

    ///operation without rounding
    void put(double x)
    {
        summator += x;
        swap(circularBuffer[frontIndex++], x);
        summator -= x;
        frontIndex %= circularBuffer.length;
    }
}

/// ma always keeps pricese average of last 1000 elements
auto ma = new MovingAverage(iota(0.0, 1000.0).array);
assert(ma.avg == (1000*999/2) / 1000.0);
/// move by 10 elements
put(ma, iota(1000.0, 1010.0));
assert(ma.avg == (1010*1009/2 - 10*9/2) / 1000.0);

move by 10 elements

Summator!double d;
assert(d.isNaN());
assert(d.sum().isNaN());
d += 100;
assert(d.isNaN());
assert(d.sum().isNaN());
d = 1;
d += 1000;
assert(d.sum == 1001);

Meta