public void KLDTest() { var actual = new Matrix(4, 1); var expected = new Matrix(4, 1); actual.InRandomize(); expected.InRandomize(); var autoErr = new KLD().Evaluate(actual, expected); var error = 0.0; for (var i = 0; i < actual.Rows; i++) { for (var j = 0; j < actual.Columns; j++) { error += expected[i, j] * Math.Log(expected[i, j] / (actual[i, j] + double.Epsilon)); } } error /= actual.Rows * actual.Columns; Assert.IsTrue(Math.Abs(error - autoErr) < 0.01, new KLD().Type().ToString() + " Forward!"); var autoDErr = new KLD().Backward(actual, expected); var dErr = (expected * -1).HadamardDivision(actual + actual.Fill(double.Epsilon)); Assert.IsTrue(Math.Abs(dErr.FrobeniusNorm() - autoDErr.FrobeniusNorm()) < 0.01, new KLD().Type().ToString() + " Backward!"); }
public void ShouldSortArray_Three_Elements_Negative() { int[] toSort = { -2, -1, -3 }; int[] expected = { -3, -2, -1 }; int[] actual = (int[])toSort.Clone(); selection.Sort(ref actual); Assert.IsTrue(KLD.AreEqualArrays(expected, actual), $"\nFailed to sort: {KLD.ats(toSort)}.\nExpected: {KLD.ats(expected)} got: {KLD.ats(actual)}"); }
public void ShouldSortArray_Ten_Elements() { int[] toSort = { 6, 12, 30, 5, 1, 32, 9, 100, 3, 31 }; int[] expected = { 1, 3, 5, 6, 9, 12, 30, 31, 32, 100 }; int[] actual = (int[])toSort.Clone(); selection.Sort(ref actual); Assert.IsTrue(KLD.AreEqualArrays(expected, actual), $"\nFailed to sort: {KLD.ats(toSort)}.\nExpected: {KLD.ats(expected)} got: {KLD.ats(actual)}"); }
public void ShouldSortArray_Ten_Elements_Sorted_Reversed() { int[] expected = KLD.Generate(10); int[] toSort = expected.Reverse().ToArray(); int[] actual = (int[])toSort.Clone(); selection.Sort(ref actual); Assert.IsTrue(KLD.AreEqualArrays(expected, actual), $"\nFailed to sort: {KLD.ats(toSort)}.\nExpected: {KLD.ats(expected)} got: {KLD.ats(actual)}"); }
public void ShouldSortArray_One_Elements() { int[] expected = KLD.Generate(3); int[] toSort = KLD.ShuffleAndClone(expected); int[] actual = (int[])toSort.Clone(); selection.Sort(ref actual); Assert.IsTrue(KLD.AreEqualArrays(expected, actual), $"\nFailed to sort: {KLD.ats(toSort)}.\nExpected: {KLD.ats(expected)} got: {KLD.ats(actual)}"); }