Finding the Cholesky decomposition of a matrix
Here’s a Java program that finds the Cholesky decomposition of a given matrix:
public class CholeskyDecomposition { public static double[][] choleskyDecomposition(double[][] matrix) { int n = matrix.length; double[][] lower = new double[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { double sum = 0; if (j == i) { for (int k = 0; k < j; k++) { sum += Math.pow(lower[j][k], 2); } lower[j][j] = Math.sqrt(matrix[j][j] - sum); } else { for (int k = 0; k < j; k++) { sum += (lower[i][k] * lower[j][k]); } lower[i][j] = (matrix[i][j] - sum) / lower[j][j]; } } } return lower; } public static void printMatrix(double[][] matrix) { for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { System.out.print(matrix[i][j] + " "); } System.out.println(); } } public static void main(String[] args) { double[][] matrix = { {25, 15, -5}, {15, 18, 0}, {-5, 0, 11} }; double[][] lower = choleskyDecomposition(matrix); System.out.println("Lower triangular matrix (Cholesky decomposition):"); printMatrix(lower); } }
This program demonstrates the Cholesky decomposition for a 3×3 matrix. You can modify the `matrix` variable in the `main` method to use different matrices. The program calculates the lower triangular matrix, which represents the Cholesky decomposition of the input matrix. The `printMatrix` method is used to display the result.
Note that this implementation assumes that the input matrix is symmetric and positive definite, which are the requirements for Cholesky decomposition.
No comments yet! You be the first to comment.