Finding the matrix eigenvectors using Jacobi algorithm.
Here’s an example Java program that implements the Jacobi algorithm to find the eigenvectors of a given matrix:
import java.util.Arrays; public class JacobiEigenvector { public static void main(String[] args) { double[][] matrix = { {2, -1, 0}, {-1, 2, -1}, {0, -1, 2} }; double tolerance = 1e-8; // Tolerance for convergence int maxIterations = 100; // Maximum number of iterations double[][] eigenvectors = findEigenvectors(matrix, tolerance, maxIterations); System.out.println("Eigenvectors:"); for (double[] vector : eigenvectors) { System.out.println(Arrays.toString(vector)); } } public static double[][] findEigenvectors(double[][] matrix, double tolerance, int maxIterations) { int n = matrix.length; double[][] eigenvectors = new double[n][n]; // Initialize eigenvectors as identity matrix for (int i = 0; i < n; i++) { eigenvectors[i][i] = 1.0; } int iterations = 0; double maxOffDiagonal = getMaxOffDiagonal(matrix); while (maxOffDiagonal > tolerance && iterations < maxIterations) { int p = 0; int q = 0; double maxOffDiagonalElement = 0.0; // Find indices (p, q) of the largest off-diagonal element for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (Math.abs(matrix[i][j]) > maxOffDiagonalElement) { maxOffDiagonalElement = Math.abs(matrix[i][j]); p = i; q = j; } } } // Compute theta (rotation angle) double theta = 0.5 * Math.atan2(2 * matrix[p][q], matrix[p][p] - matrix[q][q]); // Compute rotation matrix double[][] rotation = new double[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { rotation[i][j] = (i == j) ? 1.0 : 0.0; } } rotation[p][p] = Math.cos(theta); rotation[p][q] = -Math.sin(theta); rotation[q][p] = Math.sin(theta); rotation[q][q] = Math.cos(theta); // Update matrix and eigenvectors matrix = multiplyMatrix(multiplyMatrix(transpose(rotation), matrix), rotation); eigenvectors = multiplyMatrix(eigenvectors, rotation); maxOffDiagonal = getMaxOffDiagonal(matrix); iterations++; } return eigenvectors; } public static double getMaxOffDiagonal(double[][] matrix) { int n = matrix.length; double maxOffDiagonal = 0.0; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { double offDiagonal = Math.abs(matrix[i][j]); if (offDiagonal > maxOffDiagonal) { maxOffDiagonal = offDiagonal; } } } return maxOffDiagonal; } public static double[][] multiplyMatrix(double[][] matrix1, double[][] matrix2) { int m = matrix1.length; int n = matrix2[0].length; int p = matrix1[0].length; double[][] result = new double[m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { double sum = 0.0; for (int k = 0; k < p; k++) { sum += matrix1[i][k] * matrix2[k][j]; } result[i][j] = sum; } } return result; } public static double[][] transpose(double[][] matrix) { int m = matrix.length; int n = matrix[0].length; double[][] result = new double[n][m]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { result[j][i] = matrix[i][j]; } } return result; } }
In this example, the `findEigenvectors` method implements the Jacobi algorithm to iteratively find the eigenvectors of the given matrix. The algorithm continues until the maximum off-diagonal element of the matrix falls below a specified tolerance or the maximum number of iterations is reached.
The program calculates the eigenvectors for a sample 3×3 matrix and prints them to the console. You can modify the `matrix` array to use your own matrix data.
Please note that the implementation provided here is a simplified version of the Jacobi algorithm and may not be optimized for large matrices.
No comments yet! You be the first to comment.