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

            MatrixHelpers.ForceHermitian(A);
            var ACopy = A.Clone();
            var evd   = A.Evd(Symmetricity.Hermitian);

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

            var BCopy = B.Clone();

            var X = new DenseMatrix(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, 1);

            // Make sure A/B didn't change.
            AssertHelpers.AlmostEqual(ACopy, A, 14);
            AssertHelpers.AlmostEqual(BCopy, B, 14);
        }
Example #2
0
        public void CanSolveForRandomVectorAndSymmetricMatrixWhenResultVectorGiven([Values(1, 2, 5, 10, 50, 100)] int order)
        {
            var A = Matrix <Complex32> .Build.RandomPositiveDefinite(order, 1);

            MatrixHelpers.ForceHermitian(A);
            var ACopy = A.Clone();
            var evd   = A.Evd(Symmetricity.Hermitian);

            var b = Vector <Complex32> .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, 2);

            // Make sure A/B didn't change.
            AssertHelpers.AlmostEqual(ACopy, A, 14);
            AssertHelpers.AlmostEqual(bCopy, b, 14);
        }
Example #3
0
        public void CanFactorizeRandomSymmetricMatrix([Values(1, 2, 5, 10, 50, 100)] int order)
        {
            var A = Matrix <Complex32> .Build.RandomPositiveDefinite(order, 1);

            MatrixHelpers.ForceHermitian(A);
            var factorEvd = A.Evd(Symmetricity.Hermitian);
            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.ConjugateTranspose();

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

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

            var b     = new UserDefinedVector(Vector <Complex32> .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, 2);

            // Make sure A/B didn't change.
            AssertHelpers.AlmostEqual(ACopy, A, 14);
            AssertHelpers.AlmostEqual(bCopy, b, 14);
        }