faint….高斯算法有问题.以至求出的秩经常不是正确值,我倒.据说还是java算浮点数的问题.算了.
要不改用C++写好了.明天再试试.换个方法重写高斯消元函数.奇怪的是计算行列式值的时候又没错.
/**
* 求矩阵的秩
* @return 秩
*/
public int matrixOrder(){
//高斯消元化为上三角
guass();
this.displayMatrix(a);
int r=a.length-1;
//初始值为矩阵的行数
int count=r+1;
int c=a[0].length-1;
boolean flag=true;
//自底向上循环查找非零,非无穷,非NaN的值.发现则跳出循环
for(int i=r;i>=0&&flag;i–){
flag=true;
for(int j=c;j>=0;j–){
//高斯消元后的结果可能为无穷和非数字,加上判断
if(a[i][j]!=0&&!Double.isNaN(a[i][j])&&!Double.isInfinite(a[i][j]))flag=false;
}
//每一个满足条件的行则count减一
if(flag==true)count–;
}
return count;
}
update: 确实是java浮点数运算的问题.
Math.sqrt(-1.0) -> NaN
0.0 / 0.0 -> NaN
1.0 / 0.0 -> 无穷大
-1.0 / 0.0 -> 负无穷大
NaN + 1.0 -> NaN
无穷大 + 1.0 -> 无穷大
无穷大 + 无穷大 -> 无穷大
NaN > 1.0 -> false
NaN == 1.0 -> false
NaN < 1.0 -> false
NaN == NaN -> false
0.0 == -0.01 -> true
http://www-128.ibm.com/developerworks/cn/java/j-jtp0114/