SpectralDecompositionDecompose Method (DoubleMatrix, Boolean, DoubleMatrix) |
Namespace: Novacta.Analytics.Advanced
public static DoubleMatrix Decompose( DoubleMatrix matrix, bool lowerTriangularPart, out DoubleMatrix eigenvectors )
Exception | Condition |
---|---|
ArgumentNullException | matrix is null. |
ArgumentException | matrix is not square. |
In the following example, the spectral decomposition of a matrix is computed.
using System; using Novacta.Analytics.Advanced; namespace Novacta.Analytics.CodeExamples.Advanced { public class SpectralDecompositionExample0 { 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()); } } } // Executing method Main() produces the following output: // // Matrix to be decomposed: // 2 2 0 // 2 0 0 // 0 0 5 // // // // Eigenvalues: // -1.23606798 0 0 // 0 3.23606798 0 // 0 0 5 // // // // Eigenvectors: // 0.525731112 -0.850650808 0 // -0.850650808 -0.525731112 0 // 0 0 1 // // // // Matrix reconstruction: // 2 2 0 // 2 -1.11022302e-16 0 // 0 0 5 //