コード例 #1
0
        internal static void CheckNullSpace(IMatrixView unfactorizedMatrix, IReadOnlyList <double[]> nullSpaceBasis,
                                            double tolerance)
        {
            var comparer = new MatrixComparer(tolerance);

            // Check that each vector belongs to the nullspace
            int order = unfactorizedMatrix.NumColumns;
            //var zeroVector = Vector.CreateZero(order);
            int nullity         = nullSpaceBasis.Count;
            var nullSpaceMatrix = Matrix.CreateZero(order, nullity);

            for (int j = 0; j < nullity; ++j)
            {
                var x = Vector.CreateFromArray(nullSpaceBasis[j]);
                nullSpaceMatrix.SetSubcolumn(j, x);

                // Check that each vector belongs to the nullspace
                IVector Ax     = unfactorizedMatrix.Multiply(x);
                double  normAx = Ax.Norm2() / Ax.Length;
                //comparer.AssertEqual(0.0, normAx);
            }

            // Check that the vectors are independent.
            // TODO: perhaps this should be included in the LinearAlgebra project, not just for tests.
            (Matrix rref, List <int> independentCols) = nullSpaceMatrix.ReducedRowEchelonForm(tolerance);
            for (int j = 0; j < nullity; ++j)
            {
                Assert.Contains(j, independentCols);
            }
        }
コード例 #2
0
        public void Test_Matrix_Transformation_with_5X5_Matrix()
        {
            // prepare the input/test data
            int[,] source = new int[, ] {
                { 10, 20, 30, 40, 5 },
                { 20, 30, 40, 50, 7 },
                { 20, 30, 40, 50, 8 },
                { 20, 30, 40, 50, 0 },
                { 100, 200, 300, 400, 500 }
            };

            // define the expected result
            var expected = new int[, ] {
                { 100, 20, 20, 20, 10 },
                { 200, 30, 30, 30, 20 },
                { 300, 40, 40, 40, 30 },
                { 400, 50, 50, 50, 40 },
                { 500, 0, 8, 7, 5 }
            };

            // do the test
            MatrixTransformations.Transform90DegreeInPlace(source);

            // validate/verify the output of the method with the expected output
            Assert.IsTrue(MatrixComparer.AreSame(source, expected));
        }
コード例 #3
0
        public void TestDropZeros(int size)
        {
            var A = MatrixLoader.A(size);

            double threshold = 1.0e-5;

            var B = A.Clone() as SparseMatrix;

            foreach (var item in A.EnumerateIndexed(Zeros.AllowSkip))
            {
                int i = item.Item1;
                int j = item.Item2;

                if (i != j)
                {
                    // Make all off-diagonal entries small.
                    B[i, j] = threshold / 10;
                }
            }

            var C = B.Clone() as SparseMatrix;

            B.CoerceZero(threshold);
            C.DropZeros(threshold);

            Assert.IsTrue(MatrixComparer.Equals(B, C, EPS));
        }
コード例 #4
0
            public override BenchmarkResult Run(BenchmarkSetup config)
            {
                int    rows      = config.RowCount;
                int    cols      = config.ColumnCount;
                int    repeat    = config.Repeat;
                double density   = config.Density;
                bool   symmetric = config.Symmetric;

                var A = Create.SparseMatrix(rows, cols, density, symmetric);

                var result = new BenchmarkResult();

                var a = DenseVector.Create(rows, (i) => 1.0 + i);
                var D = DiagonalMatrix.OfDiagonal(rows, cols, a);
                var C = (SparseMatrix)A.Clone();

                Util.Tic();
                var B = A.Add(D);

                result.Time1 = Util.Toc();

                Util.Tic();
                C.FastAddDiagonalMatrix(Util.GetData(a), C);
                result.Time2 = Util.Toc();

                result.Success = MatrixComparer.Equals(B, C, EPS);

                return(result);
            }
