public void Fold2ForceIncludeZeros(TestMatrixStorage aType, TestMatrixStorage bType)
 {
     var a = TestData.MatrixStorage(aType, new[,] { {1.0, 2.0, 0.0}, {4.0, 0.0, 6.0} });
     var b = TestData.MatrixStorage(bType, new[,] { {11.0, 12.0, 13.0}, {0.0, 0.0, 16.0} });
     var result = a.Fold2(b, (acc, u, v) => acc + u + v + 1.0, 0.0, Zeros.Include);
     Assert.That(result, Is.EqualTo(71));
 }
 public void Fold2SkipZeros(TestMatrixStorage aType, TestMatrixStorage bType)
 {
     var a = TestData.MatrixStorage(aType, new[,] { {1.0, 2.0, 0.0}, {4.0, 0.0, 6.0} });
     var b = TestData.MatrixStorage(bType, new[,] { {11.0, 12.0, 13.0}, {0.0, 0.0, 16.0} });
     var result = a.Fold2(b, (acc, u, v) => acc + u + v, 0.0, Zeros.AllowSkip);
     Assert.That(result, Is.EqualTo(65));
 }
        public void TestMatrix()
        {
            var gridStorage = new TestMatrixStorage(new Vector2Int(5, 5));

            Assert.AreEqual(5, gridStorage.Width);
            Assert.AreEqual(5, gridStorage.Height);
            Assert.AreEqual(5, gridStorage.NumRows(), "Num Rows Check");
            Assert.AreEqual(5, gridStorage.NumColumns(), "Num Columns Check");

            gridStorage.Resize(new Vector2Int(4, 4));
            Assert.AreEqual(4, gridStorage.Width);
            Assert.AreEqual(4, gridStorage.Height);
            Assert.AreEqual(4, gridStorage.NumRows(), "Num Rows Check");
            Assert.AreEqual(4, gridStorage.NumColumns(), "Num Columns Check");

            gridStorage.Resize(new Vector2Int(6, 6));
            Assert.AreEqual(6, gridStorage.Width);
            Assert.AreEqual(6, gridStorage.Height);
            Assert.AreEqual(6, gridStorage.NumRows(), "Num Rows Check");
            Assert.AreEqual(6, gridStorage.NumColumns(), "Num Columns Check");

            Assert.IsFalse(gridStorage.IsValidLocation(new Vector2Int(-1, -1)));
            Assert.IsFalse(gridStorage.IsValidLocation(new Vector2Int(6, 0)));
            Assert.IsFalse(gridStorage.IsValidLocation(new Vector2Int(0, 6)));
            Assert.IsTrue(gridStorage.IsValidLocation(new Vector2Int(0, 0)));
            Assert.IsTrue(gridStorage.IsValidLocation(new Vector2Int(5, 5)));

            Assert.AreEqual(0, gridStorage.CellAt(new Vector2Int(0, 0)));
            gridStorage.SetCell(new Vector2Int(0, 0), 3);
            Assert.AreEqual(3, gridStorage.CellAt(new Vector2Int(0, 0)));
        }
 public void MapToAutoIncludeZeros(TestMatrixStorage aType, TestMatrixStorage resultType)
 {
     var a = TestData.MatrixStorage(aType, new[,] { { 1.0, 2.0 }, { 0.0, 4.0 } });
     var result = TestData.MatrixStorage<double>(resultType, 2, 2);
     var expected = DenseColumnMajorMatrixStorage<double>.OfArray(new[,] { {0.0, -1.0}, {1.0, -3.0} });
     a.MapTo(result, u => -u + 1.0, Zeros.AllowSkip);
     Assert.That(result.Equals(expected));
 }
 public void Map2ToAutoIncludeZeros(TestMatrixStorage aType, TestMatrixStorage bType, TestMatrixStorage resultType)
 {
     var a = TestData.MatrixStorage(aType, new[,] { {1.0, 2.0, 0.0}, {4.0, 0.0, 6.0} });
     var b = TestData.MatrixStorage(bType, new[,] { {11.0, 12.0, 13.0}, {0.0, 0.0, 16.0} });
     var result = TestData.MatrixStorage<double>(resultType, 2, 3);
     var expected = DenseColumnMajorMatrixStorage<double>.OfArray(new[,] { {13.0, 15.0, 14.0}, {5.0, 1.0, 23.0} });
     a.Map2To(result, b, (u, v) => u + v + 1.0, Zeros.AllowSkip, ExistingData.AssumeZeros);
     Assert.That(result.Equals(expected));
 }
 public void MapIndexedToSkipZeros(TestMatrixStorage aType, TestMatrixStorage resultType)
 {
     var a = TestData.MatrixStorage(aType, new[,] { { 1.0, 2.0 }, { 0.0, 4.0 } });
     var result = TestData.MatrixStorage<double>(resultType, 2, 2);
     var expected = DenseColumnMajorMatrixStorage<double>.OfArray(new[,] { {-1.0, -2.0}, {0.0, -4.0} });
     int badValueCount = 0; // one time is OK for zero-check
     a.MapIndexedTo(result, (i, j, u) => { if (a.At(i, j) != u) Interlocked.Increment(ref badValueCount); return -u; }, Zeros.AllowSkip);
     Assert.That(badValueCount, Is.LessThanOrEqualTo(1));
     Assert.That(result.Equals(expected));
 }
