Finding the eigenvectors of a matrix.
Here’s an example of a Java program that finds the eigenvectors of a matrix using the Apache Commons Math library:
import org.apache.commons.math3.linear.EigenDecomposition; import org.apache.commons.math3.linear.MatrixUtils; import org.apache.commons.math3.linear.RealMatrix; public class EigenvectorCalculator { public static void main(String[] args) { // Define the matrix double[][] matrixData = { { 1, 2 }, { 3, 4 } }; RealMatrix matrix = MatrixUtils.createRealMatrix(matrixData); // Perform eigen decomposition EigenDecomposition eigenDecomposition = new EigenDecomposition(matrix); // Get the eigenvectors RealMatrix eigenvectors = eigenDecomposition.getV(); // Print the eigenvectors for (int i = 0; i < eigenvectors.getColumnDimension(); i++) { double[] eigenvectorData = eigenvectors.getColumn(i); System.out.print("Eigenvector " + (i + 1) + ": "); for (double value : eigenvectorData) { System.out.print(value + " "); } System.out.println(); } } }
In this program, we use the Apache Commons Math library’s `EigenDecomposition` class to perform eigen decomposition on a given matrix. We create a `RealMatrix` object using the input matrix data, and then pass it to the `EigenDecomposition` constructor. Finally, we retrieve the eigenvectors using the `getV()` method and print them out.
Please note that you need to have the Apache Commons Math library added to your Java project in order for this code to compile and run successfully. You can download the library from the Apache Commons Math website (https://commons.apache.org/proper/commons-math/) and include the JAR file in your project’s dependencies.