コード例 #5
0
            public override BenchmarkResult Run(BenchmarkSetup config)
            {
                int    rows      = config.RowCount;
                int    cols      = config.ColumnCount;
                int    repeat    = config.Repeat;
                double density   = config.Density;
                bool   symmetric = config.Symmetric;

                var A = Create.SparseMatrix(rows, cols, density, symmetric);

                var temp = A.RowNorms(1);
                var a    = DenseVector.Create(cols, i => temp[i]);
                var b    = Util.GetData(a);
                var D    = SparseMatrix.OfDiagonalVector(a);

                var result = new BenchmarkResult();

                Util.Tic();
                var B = A.Multiply(D);

                result.Time1 = Util.Toc();

                Util.Tic();
                A.FastScaleColumns(b, A);
                result.Time2 = Util.Toc();

                result.Success = MatrixComparer.Equals(A, B, EPS);

                return(result);
            }
コード例 #6
0
            public override BenchmarkResult Run(BenchmarkSetup config)
            {
                int    rows      = config.RowCount;
                int    cols      = config.ColumnCount;
                int    repeat    = config.Repeat;
                double density   = config.Density;
                bool   symmetric = config.Symmetric;

                var A = Create.SparseMatrix(rows, cols, density, symmetric);
                var S = CreateSparse.RandomSymmetric(4, 0.5);

                var result = new BenchmarkResult();

                Util.Tic();
                var B = S.FastKroneckerProduct(A);

                result.Time2 = Util.Toc();

                Util.Tic();
                var C = S.KroneckerProduct(A);

                result.Time1 = Util.Toc();

                result.Success = MatrixComparer.Equals(B, C, EPS);

                return(result);
            }
コード例 #7
0
            public override BenchmarkResult Run(BenchmarkSetup config)
            {
                int    rows      = config.RowCount;
                int    cols      = config.ColumnCount;
                int    repeat    = config.Repeat;
                double density   = config.Density;
                bool   symmetric = config.Symmetric;

                var A = Create.SparseMatrix(rows, cols, density, symmetric);
                var B = Create.SparseMatrix(rows, cols, density, symmetric);

                var result = new BenchmarkResult();

                Util.Tic();
                var C = A.FastAdd(B);

                result.Time2 = Util.Toc();

                Util.Tic();
                //var D = A.Add(B);
                var D = A + B;

                result.Time1 = Util.Toc();

                result.Success = MatrixComparer.Equals(C, D, EPS);

                return(result);
            }
コード例 #8
0
        public void NotSameSizedMatricesTest()
        {
            var matrix1 = new Matrix(100, 1);
            var matrix2 = new Matrix(101, 1);

            Assert.IsFalse(MatrixComparer.Compare(matrix1, matrix2));
        }
コード例 #9
0
        public void TestZeroSettingWhenMatrixHasOneZero()
        {
            int[,] input = new int[3, 3]
            {
                {
                    1, 0, 3
                },
                {
                    1, 2, 3
                },
                {
                    1, 2, 3
                }
            };

            int[,] expected = new int[3, 3]
            {
                {
                    0, 0, 0
                },
                {
                    1, 0, 3
                },
                {
                    1, 0, 3
                }
            };

            ZeroColumnRowSetter.SetZeroValue(input);

            bool isEqual = MatrixComparer.Are2DEqual(input, expected);

            Assert.IsTrue(isEqual);
        }
コード例 #10
0
            public override BenchmarkResult Run(BenchmarkSetup config)
            {
                int    rows      = config.RowCount;
                int    cols      = config.ColumnCount;
                int    repeat    = config.Repeat;
                double density   = config.Density;
                bool   symmetric = config.Symmetric;

                var A = Create.SparseMatrix(rows, cols, density, symmetric);

                var p = Util.Permutation(A.RowCount, rand);

                var B = (SparseMatrix)A.Clone();
                var C = (SparseMatrix)A.Clone();

                var result = new BenchmarkResult();

                Util.Tic();
                C.FastPermuteColumns(p);
                result.Time2 = Util.Toc();

                Util.Tic();
                B.PermuteColumns(new Permutation(p));
                result.Time1 = Util.Toc();

                result.Success = MatrixComparer.Equals(B, C, EPS);

                return(result);
            }
コード例 #11
0
        public void BigNotEqualMatricesTest()
        {
            var matrix1 = new Matrix(1000, 1000, 1);
            var matrix2 = new Matrix(1000, 1000, 1);

            matrix2[900, 34] = -1;
            Assert.IsFalse(MatrixComparer.Compare(matrix1, matrix2));
        }
