Skip to content

Eigenvalues and Eigenvectors

David Wright edited this page Apr 27, 2018 · 2 revisions

Eigenvalues and eigenvectors are extremely important in many area of applied mathematics, and Meta.Numerics can compute them for you. (In these examples, we will use the PrintMatrix method defined earlier.)

How do I find the eigenvalues and eigenvectors of a matrix?

Let's use a Hilbert matrix as an example:

using Meta.Numerics.Matrices;

SymmetricMatrix H = new SymmetricMatrix(3);
for (int r = 0; r < H.Dimension; r++) {
    for (int c = 0; c <= r; c++) {
        H[r, c] = 1.0 / (r + c + 1);
    }
}

Here is some code that computes H's eigen-decomposition, then iterates through each eigenvalue/eigenvector pair, showing that the pair fulfills the eigen-equation H v = e v:

using System;

RealEigendecomposition ed = H.Eigendecomposition();

foreach(RealEigenpair pair in ed.Eigenpairs) {
    Console.WriteLine($"Eigenvalue {pair.Eigenvalue}");
    PrintMatrix("Hv", H * pair.Eigenvector);
    PrintMatrix("ev", pair.Eigenvalue * pair.Eigenvector);
}

What if I want them sorted?

When the decomposition is first formed, the eigen-pairs are in no particular order. Use the Sort method to sort them by value or absolute magnitude.

ed.Eigenpairs.Sort(OrderBy.MagnitudeDescending);
Console.WriteLine($"Largest eigenvalue {ed.Eigenpairs[0].Eigenvalue}");
ed.Eigenpairs.Sort(OrderBy.ValueAscending);
Console.WriteLine($"Least eigenvalue {ed.Eigenpairs[0].Eigenvalue}");

Can I get the diagonalizing transform?

One way to think of an eigen-decomposition is that it finding an orthogonal matrix V such that VT H V = D, or equivalently H = V D VT, where D is a diagonal matrix. You can get V and D from a RealEigendecomposition as the properties TransformMatrix and DiagonalizedMatrix. Here's some code that proves that V is orthogonal and fulfills the eigen-decomposition equation:

SquareMatrix V = ed.TransformMatrix;
PrintMatrix ("V^T V", V.Transpose * V);
PrintMatrix("V D V^T", V * ed.DiagonalizedMatrix * V.Transpose);

Can I get just eigenvalues?

Sure. Here's some code that just gets the eigenvalues, then shows that their sum is the trace and their product the determinant of the original matrix:

double[] eigenvalues = H.Eigenvalues();

double sum = 0.0;
double product = 1.0;
foreach (double eigenvalue in eigenvalues) {
    sum += eigenvalue;
    product *= eigenvalue;
}
Console.WriteLine($"sum(e) = {sum}, tr(H) = {H.Trace()}");
Console.WriteLine($"prod(e) = {product}, det(H) = {H.CholeskyDecomposition().Determinant()}");

Finding just eigenvalues is about twice as fast as finding eigenvalues and eigenvectors, so this is definitely a good move if you don't need the eigenvectors.

What about non-symmetric matrices?

Non-symmetric square matrices also have eigenvalues, but their eigenvalues (and corresponding eigenvector components) can be complex. Meta.Numerics can also eign-decompose a non-symmetric square matrix.

Home

Clone this wiki locally