23.2 Using the Eigenvalue Classes (.NET, C#, CSharp, VB, Visual Basic, F#)
The NMath eigenvalue classes solve symmetric, Hermitian, and nonsymmetric eigenvalue problems.
Constructing Eigenvalue Objects
Instances of the eigenvalue classes are constructed from matrices of the appropriate type. For example, this code creates a FloatSymEigDecomp from a FloatSymmetricMatrix:
Code Example – C# eigenvalue decomposition
var A = new FloatMatrix( "4x4 [ 0 1.73205080756888 0 0
1.73205080756888 0 2 0
0 2 0 1.73205080756888
0 0 1.73205080756888 0 ]");
var Asym = new FloatSymmetricMatrix( A );
var eig = new FloatSymEigDecomp( Asym );
Code Example – VB eigenvalue decomposition
Dim A As New FloatMatrix("4x4 [ 0 1.73205080756888 0 0" & _
"1.73205080756888 0 2 0" & _
"0 2 0 1.73205080756888" & _
"0 0 1.73205080756888 0 ]")
Dim Asym As New FloatSymmetricMatrix(A)
Dim Eig As New FloatSymEigDecomp(Asym)
Similarly, if A is a DoubleHermitianMatrix, this code creates a DoubleHermitianEigDecomp object from A:
Code Example – C# eigenvalue decomposition
var eig = new DoubleHermitianEigDecomp( A );
Code Example – VB eigenvalue decomposition
Dim Eig As New DoubleHermitianEigDecomp(A)
All eigenvalue classes provide an IsGood property that returns true if all the eigenvalues and eigenvectors were successfully computed:
Code Example – C# eigenvalue decomposition
var eig = new DoubleComplexEigDecomp( A );
if ( eig.IsGood )
{
// Do something here...
}
Code Example – VB eigenvalue decomposition
Dim Eig As New DoubleComplexEigDecomp(A)
If Eig.IsGood Then
' Do something here...
End If
Retrieving Eigenvalues and Eigenvectors
All eigenvalue classes provide read-only properties and member functions for retrieving eigenvalues and eigenvectors.
● NumberOfEigenValues gets the number of eigenvalues computed.
● EigenValues gets the vector of computed eigenvalues.
● EigenValue() returns the specified eigenvalue.
● NumberOfLeftEigenVectors gets the number of left eigenvectors.
● LeftEigenVectors gets the matrix of left eigenvectors.
● LeftEigenVector() returns the specified left eigenvector.
● NumberOfRightEigenVectors gets the number of right eigenvectors.
● RightEigenVectors gets the matrix of right eigenvectors.
● RightEigenVector() returns the specified right eigenvector.
For example:
Code Example – C# eigenvalue decomposition
var decomp = new FloatEigDecomp( A );
Console.WriteLine( "Eigenvalues = " + decomp.EigenValues );
Console.WriteLine( "Left eigenvectors = " +
decomp.LeftEigenVectors );
Console.WriteLine( "Right eigenvectors = " +
decomp.RightEigenVectors );
Code Example – VB eigenvalue decomposition
Dim Decomp As New FloatEigDecomp(A)
Console.WriteLine("Eigenvalues = {0}", Decomp.EigenValues)
Console.WriteLine("Left eigenvectors = {0}",
Decomp.LeftEigenVectors)
Console.WriteLine("Right eigenvectors = {0}",
Decomp.RightEigenVectors)
Retrieving Information About the Original Matrix
Read-only properties are also provided for retrieving information about the original matrix A:
● Rows gets the number of rows.
● Cols gets the number of columns.
Reusing Eigenvalue Decompositions
An existing eigenvalue object can be reused with another matrix using the Factor() method:
Code Example – C# eigenvalue decomposition
var eig = new FloatSymEigDecomp( A );
if ( eig.IsGood )
{
// Do something here...
}
eig.Factor( B );
if ( eig.IsGood )
{
// Do something here...
}
Code Example – VB eigenvalue decomposition
Dim Eig As New FloatSymEigDecomp(A)
If Eig.IsGood Then
' Do something here...
End If
Eig.Factor(B)
If Eig.IsGood Then
' Do something here...
End If