コード例 #12
0
        public void EmptyMatricesTest()
        {
            var matrix1 = new Matrix(new int[0, 0] {
            });
            var matrix2 = new Matrix(new int[0, 0] {
            });

            Assert.IsTrue(MatrixComparer.Compare(matrix2, matrix1));
        }
コード例 #13
0
        public void EqualMatricesTest()
        {
            var matrix1 = new Matrix(new int[, ] {
                { 1, 2, 3 },
                { 4, 5, 6 }
            });

            Assert.IsTrue(MatrixComparer.Compare(matrix1, matrix1));
        }
コード例 #14
0
        public void NotRandomGenerationTest()
        {
            var matrix1 = new Matrix(2, 2, 2);
            var matrix2 = new Matrix(new int[, ] {
                { 2, 2 },
                { 2, 2 }
            });

            Assert.IsTrue(MatrixComparer.Compare(matrix1, matrix2));
        }
コード例 #15
0
        public void TestAdd(int size)
        {
            var A = MatrixLoader.A(size);
            var B = MatrixLoader.B(size, false);

            var C = A.Add(B);
            var D = A.FastAdd(B);

            Assert.IsTrue(MatrixComparer.Equals(C, D, EPS));
        }
コード例 #16
0
        public void TestNormalizeColumns(int size)
        {
            var A = MatrixLoader.A(size);

            var B = A.NormalizeColumns(1);

            A.FastNormalizeColumns(1);

            Assert.IsTrue(MatrixComparer.Equals(A, B, EPS));
        }
コード例 #17
0
        public void TestSpiralTraversalWithMatrix_0_X_0()
        {
            int[,] inputArray = new int[, ] {
            };

            int[] expectedResult = new int[] { };

            var actualResult = ArrayTraversals.TraverseArrayInSpiralOrder(inputArray);

            Assert.IsTrue(MatrixComparer.AreSame(expectedResult, actualResult));
        }
コード例 #18
0
        private void ArrayAntiDiagonalOrderTestMethod(int[,] inputArray, IList <int[]> expectedResult)
        {
            var actualResult = ArrayTraversals.TraverseArrayInAntiDiagonalOrder(inputArray);

            Assert.AreEqual(actualResult.Count, expectedResult.Count);

            for (int i = 0; i < expectedResult.Count; i++)
            {
                Assert.IsTrue(MatrixComparer.AreSame(expectedResult[i], actualResult[i]));
            }
        }
コード例 #19
0
        public void TestUpperTriangle(int size)
        {
            var A = MatrixLoader.A(size);

            var B = A.UpperTriangle() as SparseMatrix;
            var C = A.Clone() as SparseMatrix;

            C.FastUpperTriangle();

            Assert.IsTrue(MatrixComparer.Equals(B, C, EPS));
        }
コード例 #20
0
        public void TestTranspose(int size)
        {
            var A = MatrixLoader.A(size);

            var B = A.Transpose();
            var C = CreateSparse.Zeros(A.ColumnCount, A.RowCount, A.NonZerosCount);

            A.FastTranspose(C);

            Assert.IsTrue(MatrixComparer.Equals(B, C, EPS));
        }
コード例 #21
0
        public void WorkWithFileTest()
        {
            var matrix1 = new Matrix(new int[, ] {
                { 1, 2, 3 },
                { 1, 2, 3 }
            });

            matrix1.WriteToFile("Matrix1.txt");
            var matrix2 = new Matrix("Matrix1.txt");

            Assert.IsTrue(MatrixComparer.Compare(matrix1, matrix2));
        }
コード例 #22
0
        public void BigMatricesMultiplicationTest()
        {
            const int size    = 100;
            var       matrix1 = new Matrix(size, size, 1);
            var       matrix2 = new Matrix(size, size, 2);
            var       matrix3 = new Matrix(size, size, size * 2);
            var       matrix4 = MatrixMultiplier.MultiplyConcurrently(matrix1, matrix2);
            var       matrix5 = MatrixMultiplier.MultiplySequentally(matrix1, matrix2);

            Assert.IsTrue(MatrixComparer.Compare(matrix4, matrix3));
            Assert.IsTrue(MatrixComparer.Compare(matrix5, matrix3));
        }
