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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.dub
dub.selections.json
__*
*.exe
*.exe
dstats-test-library
20 changes: 10 additions & 10 deletions source/dstats/alloc.d
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public:
void popFront()
in {
assert(!empty);
} body {
} do {
this._length--;
if(next is null) {
do {
Expand Down Expand Up @@ -149,14 +149,14 @@ public:
@property ref Unqual!(K) front()
in {
assert(!empty);
} body {
} do {
return *frontElem;
}
} else {
@property Unqual!(K) front()
in {
assert(!empty);
} body {
} do {
return *frontElem;
}
}
Expand Down Expand Up @@ -911,7 +911,7 @@ struct AVLNodeBitwise(T) {
void left(typeof(this)* newLeft) nothrow @property
in {
assert((cast(size_t) newLeft & mask) == 0);
} body {
} do {
_left &= mask;
_left |= cast(size_t) newLeft;
assert(left is newLeft);
Expand All @@ -928,7 +928,7 @@ struct AVLNodeBitwise(T) {
void right(typeof(this)* newRight) nothrow @property
in {
assert((cast(size_t) newRight & mask) == 0);
} body {
} do {
_right &= mask;
_right |= cast(size_t) newRight;
assert(right is newRight);
Expand Down Expand Up @@ -1054,7 +1054,7 @@ private:
assert(node.left !is null);
assert( abs(node.balance) <= 2);

} body {
} do {
Node* newHead = node.left;
node.left = newHead.right;
newHead.right = node;
Expand All @@ -1070,7 +1070,7 @@ private:
in {
assert(node.right !is null);
assert( abs(node.balance) <= 2);
} body {
} do {
Node* newHead = node.right;
node.right = newHead.left;
newHead.left = node;
Expand All @@ -1087,7 +1087,7 @@ private:
assert(node is null || abs(node.balance) <= 2);
} out(ret) {
assert( abs(ret.balance) < 2);
} body {
} do {
if(node is null) {
return null;
}
Expand Down Expand Up @@ -1144,7 +1144,7 @@ private:
in {
assert(freeList);
assert(*freeList);
} body {
} do {
auto ret = *freeList;
*freeList = ret.right;
return ret;
Expand All @@ -1153,7 +1153,7 @@ private:
Node* newNode(T payload)
in {
assert(freeList, "Uninitialized StackTree!(" ~ T.stringof ~ ")");
} body {
} do {
Node* ret;
if(*freeList !is null) {
ret = popFreeList();
Expand Down
83 changes: 67 additions & 16 deletions source/dstats/base.d
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,57 @@ import std.math, std.mathspecial, std.traits, std.typecons, std.algorithm,

import dstats.alloc, dstats.sort;

template FloatingPointBaseType(T)
{
import std.range.primitives : ElementType;
static if (isFloatingPoint!T)
{
alias FloatingPointBaseType = Unqual!T;
}
else static if (isFloatingPoint!(ElementType!(Unqual!T)))
{
alias FloatingPointBaseType = Unqual!(ElementType!(Unqual!T));
}
else
{
alias FloatingPointBaseType = real;
}
}

template CommonDefaultFor(T,U)
{
import std.algorithm.comparison : min;
alias baseT = FloatingPointBaseType!T;
alias baseU = FloatingPointBaseType!U;
enum CommonType!(baseT, baseU) CommonDefaultFor = 10.0L ^^ -((min(baseT.dig, baseU.dig) + 1) / 2 + 1);
}

version(unittest) {
import std.stdio, std.random;
bool isClose2(T, U, V)(T lhs, U rhs,
V maxRelDiff = CommonDefaultFor!(T,U),
V maxAbsDiff = 0.0,
string file = __FILE__,
uint line = __LINE__)
{
enum show = false;
const result = std.math.isClose(lhs, rhs, maxRelDiff, maxAbsDiff);
if (!result)
{
static if (isInputRange!T)
writefln(file ~ "(" ~ line.to!string ~ ",1): Debug: [%(%.13e, %)]", lhs);
else
{
if (abs(lhs) < 0.1)
writefln(file ~ "(" ~ line.to!string ~ ",1): Debug: %.13e", lhs);
else
writefln(file ~ "(" ~ line.to!string ~ ",1): Debug: %.13f", lhs);
}
}
return true;
}
}

// Returns the number of dimensions in an array T.
package template nDims(T)
{
Expand Down Expand Up @@ -530,7 +581,7 @@ private void averageTies(T, U)(T sortedInput, U[] ranks, size_t[] perms)
in {
assert(sortedInput.length == ranks.length);
assert(ranks.length == perms.length);
} body {
} do {
size_t tieCount = 1;
foreach(i; 1..ranks.length) {
if(sortedInput[i] == sortedInput[i - 1]) {
Expand Down Expand Up @@ -811,8 +862,8 @@ unittest {
// Values worked out by hand on paper. If you don't believe me, work
// them out yourself.
assert(auroc([4,5,6], [1,2,3]) == 1);
assert(approxEqual(auroc([8,6,7,5,3,0,9], [3,6,2,4,3,6]), 0.6904762));
assert(approxEqual(auroc([2,7,1,8,2,8,1,8], [3,1,4,1,5,9,2,6]), 0.546875));
assert(isClose(auroc([8,6,7,5,3,0,9], [3,6,2,4,3,6]), 0.690476190));
assert(isClose(auroc([2,7,1,8,2,8,1,8], [3,1,4,1,5,9,2,6]), 0.546875));
}

///
Expand Down Expand Up @@ -848,15 +899,15 @@ unittest {
assert(cast(uint) round(exp(logFactorial(7)))==5040);
assert(cast(uint) round(exp(logFactorial(3)))==6);
// Gamma branch.
assert(approxEqual(logFactorial(12000), 1.007175584216837e5, 1e-14));
assert(approxEqual(logFactorial(14000), 1.196610688711534e5, 1e-14));
assert(isClose(logFactorial(12000), 1.007175584216837e5, 1e-14));
assert(isClose(logFactorial(14000), 1.196610688711534e5, 1e-14));
}

///Log of (n choose k).
double logNcomb(ulong n, ulong k)
in {
assert(k <= n);
} body {
} do {
if(n < k) return -double.infinity;
//Extra parentheses increase numerical accuracy.
return logFactorial(n) - (logFactorial(k) + logFactorial(n - k));
Expand Down Expand Up @@ -1220,7 +1271,7 @@ public:
this(uint n, uint r)
in {
assert(n >= r);
} body {
} do {
if(r > 0) {
pos = (seq(0U, r)).ptr;
pos[r - 1]--;
Expand Down Expand Up @@ -1481,13 +1532,13 @@ unittest {
scope(exit) std.file.remove("NumericFileTestDeleteMe.txt");
auto myFile = File("NumericFileTestDeleteMe.txt");
auto rng = toNumericRange(myFile.byLine());
assert(approxEqual(rng.front, 3.14));
assert(isClose(rng.front, 3.14));
rng.popFront;
assert(approxEqual(rng.front, 2.71));
assert(isClose(rng.front, 2.71));
rng.popFront;
assert(approxEqual(rng.front, 8.67));
assert(isClose(rng.front, 8.67));
rng.popFront;
assert(approxEqual(rng.front, 362435));
assert(isClose(rng.front, 362436));
assert(!rng.empty);
rng.popFront;
assert(rng.empty);
Expand Down Expand Up @@ -1622,9 +1673,9 @@ version(scid) {
auto mat = [[1.0, 2, 3], [4.0, 7, 6], [7.0, 8, 9]];
auto toMat = [new double[3], new double[3], new double[3]];
invert(mat, toMat);
assert(approxEqual(toMat[0], [-0.625, -0.25, 0.375]));
assert(approxEqual(toMat[1], [-0.25, 0.5, -0.25]));
assert(approxEqual(toMat[2], [0.708333, -0.25, 0.041667]));
assert(isClose(toMat[0], [-0.625, -0.25, 0.375]));
assert(isClose(toMat[1], [-0.25, 0.5, -0.25]));
assert(isClose(toMat[2], [0.70833333333, -0.25000000000, 0.04166666667]));
}

void solve(DoubleMatrix mat, double[] vec) {
Expand Down Expand Up @@ -1685,12 +1736,12 @@ version(scid) {
auto mat = [[2.0, 1, -1], [-3.0, -1, 2], [-2.0, 1, 2]];
auto vec = [8.0, -11, -3];
solve(mat, vec);
assert(approxEqual(vec, [2, 3, -1]));
assert(isClose(vec, [2, 3, -1]));

auto mat2 = [[1.0, 2, 3], [4.0, 7, 6], [7.0, 8, 9]];
auto vec2 = [8.0, 6, 7];
solve(mat2, vec2);
assert(approxEqual(vec2, [-3.875, -0.75, 4.45833]));
assert(isClose(vec2, [-3.875, -0.75, 4.45833333333]));
}

// Cholesky decomposition functions adapted from Don Clugston's MathExtra
Expand Down
Loading