public void Main() { // Create a matrix containing the // lower triangular part of the symmetric matrix // to be decomposed. var data = new double[9] { 2, 2, 0, 0, 0, 0, 1, 0, 5 }; var matrix = DoubleMatrix.Dense(3, 3, data); // Set the relevant triangular part. bool lowerTriangularPart = true; Console.WriteLine("Matrix to be decomposed:"); Console.WriteLine(DoubleMatrix.Dense(3, 3, new double[9] { 2, 2, 0, 2, 0, 0, 0, 0, 5 })); // Compute the Spectral decomposition. var eigenvalues = SpectralDecomposition.Decompose( matrix, lowerTriangularPart, out DoubleMatrix eigenvectors); Console.WriteLine(); Console.WriteLine("Eigenvalues:"); Console.WriteLine(eigenvalues); Console.WriteLine(); Console.WriteLine("Eigenvectors:"); Console.WriteLine(eigenvectors); Console.WriteLine(); Console.WriteLine("Matrix reconstruction:"); var l = eigenvalues; var v = eigenvectors; Console.WriteLine(v * l * v.Transpose()); }
/// Tests that method /// <see cref="SpectralDecomposition /// .Decompose(DoubleMatrix, bool, out DoubleMatrix)"/>, /// has been properly implemented. public static void Succeed( TestableSpectralDecomposition <TestableDoubleMatrix, DoubleMatrix> testableSD) { var testableMatrix = testableSD.TestableMatrix; #region Writable // Dense, Lower { var actualValues = SpectralDecomposition.Decompose( testableMatrix.AsDense, lowerTriangularPart: true, out DoubleMatrix actualVectors); DoubleMatrixAssert.AreEqual( expected: testableSD .Values, actual: actualValues, delta: SpectralDecompositionTest.Accuracy); DoubleMatrixAssert.AreEqual( expected: testableSD .VectorsIfLower, actual: actualVectors, delta: SpectralDecompositionTest.Accuracy); DoubleMatrixAssert.AreEqual( expected: testableSD .TestableMatrix.AsDense, actual: actualVectors * actualValues * actualVectors.Transpose(), delta: SpectralDecompositionTest.Accuracy); } // Sparse, Lower { var actualValues = SpectralDecomposition.Decompose( testableMatrix.AsSparse, lowerTriangularPart: true, out DoubleMatrix actualVectors); DoubleMatrixAssert.AreEqual( expected: testableSD .Values, actual: actualValues, delta: SpectralDecompositionTest.Accuracy); DoubleMatrixAssert.AreEqual( expected: testableSD .VectorsIfLower, actual: actualVectors, delta: SpectralDecompositionTest.Accuracy); DoubleMatrixAssert.AreEqual( expected: testableSD .TestableMatrix.AsDense, actual: actualVectors * actualValues * actualVectors.Transpose(), delta: SpectralDecompositionTest.Accuracy); } // Dense, Upper { var actualValues = SpectralDecomposition.Decompose( testableMatrix.AsDense, lowerTriangularPart: false, out DoubleMatrix actualVectors); DoubleMatrixAssert.AreEqual( expected: testableSD .Values, actual: actualValues, delta: SpectralDecompositionTest.Accuracy); DoubleMatrixAssert.AreEqual( expected: testableSD .VectorsIfUpper, actual: actualVectors, delta: SpectralDecompositionTest.Accuracy); DoubleMatrixAssert.AreEqual( expected: testableSD .TestableMatrix.AsDense, actual: actualVectors * actualValues * actualVectors.Transpose(), delta: SpectralDecompositionTest.Accuracy); } // Sparse, Upper { var actualValues = SpectralDecomposition.Decompose( testableMatrix.AsSparse, lowerTriangularPart: false, out DoubleMatrix actualVectors); DoubleMatrixAssert.AreEqual( expected: testableSD .Values, actual: actualValues, delta: SpectralDecompositionTest.Accuracy); DoubleMatrixAssert.AreEqual( expected: testableSD .VectorsIfUpper, actual: actualVectors, delta: SpectralDecompositionTest.Accuracy); DoubleMatrixAssert.AreEqual( expected: testableSD .TestableMatrix.AsDense, actual: actualVectors * actualValues * actualVectors.Transpose(), delta: SpectralDecompositionTest.Accuracy); } #endregion #region ReadOnly // Dense, Lower { var actualValues = SpectralDecomposition.Decompose( testableMatrix.AsDense.AsReadOnly(), lowerTriangularPart: true, out DoubleMatrix actualVectors); DoubleMatrixAssert.AreEqual( expected: testableSD .Values, actual: actualValues, delta: SpectralDecompositionTest.Accuracy); DoubleMatrixAssert.AreEqual( expected: testableSD .VectorsIfLower, actual: actualVectors, delta: SpectralDecompositionTest.Accuracy); DoubleMatrixAssert.AreEqual( expected: testableSD .TestableMatrix.AsDense, actual: actualVectors * actualValues * actualVectors.Transpose(), delta: SpectralDecompositionTest.Accuracy); } // Sparse, Lower { var actualValues = SpectralDecomposition.Decompose( testableMatrix.AsSparse.AsReadOnly(), lowerTriangularPart: true, out DoubleMatrix actualVectors); DoubleMatrixAssert.AreEqual( expected: testableSD .Values, actual: actualValues, delta: SpectralDecompositionTest.Accuracy); DoubleMatrixAssert.AreEqual( expected: testableSD .VectorsIfLower, actual: actualVectors, delta: SpectralDecompositionTest.Accuracy); DoubleMatrixAssert.AreEqual( expected: testableSD .TestableMatrix.AsDense, actual: actualVectors * actualValues * actualVectors.Transpose(), delta: SpectralDecompositionTest.Accuracy); } // Dense, Upper { var actualValues = SpectralDecomposition.Decompose( testableMatrix.AsDense.AsReadOnly(), lowerTriangularPart: false, out DoubleMatrix actualVectors); DoubleMatrixAssert.AreEqual( expected: testableSD .Values, actual: actualValues, delta: SpectralDecompositionTest.Accuracy); DoubleMatrixAssert.AreEqual( expected: testableSD .VectorsIfUpper, actual: actualVectors, delta: SpectralDecompositionTest.Accuracy); DoubleMatrixAssert.AreEqual( expected: testableSD .TestableMatrix.AsDense, actual: actualVectors * actualValues * actualVectors.Transpose(), delta: SpectralDecompositionTest.Accuracy); } // Sparse, Upper { var actualValues = SpectralDecomposition.Decompose( testableMatrix.AsSparse.AsReadOnly(), lowerTriangularPart: false, out DoubleMatrix actualVectors); DoubleMatrixAssert.AreEqual( expected: testableSD .Values, actual: actualValues, delta: SpectralDecompositionTest.Accuracy); DoubleMatrixAssert.AreEqual( expected: testableSD .VectorsIfUpper, actual: actualVectors, delta: SpectralDecompositionTest.Accuracy); DoubleMatrixAssert.AreEqual( expected: testableSD .TestableMatrix.AsDense, actual: actualVectors * actualValues * actualVectors.Transpose(), delta: SpectralDecompositionTest.Accuracy); } #endregion }