Skip to content
Open
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
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ sources_list = [
'mir/math/func/normal',
'mir/math/numeric',
'mir/math/stat',
'mir/math/internal/stat_type',
'mir/math/sum',
'mir/ndslice/allocation',
'mir/ndslice/chunks',
Expand Down
152 changes: 152 additions & 0 deletions source/mir/math/internal/stat_type.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/++ Internal result-type selection shared by statistical and product algorithms.
License: Apache-2.0
Authors: Shigeki Karita, Ilia Ki, John Michael Hall
Copyright: 2020 Ilia Ki, Kaleidic Associates Advisory Limited, Symmetry Investments
+/
module mir.math.internal.stat_type;

import std.traits: Unqual;

///
package(mir)
template statType(T, bool checkComplex = true)
{
import mir.internal.utility: isFloatingPoint;

static if (isFloatingPoint!T) {
import std.traits: Unqual;
alias statType = Unqual!T;
} else static if (is(T : double)) {
alias statType = double;
} else static if (checkComplex) {
import mir.internal.utility: isComplex;
static if (isComplex!T) {
static if (__traits(getAliasThis, T).length == 1)
{
alias statType = .statType!(typeof(__traits(getMember, T, __traits(getAliasThis, T)[0])));
}
else
{
alias statType = Unqual!T;
}
} else {
static assert(0, "statType: type " ~ T.stringof ~ " must be convertible to a complex floating point type");
}
} else {
static assert(0, "statType: type " ~ T.stringof ~ " must be convertible to a floating point type");
}
}

version(mir_test)
@safe pure nothrow @nogc
unittest
{
static assert(is(statType!int == double));
static assert(is(statType!uint == double));
static assert(is(statType!double == double));
static assert(is(statType!float == float));
static assert(is(statType!real == real));

static assert(is(statType!(const(int)) == double));
static assert(is(statType!(immutable(int)) == double));
static assert(is(statType!(const(double)) == double));
static assert(is(statType!(immutable(double)) == double));
}

version(mir_test)
@safe pure nothrow @nogc
unittest
{
import mir.complex: Complex;

static assert(is(statType!(Complex!float) == Complex!float));
static assert(is(statType!(Complex!double) == Complex!double));
static assert(is(statType!(Complex!real) == Complex!real));
}

version(mir_test)
@safe pure nothrow @nogc
unittest
{
static struct Foo {
float x;
alias x this;
}

static assert(is(statType!Foo == double)); // note: this is not float
}

version(mir_test)
@safe pure nothrow @nogc
unittest
{
import mir.complex;
static struct Foo {
Complex!float x;
alias x this;
}

static assert(is(statType!Foo == Complex!float));
}

version(mir_test)
@safe pure nothrow @nogc
unittest
{
static struct Foo {
double x;
alias x this;
}

static assert(is(statType!Foo == double));
}

version(mir_test)
@safe pure nothrow @nogc
unittest
{
import mir.complex;
static struct Foo {
Complex!double x;
alias x this;
}

static assert(is(statType!Foo == Complex!double));
}

version(mir_test)
@safe pure nothrow @nogc
unittest
{
static struct Foo {
real x;
alias x this;
}

static assert(is(statType!Foo == double)); // note: this is not real
}

version(mir_test)
@safe pure nothrow @nogc
unittest
{
import mir.complex;
static struct Foo {
Complex!real x;
alias x this;
}

static assert(is(statType!Foo == Complex!real));
}

version(mir_test)
@safe pure nothrow @nogc
unittest
{
static struct Foo {
int x;
alias x this;
}

static assert(is(statType!Foo == double)); // note: this is not ints
}
2 changes: 1 addition & 1 deletion source/mir/math/numeric.d
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ package template prodType(T)
auto temp = U.init * U.init;
temp *= U.init;
})) {
import mir.math.stat: statType;
import mir.math.internal.stat_type: statType;

alias V = typeof(U.init * U.init);
alias prodType = statType!(V, false);
Expand Down
Loading
Loading