I'm working on a small Java application that performs calculations on matrices. This is what I did for now to calculate the determinant and inverse of a square matrix. But I want to use the class Jama for the calculation of the eigenvalues and eigenvectors, but I do not know how to use it, could anyone give me a hand? Thanks.
import java.util.Scanner;
import Jama.*;
public class matrix {
public static void main(String[] args) {
double[][] matrix;
double det;
int n;
Scanner scanner = new Scanner(System.in);
System.out.println("Dimension of the matrix: ");
n = scanner.nextInt();
matrix = new int[n][n];
// insert values
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.printf("Values: " + i + " - " + j);
System.out.printf("\n");
matrix[i][j] = scanner.nextDouble();
}
}
// calculate determinant
det = dete(matrix, n);
}
private static dete(double ai[][], int i) {
double l = 0;
if (i == 1)
l = ai[0][0];
else if (i == 2) {
l = ai[0][0] * ai[1][1] - ai[0][1] * ai[1][0];
} else {
double ai1[][] = new double[i - 1][i - 1];
for (int k = 0; k < i; k++) {
for (int i1 = 1; i1 < i; i1++) {
int j = 0;
for (int j1 = 0; j1 < i; j1++)
if (j1 != k) {
ai1[i1 - 1][j] = ai[i1][j1];
j++;
}
}
if (k % 2 == 0)
l += ai[0][k] * dete(ai1, i - 1);
else
l -= ai[0][k] * dete(ai1, i - 1);
}
}
return l;
}
}
You just have to do this -
Do not forget to include Jama.Matrix.EigenvalueDecomposition.
You can have a look here