Finding the matrix eigenvectors using inverse iteration
Here’s an example Java program that uses inverse iteration to find the eigenvectors of a matrix:
import Jama.Matrix; import Jama.EigenvalueDecomposition; public class MatrixEigenvectors { public static void main(String[] args) { // Define the matrix double[][] matrixData = { {2.0, -1.0, 0.0}, {-1.0, 2.0, -1.0}, {0.0, -1.0, 2.0} }; Matrix matrix = new Matrix(matrixData); // Compute eigenvalues and eigenvectors EigenvalueDecomposition eig = matrix.eig(); Matrix eigenvalues = eig.getD(); Matrix eigenvectors = eig.getV(); // Find eigenvectors using inverse iteration for (int i = 0; i < eigenvalues.getRowDimension(); i++) { double eigenvalue = eigenvalues.get(i, i); Matrix A = matrix.minus(Matrix.identity(matrix.getRowDimension(), matrix.getColumnDimension()).times(eigenvalue)); // Initial guess for eigenvector Matrix x = Matrix.random(matrix.getColumnDimension(), 1); // Perform inverse iteration double tolerance = 1e-6; // Convergence tolerance int maxIterations = 100; // Maximum number of iterations int iteration = 0; double error = Double.POSITIVE_INFINITY; while (error > tolerance && iteration < maxIterations) { Matrix y = A.solve(x); double norm = y.norm2(); x = y.times(1.0 / norm); error = A.times(x).minus(x.times(eigenvalue)).norm2(); iteration++; } System.out.println("Eigenvalue: " + eigenvalue); System.out.println("Eigenvector: "); x.print(8, 4); // Print eigenvector with 8 decimal places System.out.println(); } } }
This program uses the Jama library (https://math.nist.gov/javanumerics/jama/) for matrix operations and the EigenvalueDecomposition class to compute the eigenvalues and eigenvectors of the matrix. The inverse iteration method is then applied to find the eigenvectors iteratively. The program prints the eigenvalues and corresponding eigenvectors for each eigenvalue found.
No comments yet! You be the first to comment.