Ejemplo n.º 1
0
        public void CanSolveForRandomMatrixAndSymmetricMatrixWhenResultMatrixGiven([Values(1, 2, 5, 10, 50, 100)] int order)
        {
            var A = Matrix <double> .Build.RandomPositiveDefinite(order, 1);

            MatrixHelpers.ForceSymmetric(A);
            var ACopy = A.Clone();
            var evd   = A.Evd();

            var B = Matrix <double> .Build.Random(order, order, 2);

            var BCopy = B.Clone();

            var X = Matrix <double> .Build.Dense(order, order);

            evd.Solve(B, X);

            // The solution X row dimension is equal to the column dimension of A
            Assert.AreEqual(A.ColumnCount, X.RowCount);

            // The solution X has the same number of columns as B
            Assert.AreEqual(B.ColumnCount, X.ColumnCount);

            var BReconstruct = A * X;

            // Check the reconstruction.
            AssertHelpers.AlmostEqual(B, BReconstruct, 8);

            // Make sure A/B didn't change.
            AssertHelpers.AlmostEqual(ACopy, A, 14);
            AssertHelpers.AlmostEqual(BCopy, B, 14);
        }
        public void CanSolveForRandomVectorAndSymmetricMatrix(int order)
        {
            var matrixA = MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order);

            MatrixHelpers.ForceSymmetric(matrixA);
            var matrixACopy = matrixA.Clone();
            var factorEvd   = matrixA.Evd();

            var vectorb = MatrixLoader.GenerateRandomDenseVector(order);
            var resultx = factorEvd.Solve(vectorb);

            Assert.AreEqual(matrixA.ColumnCount, resultx.Count);

            var matrixBReconstruct = matrixA * resultx;

            // Check the reconstruction.
            for (var i = 0; i < vectorb.Count; i++)
            {
                Assert.AreEqual(vectorb[i], matrixBReconstruct[i], 1e-3);
            }

            // Make sure A didn't change.
            for (var i = 0; i < matrixA.RowCount; i++)
            {
                for (var j = 0; j < matrixA.ColumnCount; j++)
                {
                    Assert.AreEqual(matrixACopy[i, j], matrixA[i, j]);
                }
            }
        }
Ejemplo n.º 3
0
        public void CanSolveForRandomVectorAndSymmetricMatrixWhenResultVectorGiven([Values(1, 2, 5, 10, 50, 100)] int order)
        {
            var A = Matrix <double> .Build.RandomPositiveDefinite(order, 1);

            MatrixHelpers.ForceSymmetric(A);
            var ACopy = A.Clone();
            var evd   = A.Evd();

            var b = Vector <double> .Build.Random(order, 2);

            var bCopy = b.Clone();

            var x = new DenseVector(order);

            evd.Solve(b, x);

            var bReconstruct = A * x;

            // Check the reconstruction.
            AssertHelpers.AlmostEqual(b, bReconstruct, 8);

            // Make sure A/B didn't change.
            AssertHelpers.AlmostEqual(ACopy, A, 14);
            AssertHelpers.AlmostEqual(bCopy, b, 14);
        }
        public void CanFactorizeRandomSymmetricMatrix([Values(1, 2, 5, 10, 50)] int order)
        {
            var matrixA = MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order);

            MatrixHelpers.ForceSymmetric(matrixA);
            var factorEvd    = matrixA.Evd();
            var eigenVectors = factorEvd.EigenVectors();
            var d            = factorEvd.D();

            Assert.AreEqual(order, eigenVectors.RowCount);
            Assert.AreEqual(order, eigenVectors.ColumnCount);

            Assert.AreEqual(order, d.RowCount);
            Assert.AreEqual(order, d.ColumnCount);

            // Make sure the A = V*λ*VT
            var matrix = eigenVectors * d * eigenVectors.Transpose();

            for (var i = 0; i < matrix.RowCount; i++)
            {
                for (var j = 0; j < matrix.ColumnCount; j++)
                {
                    Assert.AreEqual(matrix[i, j], matrixA[i, j], 1e-3);
                }
            }
        }
Ejemplo n.º 5
0
        public void CanFactorizeRandomSymmetricMatrix(int order)
        {
            var matrixA = Matrix <double> .Build.RandomPositiveDefinite(order, 1);

            MatrixHelpers.ForceSymmetric(matrixA);
            var factorEvd    = matrixA.Evd();
            var eigenVectors = factorEvd.EigenVectors;
            var d            = factorEvd.D;

            Assert.AreEqual(order, eigenVectors.RowCount);
            Assert.AreEqual(order, eigenVectors.ColumnCount);

            Assert.AreEqual(order, d.RowCount);
            Assert.AreEqual(order, d.ColumnCount);

            // Make sure the A = V*λ*VT
            var matrix = eigenVectors * d * eigenVectors.Transpose();

            for (var i = 0; i < matrix.RowCount; i++)
            {
                for (var j = 0; j < matrix.ColumnCount; j++)
                {
                    Assert.AreEqual(matrix[i, j], matrixA[i, j], 1.0e-10);
                }
            }
        }
