Finding the matrix eigenvalues using Jacobi algorithm
Here’s a Java implementation of the Jacobi algorithm for finding the eigenvalues of a matrix:
import java.util.Arrays; public class JacobiAlgorithm { // Function to find the maximum off-diagonal element private static double findMaxOffDiagonal(double[][] matrix) { int n = matrix.length; double maxVal = 0.0; int row = 0, col = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (Math.abs(matrix[i][j]) > maxVal) { maxVal = Math.abs(matrix[i][j]); row = i; col = j; } } } return matrix[row][col]; } // Function to perform the Jacobi rotation private static void performJacobiRotation(double[][] matrix, double[][] eigenVectors, int p, int q) { int n = matrix.length; double tau = (matrix[q][q] - matrix[p][p]) / (2 * matrix[p][q]); double t = Math.sqrt(1 + tau * tau); double sinTheta = 1 / t; double cosTheta = tau / t; for (int i = 0; i < n; i++) { double oldPi = matrix[p][i]; double oldQi = matrix[q][i]; matrix[p][i] = oldPi * cosTheta + oldQi * sinTheta; matrix[q][i] = -oldPi * sinTheta + oldQi * cosTheta; double oldVi = eigenVectors[i][p]; double oldWi = eigenVectors[i][q]; eigenVectors[i][p] = oldVi * cosTheta + oldWi * sinTheta; eigenVectors[i][q] = -oldVi * sinTheta + oldWi * cosTheta; } } // Function to find the eigenvalues and eigenvectors of the matrix public static void findEigenvalues(double[][] matrix) { int n = matrix.length; double[][] eigenVectors = new double[n][n]; // Initialize eigenVectors as an identity matrix for (int i = 0; i < n; i++) { eigenVectors[i][i] = 1.0; } // Set the maximum number of iterations int maxIterations = 100; int iterations = 0; while (findMaxOffDiagonal(matrix) > 1e-8 && iterations < maxIterations) { int p = 0, q = 1; double maxOffDiagonal = findMaxOffDiagonal(matrix); // Find indices p and q corresponding to the maximum off-diagonal element for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (Math.abs(matrix[i][j]) == maxOffDiagonal) { p = i; q = j; break; } } } performJacobiRotation(matrix, eigenVectors, p, q); iterations++; } // Print the eigenvalues System.out.println("Eigenvalues:"); for (int i = 0; i < n; i++) { System.out.println(matrix[i][i]); } // Print the eigenvectors System.out.println("Eigenvectors:"); for (int i = 0; i < n; i++) { System.out.println(Arrays.toString(eigenVectors[i])); } } public static void main(String[] args) { double[][] matrix = { {4.0, -3.0, 1.0}, {-3.0, 6.0, -4.0}, {1.0, -4.0, 7.0} }; findEigenvalues(matrix); } }
This code takes a matrix as input and uses the Jacobi algorithm to find the eigenvalues and eigenvectors of the matrix. It iteratively performs Jacobi rotations until the maximum off-diagonal element becomes smaller than a threshold. The resulting eigenvalues and eigenvectors are then printed.
In the `main` method, a sample matrix is provided for demonstration purposes. You can modify the matrix with your desired values or pass it dynamically as per your requirements.
No comments yet! You be the first to comment.