我正在研究一个在矩阵上执行计算的小型Java应用程序。这是我现在所做的计算方阵的行列式和倒数。但我想用班级Jama来计算特征值和特征向量,但我不知道如何使用它,谁能帮我一下吗?谢谢。使用贾马来计算特征值/特征向量?
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;
}
}
这是实际的代码?格式很糟糕,甚至没有编译。 –
@PhilippReichart - 是的,我确定了他的格式。但代码确实不能编译。 – Perception
对不起,现在我的代码运行良好。你能帮我解答一下吗? – whiteTIGER