コード例 #23
0
        public void TestKroneckerProduct(int size)
        {
            var A = MatrixLoader.A(size);

            var E = DenseMatrix.OfColumnMajor(2, 2, new[] { 1.0, 1.0, 1.0, 1.0 });
            var S = SparseMatrix.OfMatrix(E);

            var B = S.KroneckerProduct(A);
            var C = S.FastKroneckerProduct(A);

            Assert.IsTrue(MatrixComparer.Equals(B, C, EPS));
        }
コード例 #24
0
 private static void TestIndefiniteSystem(LinearAlgebraProviderChoice providers)
 {
     TestSettings.RunMultiproviderTest(providers, delegate()
     {
         var comparer             = new MatrixComparer(1E-4);
         double residualTolerance = 1e-8;
         (Matrix A, Vector b, Vector xExpected, IPreconditioner M) = DiagonalIndefinite.BuildIndefiniteSystem(20);
         var minres = new MinRes(A.NumRows, residualTolerance, 0, false, false);
         (IVector xComputed, MinresStatistics stats) = minres.Solve(A, b);
         comparer.AssertEqual(xExpected, xComputed);
     });
 }
コード例 #25
0
        public void TestInvertLowerTriangle()
        {
            var A = DenseMatrix.Build.Random(N, N, RAND_SEED);

            var L = (DenseMatrix)A.LowerTriangle();
            var S = (DenseMatrix)L.Inverse();

            L.InvertLowerTriangle();

            // Shouldn't use random matrix: can be ill-conditioned.
            Assert.IsTrue(MatrixComparer.Equals(S, L, 1000 * EPS));
        }
コード例 #26
0
        public void TestSpiralTraversalWithMatrix_5_X_1()
        {
            int[,] inputArray = new int[, ] {
                { 1 }, { 5 }, { 6 }, { 7 }, { 10 }
            };

            int[] expectedResult = new int[] { 1, 5, 6, 7, 10 };

            var actualResult = ArrayTraversals.TraverseArrayInSpiralOrder(inputArray);

            Assert.IsTrue(MatrixComparer.AreSame(expectedResult, actualResult));
        }
コード例 #27
0
        public void EmptyMatricesTest()
        {
            var matrix1 = new Matrix(new int[0, 0] {
            });
            var matrix2 = new Matrix(new int[0, 0] {
            });
            var matrix3 = MatrixMultiplier.MultiplyConcurrently(matrix1, matrix2);

            Assert.IsTrue(MatrixComparer.Compare(matrix3, matrix1));
            var matrix4 = MatrixMultiplier.MultiplySequentally(matrix1, matrix2);

            Assert.IsTrue(MatrixComparer.Compare(matrix4, matrix1));
        }
コード例 #28
0
        public void NotEqualMatricesTest()
        {
            var matrix1 = new Matrix(new int[, ] {
                { 1, 2, 3 },
                { 4, 5, 6 }
            });
            var matrix2 = new Matrix(new int[, ] {
                { 1, 2, 3 },
                { 4, -5, 6 }
            });

            Assert.IsFalse(MatrixComparer.Compare(matrix1, matrix2));
        }
コード例 #29
0
 private static void TestPosDefSparseSystem(LinearAlgebraProviderChoice providers)
 {
     TestSettings.RunMultiproviderTest(providers, delegate()
     {
         var comparer  = new MatrixComparer(1E-6);
         int n         = SparsePosDef10by10.Order;
         var A         = Matrix.CreateFromArray(SparsePosDef10by10.Matrix);
         var b         = Vector.CreateFromArray(SparsePosDef10by10.Rhs);
         var xExpected = Vector.CreateFromArray(SparsePosDef10by10.Lhs);
         var minres    = new MinRes(n, 1e-10, 0, false, false);
         (IVector xComputed, MinresStatistics stats) = minres.Solve(A, b);
         comparer.AssertEqual(xExpected, xComputed);
     });
 }
コード例 #30
0
        public void TestPermuteRows(int size)
        {
            var A = MatrixLoader.A(size);

            var p = Util.Permutation(A.RowCount, RAND_SEED);

            var B = A.Clone() as SparseMatrix;
            var C = A.Clone() as SparseMatrix;

            B.PermuteRows(new Permutation(p));
            C.FastPermuteRows(p);

            Assert.IsTrue(MatrixComparer.Equals(B, C, EPS));
        }