public static void EigenvaluesAndEigenvectors() { 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); } } RealEigendecomposition ed = H.Eigendecomposition(); SquareMatrix V = ed.TransformMatrix; PrintMatrix("V^T V", V.Transpose * V); PrintMatrix("V D V^T", V * ed.DiagonalizedMatrix * V.Transpose); foreach (RealEigenpair pair in ed.Eigenpairs) { Console.WriteLine($"Eigenvalue {pair.Eigenvalue}"); PrintMatrix("Hv", H * pair.Eigenvector); PrintMatrix("ev", pair.Eigenvalue * pair.Eigenvector); } 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}"); 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()}"); SquareMatrix G1 = new SquareMatrix(4); G1[0, 3] = 1.0; G1[1, 2] = 1.0; G1[2, 1] = -1.0; G1[3, 0] = -1.0; ComplexEigendecomposition ced = G1.Eigendecomposition(); foreach (ComplexEigenpair pair in ced.Eigenpairs) { Console.WriteLine(pair.Eigenvalue); } }
public void SymmetricHilbertMatrixEigenvalues() { for (int d = 1; d <= 8; d++) { SymmetricMatrix H = TestUtilities.CreateSymmetricHilbertMatrix(d); double tr = H.Trace(); double[] es = H.Eigenvalues(); Assert.IsTrue(TestUtilities.IsSumNearlyEqual(es, tr)); } }