-
Notifications
You must be signed in to change notification settings - Fork 30
Eigenvalues and Eigenvectors
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.)
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);
}
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}");
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);
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.
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.
- Project
- What's New
- Installation
- Versioning
- Tutorials
- Functions
- Compute a Special Function
- Bessel Functions
- Solvers
- Evaluate An Integral
- Find a Maximum or Minimum
- Solve an Equation
- Integrate a Differential Equation
- Data Wrangling
- Statistics
- Analyze a Sample
- Compare Two Samples
- Simple Linear Regression
- Association
- ANOVA
- Contingency Tables
- Multiple Regression
- Logistic Regression
- Cluster and Component Analysis
- Time Series Analysis
- Fit a Sample to a Distribution
- Distributions
- Special Objects
- Linear Algebra
- Polynomials
- Permutations
- Partitions
- Uncertain Values
- Extended Precision
- Functions