From a264487a7f95b67ef2a676c9af20f780839fa3e1 Mon Sep 17 00:00:00 2001 From: jmh530 Date: Tue, 2 Jun 2020 11:35:25 -0400 Subject: [PATCH] Revert "Add overloads for sum (#264) (#8)" This reverts commit 960cb766bb7c786c67c16d4e092b25d9e24f8271. --- source/mir/math/numeric.d | 26 ++++++++++++---- source/mir/math/stat.d | 11 ++++--- source/mir/math/sum.d | 64 +++------------------------------------ 3 files changed, 31 insertions(+), 70 deletions(-) diff --git a/source/mir/math/numeric.d b/source/mir/math/numeric.d index 767eed0f..d4e3ec94 100644 --- a/source/mir/math/numeric.d +++ b/source/mir/math/numeric.d @@ -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); } @@ -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 diff --git a/source/mir/math/stat.d b/source/mir/math/stat.d index d3ed7ca5..f85d9a9c 100644 --- a/source/mir/math/stat.d +++ b/source/mir/math/stat.d @@ -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 @@ -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); } } diff --git a/source/mir/math/sum.d b/source/mir/math/sum.d index 7d3ade31..9e9050ff 100644 --- a/source/mir/math/sum.d +++ b/source/mir/math/sum.d @@ -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) @@ -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 @@ -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 @@ -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) @@ -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;