Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions source/mir/math/numeric.d
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ prodType!Range prod(Range)(Range r)
if (isIterable!Range)
{
import core.lifetime: move;

alias F = typeof(return);
return .prod!(F, Range)(r.move);
}
Expand All @@ -264,18 +264,32 @@ prodType!Range prod(Range)(Range r, ref long exp)

/++
Params:
ar = values
val = values
Returns:
The prduct of all the elements in `ar`
The prduct of all the elements in `val`
+/
prodType!T prod(T)(scope const T[] ar...)
F prod(F)(scope const F[] val...)
if (isFloatingPoint!F)
{
alias F = typeof(return);
ProdAccumulator!F prod;
prod.put(ar);
prod.put(val);
return prod.prod;
}

/++
Params:
val = values
Returns:
The prduct of all the elements in `val`
+/
prodType!(CommonType!T) prod(T...)(T val)
if (T.length > 0 &&
!is(CommonType!T == void))
{
alias F = typeof(return);
return .prod!(F)(val);
}

/// Product of arbitrary inputs
version(mir_test)
@safe pure @nogc nothrow
Expand Down
11 changes: 7 additions & 4 deletions source/mir/math/stat.d
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ template mean(F, Summation summation = Summation.appropriate)
/// ditto
template mean(Summation summation = Summation.appropriate)
{
import std.traits: CommonType;

/++
Params:
r = range, must be finite iterable
Expand All @@ -149,12 +151,13 @@ template mean(Summation summation = Summation.appropriate)

/++
Params:
ar = values
val = values
+/
@fmamath sumType!T mean(T)(scope const T[] ar...)
@fmamath CommonType!T mean(T...)(T val)
if (T.length > 0 &&
!is(CommonType!T == void))
{
alias F = typeof(return);
return .mean!(F, summation)(ar);
return .mean!(CommonType!T, summation)(val);
}
}

Expand Down
64 changes: 4 additions & 60 deletions source/mir/math/sum.d
Original file line number Diff line number Diff line change
Expand Up @@ -235,17 +235,6 @@ unittest
assert(ma.avg == (1010 * 1009 / 2 - 10 * 9 / 2) / 1000.0);
}

/// Arbitrary sum
version(mir_test)
@safe pure nothrow
unittest
{
assert(sum(1, 2, 3, 4) == 10);
assert(sum!float(1, 2, 3, 4) == 10f);
assert(sum(1f, 2, 3, 4) == 10f);
assert(sum(1.0 + 2i, 2 + 3i, 3 + 4i, 4 + 5i) == (10 + 14i));
}

version(X86)
version = X86_Any;
version(X86_64)
Expand Down Expand Up @@ -1712,21 +1701,6 @@ template sum(F, Summation summation = Summation.appropriate)
}
}
}

///
F sum(scope const F[] r...)
{
static if (isComplex!F && summation == Summation.precise)
{
return sum(r, summationInitValue!F);
}
else
{
Summator!(F, ResolveSummationType!(summation, const(F)[], F)) sum;
sum.put(r);
return sum.sum;
}
}
}

///ditto
Expand All @@ -1748,13 +1722,6 @@ template sum(Summation summation = Summation.appropriate)
import core.lifetime: move;
return .sum!(F, ResolveSummationType!(summation, Range, F))(r.move, seed);
}

///
sumType!T sum(T)(scope const T[] ar...)
{
alias F = typeof(return);
return .sum!(F, ResolveSummationType!(summation, F[], F))(ar);
}
}

///ditto
Expand Down Expand Up @@ -1873,24 +1840,6 @@ unittest
}
}

version(mir_test)
unittest
{
assert(sum(1) == 1);
assert(sum(1, 2, 3) == 6);
assert(sum(1.0, 2.0, 3.0) == 6);
assert(sum(1.0 + 1i, 2.0 + 2i, 3.0 + 3i) == (6 + 6i));
}

version(mir_test)
unittest
{
assert(sum!float(1) == 1f);
assert(sum!float(1, 2, 3) == 6f);
assert(sum!float(1.0, 2.0, 3.0) == 6f);
assert(sum!cfloat(1.0 + 1i, 2.0 + 2i, 3.0 + 3i) == (6f + 6i));
}

version(LDC)
version(X86_Any)
version(mir_test)
Expand Down Expand Up @@ -1986,15 +1935,10 @@ private T summationInitValue(T)()
package template sumType(Range)
{
import mir.ndslice.slice: isSlice, DeepElementType;

static if (isIterable!Range) {
static if (isSlice!Range)
alias T = Unqual!(DeepElementType!(Range.This));
else
alias T = Unqual!(ForeachType!Range);
} else {
alias T = Unqual!Range;
}
static if (isSlice!Range)
alias T = Unqual!(DeepElementType!(Range.This));
else
alias T = Unqual!(ForeachType!Range);
static if (__traits(compiles, {
auto a = T.init + T.init;
a += T.init;
Expand Down