Finding the matrix square root.
Here’s a Java program that finds the square root of a given matrix:
import Jama.Matrix; public class MatrixSquareRoot { public static void main(String[] args) { double[][] matrixData = {{4, 1}, {2, 3}}; // Sample matrix data Matrix inputMatrix = new Matrix(matrixData); Matrix sqrtMatrix = findSquareRoot(inputMatrix); System.out.println("Input Matrix:"); inputMatrix.print(2, 2); System.out.println("\nSquare Root Matrix:"); sqrtMatrix.print(2, 2); } public static Matrix findSquareRoot(Matrix matrix) { Matrix[] eigDecomposition = matrix.eig().getV().eig().getV().toArray(); // Eigenvalue decomposition Matrix eigenvalueMatrix = eigDecomposition[0]; Matrix eigenvectorMatrix = eigDecomposition[1]; for (int i = 0; i < eigenvalueMatrix.getRowDimension(); i++) { double eigenvalue = eigenvalueMatrix.get(i, i); eigenvalueMatrix.set(i, i, Math.sqrt(eigenvalue)); // Square root of eigenvalues } Matrix sqrtMatrix = eigenvectorMatrix.times(eigenvalueMatrix).times(eigenvectorMatrix.inverse()); return sqrtMatrix; } }
The program uses the Jama library to perform the eigenvalue decomposition of the matrix. It finds the eigenvectors and eigenvalues, calculates the square root of the eigenvalues, and then reconstructs the square root matrix using the eigenvectors and square root of eigenvalues.
Please note that you need to have the Jama library added to your project for this code to work. You can download it from the Jama website or use a dependency management tool like Maven or Gradle to include it in your project.
No comments yet! You be the first to comment.