Finding the matrix eigenvalues using power iteration
Here’s a Java program that uses the power iteration method to find the eigenvalues of a given matrix:
import java.util.Arrays; public class PowerIteration { public static void main(String[] args) { double[][] matrix = {{2, -1}, {-4, 3}}; // Example matrix double[] eigenvalues = findEigenvalues(matrix); System.out.println("Eigenvalues: " + Arrays.toString(eigenvalues)); } public static double[] findEigenvalues(double[][] matrix) { int n = matrix.length; double[] eigenvalues = new double[n]; // Initial guess for eigenvector double[] x = new double[n]; Arrays.fill(x, 1.0); double epsilon = 1e-6; // Error tolerance double prevEigenvalue = 0.0; for (int i = 0; i < n; i++) { while (true) { double[] y = multiply(matrix, x); double norm = calculateNorm(y); x = divide(y, norm); double eigenvalue = dotProduct(x, multiply(matrix, x)); if (Math.abs(eigenvalue - prevEigenvalue) < epsilon) { eigenvalues[i] = eigenvalue; prevEigenvalue = eigenvalue; break; } prevEigenvalue = eigenvalue; } // Deflate matrix matrix = deflate(matrix, eigenvalue, x); } return eigenvalues; } public static double[] multiply(double[][] matrix, double[] vector) { int n = matrix.length; double[] result = new double[n]; for (int i = 0; i < n; i++) { double sum = 0.0; for (int j = 0; j < n; j++) { sum += matrix[i][j] * vector[j]; } result[i] = sum; } return result; } public static double calculateNorm(double[] vector) { double sumOfSquares = 0.0; for (double value : vector) { sumOfSquares += value * value; } return Math.sqrt(sumOfSquares); } public static double[] divide(double[] vector, double scalar) { int n = vector.length; double[] result = new double[n]; for (int i = 0; i < n; i++) { result[i] = vector[i] / scalar; } return result; } public static double dotProduct(double[] vector1, double[] vector2) { int n = vector1.length; double sum = 0.0; for (int i = 0; i < n; i++) { sum += vector1[i] * vector2[i]; } return sum; } public static double[][] deflate(double[][] matrix, double eigenvalue, double[] eigenvector) { int n = matrix.length; double[][] result = new double[n][n]; for (int i = 0; i < n; i++) { result[i] = Arrays.copyOf(matrix[i], n); } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { result[i][j] -= eigenvalue * eigenvector[i] * eigenvector[j]; } } return result; } }
In this program, the `findEigenvalues` method takes a square matrix as input and returns an array containing the eigenvalues of the matrix. The `multiply` method performs matrix-vector multiplication, and the `calculateNorm` method calculates the Euclidean norm of a vector. The `divide` method divides a vector by a scalar, and the `dotProduct` method calculates the dot product of two vectors. The `deflate` method removes the contribution of a given eigenvalue and eigenvector from the matrix.
The program uses a loop to iteratively find the eigenvalues one by one. The algorithm starts with an initial guess for the eigenvector (in this case, a vector of all 1s), and then performs the power iteration steps until convergence. The eigenvalue and eigenvector are updated in each iteration, and the matrix is deflated to remove the contribution of the found eigenvalue.
In the example provided, the program calculates the eigenvalues of the 2×2 matrix {{2, -1}, {-4, 3}}. You can modify the `matrix` variable in the `main` method to test the program with different matrices.