Finding the matrix eigenvectors using QR algorithm
Here’s an example Java code that implements the QR algorithm to find the eigenvectors of a matrix:
import java.util.Arrays; public class QRAlgorithm { public static void main(String[] args) { double[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; double[][] eigenvectors = findEigenvectors(matrix); System.out.println("Eigenvectors:"); for (double[] vector : eigenvectors) { System.out.println(Arrays.toString(vector)); } } public static double[][] findEigenvectors(double[][] matrix) { int n = matrix.length; double[][] qMatrix = matrix.clone(); double[][] rMatrix = new double[n][n]; double[][] eigenvectors = new double[n][n]; // Initialize the eigenvectors matrix as an identity matrix for (int i = 0; i < n; i++) { eigenvectors[i][i] = 1; } for (int i = 0; i < 50; i++) { // Perform 50 iterations (can be adjusted) qrDecomposition(qMatrix, qMatrix, rMatrix); qMatrix = multiplyMatrix(rMatrix, qMatrix); // Update the eigenvectors matrix eigenvectors = multiplyMatrix(eigenvectors, qMatrix); } return eigenvectors; } public static void qrDecomposition(double[][] matrix, double[][] qMatrix, double[][] rMatrix) { int n = matrix.length; double[][] aMatrix = matrix.clone(); for (int k = 0; k < n - 1; k++) { double[] v = new double[n]; double[] e = new double[n]; for (int i = k; i < n; i++) { v[i] = aMatrix[i][k]; } double vNorm = calculateNorm(v, k, n); e[k + 1] = vNorm; if (vNorm != 0) { double[] u = calculateUnitVector(v, vNorm, k, n); for (int j = k; j < n; j++) { rMatrix[k][j] = u[j]; } for (int j = k; j < n; j++) { double[] aColumn = getColumn(aMatrix, j); double dotProduct = calculateDotProduct(u, aColumn, k, n); for (int i = k; i < n; i++) { aMatrix[i][j] -= 2 * u[i] * dotProduct; } } } } for (int i = 0; i < n; i++) { rMatrix[i][i] = aMatrix[i][i]; } } public static double[][] multiplyMatrix(double[][] matrixA, double[][] matrixB) { int rowsA = matrixA.length; int colsA = matrixA[0].length; int colsB = matrixB[0].length; double[][] result = new double[rowsA][colsB]; for (int i = 0; i < rowsA; i++) { for (int j = 0; j < colsB; j++) { for (int k = 0; k < colsA; k++) { result[i][j] += matrixA[i][k] * matrixB [k][j]; } } } return result; } public static double calculateNorm(double[] vector, int startIndex, int endIndex) { double norm = 0; for (int i = startIndex; i < endIndex; i++) { norm += Math.pow(vector[i], 2); } return Math.sqrt(norm); } public static double[] calculateUnitVector(double[] vector, double norm, int startIndex, int endIndex) { double[] unitVector = new double[vector.length]; for (int i = startIndex; i < endIndex; i++) { unitVector[i] = vector[i] / norm; } return unitVector; } public static double[] getColumn(double[][] matrix, int columnIndex) { double[] column = new double[matrix.length]; for (int i = 0; i < matrix.length; i++) { column[i] = matrix[i][columnIndex]; } return column; } public static double calculateDotProduct(double[] vectorA, double[] vectorB, int startIndex, int endIndex) { double dotProduct = 0; for (int i = startIndex; i < endIndex; i++) { dotProduct += vectorA[i] * vectorB[i]; } return dotProduct; } }
This code performs the QR decomposition and QR iterations to find the eigenvectors of a given matrix. Adjust the matrix values and the number of iterations as needed. The code assumes that the matrix is square.
No comments yet! You be the first to comment.