Exemple #7
0
        public void Fold2ForceIncludeZeros(TestMatrixStorage aType, TestMatrixStorage bType)
        {
            var a = TestData.MatrixStorage(aType, new[, ] {
                { 1.0, 2.0, 0.0 }, { 4.0, 0.0, 6.0 }
            });
            var b = TestData.MatrixStorage(bType, new[, ] {
                { 11.0, 12.0, 13.0 }, { 0.0, 0.0, 16.0 }
            });
            var result = a.Fold2(b, (acc, u, v) => acc + u + v + 1.0, 0.0, Zeros.Include);

            Assert.That(result, Is.EqualTo(71));
        }
Exemple #8
0
        public void Fold2SkipZeros(TestMatrixStorage aType, TestMatrixStorage bType)
        {
            var a = TestData.MatrixStorage(aType, new[, ] {
                { 1.0, 2.0, 0.0 }, { 4.0, 0.0, 6.0 }
            });
            var b = TestData.MatrixStorage(bType, new[, ] {
                { 11.0, 12.0, 13.0 }, { 0.0, 0.0, 16.0 }
            });
            var result = a.Fold2(b, (acc, u, v) => acc + u + v, 0.0, Zeros.AllowSkip);

            Assert.That(result, Is.EqualTo(65));
        }
Exemple #9
0
        public void MapToAutoIncludeZeros(TestMatrixStorage aType, TestMatrixStorage resultType)
        {
            var a = TestData.MatrixStorage(aType, new[, ] {
                { 1.0, 2.0 }, { 0.0, 4.0 }
            });
            var result   = TestData.MatrixStorage <double>(resultType, 2, 2);
            var expected = DenseColumnMajorMatrixStorage <double> .OfArray(new[, ] {
                { 0.0, -1.0 }, { 1.0, -3.0 }
            });

            a.MapTo(result, u => - u + 1.0, Zeros.AllowSkip);
            Assert.That(result.Equals(expected));
        }
Exemple #10
0
        public void MapToSkipZeros(TestMatrixStorage aType, TestMatrixStorage resultType)
        {
            var a = TestData.MatrixStorage(aType, new[, ] {
                { 1.0, 2.0 }, { 0.0, 4.0 }
            });
            var result   = TestData.MatrixStorage <double>(resultType, 2, 2);
            var expected = DenseColumnMajorMatrixStorage <double> .OfArray(new[, ] {
                { -1.0, -2.0 }, { 0.0, -4.0 }
            });

            a.MapTo(result, u => - u, Zeros.AllowSkip, ExistingData.Clear);
            Assert.That(result.Equals(expected));
        }
Exemple #11
0
        public void Map2ToAutoIncludeZeros(TestMatrixStorage aType, TestMatrixStorage bType, TestMatrixStorage resultType)
        {
            var a = TestData.MatrixStorage(aType, new[, ] {
                { 1.0, 2.0, 0.0 }, { 4.0, 0.0, 6.0 }
            });
            var b = TestData.MatrixStorage(bType, new[, ] {
                { 11.0, 12.0, 13.0 }, { 0.0, 0.0, 16.0 }
            });
            var result   = TestData.MatrixStorage <double>(resultType, 2, 3);
            var expected = DenseColumnMajorMatrixStorage <double> .OfArray(new[, ] {
                { 13.0, 15.0, 14.0 }, { 5.0, 1.0, 23.0 }
            });

            a.Map2To(result, b, (u, v) => u + v + 1.0, Zeros.AllowSkip, ExistingData.AssumeZeros);
            Assert.That(result.Equals(expected));
        }
Exemple #12
0
        public static MatrixStorage <T> MatrixStorage <T>(TestMatrixStorage type, int rows, int columns)
            where T : struct, IEquatable <T>, IFormattable
        {
            switch (type)
            {
            case TestMatrixStorage.DenseMatrix:
                return(new DenseColumnMajorMatrixStorage <T>(rows, columns));

            case TestMatrixStorage.SparseMatrix:
                return(new SparseCompressedRowMatrixStorage <T>(rows, columns));

            case TestMatrixStorage.DiagonalMatrix:
                return(new DiagonalMatrixStorage <T>(rows, columns));

            default:
                throw new NotSupportedException();
            }
        }
Exemple #13
0
        public static MatrixStorage <T> MatrixStorage <T>(TestMatrixStorage type, T[,] data)
            where T : struct, IEquatable <T>, IFormattable
        {
            switch (type)
            {
            case TestMatrixStorage.DenseMatrix:
                return(DenseColumnMajorMatrixStorage <T> .OfArray(data));

            case TestMatrixStorage.SparseMatrix:
                return(SparseCompressedRowMatrixStorage <T> .OfArray(data));

            case TestMatrixStorage.DiagonalMatrix:
                return(DiagonalMatrixStorage <T> .OfArray(data));

            default:
                throw new NotSupportedException();
            }
        }
Exemple #14
0
        public void MapIndexedToSkipZeros(TestMatrixStorage aType, TestMatrixStorage resultType)
        {
            var a = TestData.MatrixStorage(aType, new[, ] {
                { 1.0, 2.0 }, { 0.0, 4.0 }
            });
            var result   = TestData.MatrixStorage <double>(resultType, 2, 2);
            var expected = DenseColumnMajorMatrixStorage <double> .OfArray(new[, ] {
                { -1.0, -2.0 }, { 0.0, -4.0 }
            });

            int badValueCount = 0; // one time is OK for zero-check

            a.MapIndexedTo(result, (i, j, u) => { if (a.At(i, j) != u)
                                                  {
                                                      Interlocked.Increment(ref badValueCount);
                                                  }
                                                  return(-u); }, Zeros.AllowSkip);
            Assert.That(badValueCount, Is.LessThanOrEqualTo(1));
            Assert.That(result.Equals(expected));
        }