所以我刚开始用 Java 做数值方法;
// I have a matrix class that has get, set methods for creating matrices; have used those in the method below.
// The method below computes a Matrix and prints as well returns it.
public Matrix computeA(){
int nrows = A.getNumRows();
int ncols = A.getNumCols();
for(int i = 0; i < nrows; i++){
double sum = 0.0;
for (int j = 0; i< ncols; j++){
if (i!=j){
A.setElement(i,j, aijCalc(i,j) );} // aijCalc is numeric method; setElement has parameters (i, j, value)
}
A.print();
return A;
}
现在,这是我不明白的地方:i = j 的对角线,矩阵条目为 1 -(该行中所有 aij 条目的总和(对于 i!=j))。如何在上述方法中对此进行编码?
我当前的输出(没有任何四舍五入看起来像这样):
0.0 0.026055555555555554 0.0248125 0.050694444444444445 0.058722222222222222 0.030208333333333334
-0.0053750000000000004 0.0 0.00792361111111111 0.01813194444444444 0.0236111111111111 0.009874999999999998
-0.0013854166666666667 -0.005291666666666666 0.0 0.008680555555555556 0.009041666666666667 0.0
0.0 0.009041666666666667 0.008680555555555556 0.0 -0.005291666666666666 -0.0013854166666666667
0.009874999999999998 0.0236111111111111 0.01813194444444444 0.00792361111111111 0.0 -0.0053750000000000004
0.030208333333333334 0.05872222222222222 0.050694444444444445 0.0248125 0.026055555555555554 0.0
(它有 0,其中 i = j)
它应该看起来像这样:
0.810 0.026 0.025 0.051 0.059 0.030
-‐0.005 0.946 0.008 0.018 0.024 0.010
-‐0.001 -‐0.005 0.989 0.009 0.009 0.000
0.000 0.009 0.009 0.989 -‐0.005 -‐0.001
0.010 0.024 0.018 0.008 0.946 -‐0.005
0.030 0.059 0.051 0.025 0.026 0.810
谢谢!