下面的代码片段的目的是创建一个矩阵的实例,然后计算一个 nxn 矩阵的行列式。但是,方法computeDet
不会返回,因为发生以下错误:value cannot be resolved to a variable
. 这是为什么?
** 从这里编辑的行列式代码(此代码确实有效 - 没有错误)
public class MatrixArrayCR {
long[][] matrix;
public MatrixArrayCR(long[] arr) {
double len = Math.sqrt(arr.length);
int counter = 0;
for (int i=0; i==len; i++) {
for (int j=0; j==len; j++) {
matrix[i][j] = arr[counter];
counter++;
}
}
}
// determinant method edited from code off http://www.coderanch.com/t/446179/java/java/Learning-Project
public long determinant() {
return computeDet(matrix);
}
public long computeDet(long[][] matrix) {
int matrixSize = matrix.length;
if (matrixSize==2) {
return matrix[0][0]*matrix[1][1]-matrix[0][1]*matrix[1][0];
} else {
long value = 0;
for (int i=0; i<matrixSize; i++) {
long[][] minor=generateMinor(matrix,0,i);
value += (sgn(i)*matrix[0][i]*computeDet(minor));
}
} return value; //ERROR HERE
}
private int sgn(int n) {
if (n%2==0) {
return 1;
} else {
return -1;
}
}
private long[][] generateMinor(long[][] matrix, int row, int column) {
int matrixSize = matrix.length;
int minorSize = matrixSize -1;
int counterOne = 0;
int counterTwo = 0;
long[][] minor = new long[minorSize][minorSize];
for (int i=0; i<matrixSize; i++) {
if (i==row) {
continue;
} for (int j=0; j<matrixSize; j++) {
if (j==column) {
continue;
} minor[counterOne][counterTwo] = matrix[i][j];
++ counterTwo;
} ++counterOne;
counterTwo = 0;
} return minor;
}
}