Ejemplo n.º 6
0
        public void CanSolveForRandomMatrixAndSymmetricMatrixWhenResultMatrixGiven(int order)
        {
            var matrixA = Matrix <double> .Build.RandomPositiveDefinite(order, 1);

            MatrixHelpers.ForceSymmetric(matrixA);
            var matrixACopy = matrixA.Clone();
            var factorEvd   = matrixA.Evd();

            var matrixB = Matrix <double> .Build.Random(order, order, 1);

            var matrixBCopy = matrixB.Clone();

            var matrixX = new DenseMatrix(order, order);

            factorEvd.Solve(matrixB, matrixX);

            // The solution X row dimension is equal to the column dimension of A
            Assert.AreEqual(matrixA.ColumnCount, matrixX.RowCount);

            // The solution X has the same number of columns as B
            Assert.AreEqual(matrixB.ColumnCount, matrixX.ColumnCount);

            var matrixBReconstruct = matrixA * matrixX;

            // Check the reconstruction.
            for (var i = 0; i < matrixB.RowCount; i++)
            {
                for (var j = 0; j < matrixB.ColumnCount; j++)
                {
                    Assert.AreEqual(matrixB[i, j], matrixBReconstruct[i, j], 1.0e-10);
                }
            }

            // Make sure A didn't change.
            for (var i = 0; i < matrixA.RowCount; i++)
            {
                for (var j = 0; j < matrixA.ColumnCount; j++)
                {
                    Assert.AreEqual(matrixACopy[i, j], matrixA[i, j]);
                }
            }

            // Make sure B didn't change.
            for (var i = 0; i < matrixB.RowCount; i++)
            {
                for (var j = 0; j < matrixB.ColumnCount; j++)
                {
                    Assert.AreEqual(matrixBCopy[i, j], matrixB[i, j]);
                }
            }
        }
Ejemplo n.º 7
0
        public void CanFactorizeRandomSymmetricMatrix([Values(1, 2, 5, 10, 50, 100)] int order)
        {
            var A = Matrix <double> .Build.RandomPositiveDefinite(order, 1);

            MatrixHelpers.ForceSymmetric(A);
            var factorEvd = A.Evd();
            var V         = factorEvd.EigenVectors;
            var λ         = factorEvd.D;

            Assert.AreEqual(order, V.RowCount);
            Assert.AreEqual(order, V.ColumnCount);
            Assert.AreEqual(order, λ.RowCount);
            Assert.AreEqual(order, λ.ColumnCount);

            // Verify A = V*λ*VT
            var matrix = V * λ * V.Transpose();

            AssertHelpers.AlmostEqual(matrix, A, 10);
            AssertHelpers.AlmostEqualRelative(matrix, A, 9);
        }
Ejemplo n.º 8
0
        public void CanSolveForRandomVectorAndSymmetricMatrix([Values(1, 2, 5, 10, 50, 100)] int order)
        {
            var A = new UserDefinedMatrix(Matrix <double> .Build.RandomPositiveDefinite(order, 1).ToArray());

            MatrixHelpers.ForceSymmetric(A);
            var ACopy = A.Clone();
            var evd   = A.Evd();

            var b     = new UserDefinedVector(Vector <double> .Build.Random(order, 1).ToArray());
            var bCopy = b.Clone();

            var x = evd.Solve(b);

            var bReconstruct = A * x;

            // Check the reconstruction.
            AssertHelpers.AlmostEqual(b, bReconstruct, 8);

            // Make sure A/B didn't change.
            AssertHelpers.AlmostEqual(ACopy, A, 14);
            AssertHelpers.AlmostEqual(bCopy, b, 14);
        }
Ejemplo n.º 9
0
        public void CanSolveForRandomVectorAndSymmetricMatrixWhenResultVectorGiven(int order)
        {
            var matrixA = Matrix <double> .Build.RandomPositiveDefinite(order, 1);

            MatrixHelpers.ForceSymmetric(matrixA);
            var matrixACopy = matrixA.Clone();
            var factorEvd   = matrixA.Evd();
            var vectorb     = Vector <double> .Build.Random(order, 1);

            var vectorbCopy = vectorb.Clone();
            var resultx     = new DenseVector(order);

            factorEvd.Solve(vectorb, resultx);

            var matrixBReconstruct = matrixA * resultx;

            // Check the reconstruction.
            for (var i = 0; i < vectorb.Count; i++)
            {
                Assert.AreEqual(vectorb[i], matrixBReconstruct[i], 1.0e-10);
            }

            // Make sure A didn't change.
            for (var i = 0; i < matrixA.RowCount; i++)
            {
                for (var j = 0; j < matrixA.ColumnCount; j++)
                {
                    Assert.AreEqual(matrixACopy[i, j], matrixA[i, j]);
                }
            }

            // Make sure b didn't change.
            for (var i = 0; i < vectorb.Count; i++)
            {
                Assert.AreEqual(vectorbCopy[i], vectorb[i]);
            }
        }