Finding the eigenvalues of a matrix.
Here’s an example program in Java that uses the Apache Commons Math library to find the eigenvalues of a matrix:
import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.linear.EigenDecomposition; import org.apache.commons.math3.linear.RealMatrix; public class EigenvaluesExample { public static void main(String[] args) { // Define the matrix double[][] matrixData = { { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 }, { 7.0, 8.0, 9.0 } }; // Create a RealMatrix object RealMatrix matrix = new Array2DRowRealMatrix(matrixData); // Create an EigenDecomposition object EigenDecomposition eigenDecomposition = new EigenDecomposition(matrix); // Get the eigenvalues double[] eigenvalues = eigenDecomposition.getRealEigenvalues(); // Print the eigenvalues System.out.println("Eigenvalues:"); for (double eigenvalue : eigenvalues) { System.out.println(eigenvalue); } } }
In this example, we use the `Array2DRowRealMatrix` class from the Apache Commons Math library to create a real matrix. Then, we create an `EigenDecomposition` object using the matrix. Finally, we use the `getRealEigenvalues` method to retrieve the eigenvalues as an array and print them.
Make sure you have the Apache Commons Math library added to your project’s classpath to run this code. You can download the library from the Apache Commons Math website (https://commons.apache.org/proper/commons-math/).
No comments yet! You be the first to comment.