public void CanSolveForRandomVectorAndSymmetricMatrix(int order) { var matrixA = MatrixLoader.GenerateRandomPositiveDefiniteHermitianDenseMatrix(order); MatrixHelpers.ForceConjugateSymmetric(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].Real, matrixBReconstruct[i].Real, 1e-3f); Assert.AreEqual(vectorb[i].Imaginary, matrixBReconstruct[i].Imaginary, 1e-3f); } // 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]); } } }
public void CanSolveForRandomMatrixAndSymmetricMatrixWhenResultMatrixGiven([Values(1, 2, 5, 10, 50, 100)] int order) { var A = new UserDefinedMatrix(Matrix <Complex> .Build.RandomPositiveDefinite(order, 1).ToArray()); MatrixHelpers.ForceConjugateSymmetric(A); var ACopy = A.Clone(); var evd = A.Evd(); var B = new UserDefinedMatrix(Matrix <Complex> .Build.Random(order, order, 1).ToArray()); var BCopy = B.Clone(); var X = new UserDefinedMatrix(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, 9); // 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)] int order) { var matrixA = MatrixLoader.GenerateRandomPositiveDefiniteHermitianDenseMatrix(order); MatrixHelpers.ForceConjugateSymmetric(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.ConjugateTranspose(); for (var i = 0; i < matrix.RowCount; i++) { for (var j = 0; j < matrix.ColumnCount; j++) { AssertHelpers.AlmostEqualRelative(matrix[i, j], matrixA[i, j], 3); } } }
public void CanSolveForRandomVectorAndSymmetricMatrix(int order) { var matrixA = Matrix <Complex> .Build.RandomPositiveDefinite(order, 1); MatrixHelpers.ForceConjugateSymmetric(matrixA); var matrixACopy = matrixA.Clone(); var factorEvd = matrixA.Evd(); var vectorb = Vector <Complex> .Build.Random(order, 1); 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++) { AssertHelpers.AlmostEqual(vectorb[i], matrixBReconstruct[i], 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]); } } }
public void CanSolveForRandomVectorAndSymmetricMatrixWhenResultVectorGiven([Values(1, 2, 5, 10, 50, 100)] int order) { var A = Matrix <Complex32> .Build.RandomPositiveDefinite(order, 1); MatrixHelpers.ForceConjugateSymmetric(A); var ACopy = A.Clone(); var evd = A.Evd(); 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); }
public void CanFactorizeRandomSymmetricMatrix(int order) { var matrixA = Matrix <Complex> .Build.RandomPositiveDefinite(order, 1); MatrixHelpers.ForceConjugateSymmetric(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.ConjugateTranspose(); for (var i = 0; i < matrix.RowCount; i++) { for (var j = 0; j < matrix.ColumnCount; j++) { AssertHelpers.AlmostEqualRelative(matrix[i, j], matrixA[i, j], 7); } } }
public void CanSolveForRandomMatrixAndSymmetricMatrixWhenResultMatrixGiven(int order) { var matrixA = Matrix <Complex32> .Build.RandomPositiveDefinite(order, 1); MatrixHelpers.ForceConjugateSymmetric(matrixA); var matrixACopy = matrixA.Clone(); var factorEvd = matrixA.Evd(); var matrixB = Matrix <Complex32> .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].Real, matrixBReconstruct[i, j].Real, 1e-2f); Assert.AreEqual(matrixB[i, j].Imaginary, matrixBReconstruct[i, j].Imaginary, 1e-2f); } } // 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]); } } }
public void CanFactorizeRandomSymmetricMatrix([Values(1, 2, 5, 10, 50, 100)] int order) { var A = Matrix <Complex32> .Build.RandomPositiveDefinite(order, 1); MatrixHelpers.ForceConjugateSymmetric(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.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 <Complex> .Build.RandomPositiveDefinite(order, 1).ToArray()); MatrixHelpers.ForceConjugateSymmetric(A); var ACopy = A.Clone(); var evd = A.Evd(); var b = new UserDefinedVector(Vector <Complex> .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, 9); // Make sure A/B didn't change. AssertHelpers.AlmostEqual(ACopy, A, 14); AssertHelpers.AlmostEqual(bCopy, b, 14); }
public void CanSolveForRandomVectorAndSymmetricMatrixWhenResultVectorGiven(int order) { var matrixA = Matrix <Complex32> .Build.RandomPositiveDefinite(order, 1); MatrixHelpers.ForceConjugateSymmetric(matrixA); var matrixACopy = matrixA.Clone(); var factorEvd = matrixA.Evd(); var vectorb = Vector <Complex32> .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].Real, matrixBReconstruct[i].Real, 1e-3f); Assert.AreEqual(vectorb[i].Imaginary, matrixBReconstruct[i].Imaginary, 1e-3f); } // 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]); } }
public void CanSolveForRandomMatrixAndSymmetricMatrix(int order) { var matrixA = MatrixLoader.GenerateRandomPositiveDefiniteHermitianDenseMatrix(order); MatrixHelpers.ForceConjugateSymmetric(matrixA); var matrixACopy = matrixA.Clone(); var factorEvd = matrixA.Evd(); var matrixB = MatrixLoader.GenerateRandomDenseMatrix(order, order); var matrixX = factorEvd.Solve(matrixB); // 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++) { AssertHelpers.AlmostEqual(matrixB[i, j], matrixBReconstruct[i, j], 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]); } } }
public void CanSolveForRandomVectorAndSymmetricMatrixWhenResultVectorGiven(int order) { var matrixA = MatrixLoader.GenerateRandomPositiveDefiniteHermitianDenseMatrix(order); MatrixHelpers.ForceConjugateSymmetric(matrixA); var matrixACopy = matrixA.Clone(); var factorEvd = matrixA.Evd(); var vectorb = MatrixLoader.GenerateRandomDenseVector(order); 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++) { AssertHelpers.AlmostEqual(vectorb[i], matrixBReconstruct[i], 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]); } }