对我的Gauss-Jordan消除代码感到困惑

时间:2014-02-05 13:45:16

标签: java algorithm

所以,我一直在使用这个关于Rosetta代码的伪代码研究这个java算法,我认为它真的很接近于工作它只是最后一列的值是错误的。我想知道是否有人可以偷看并告诉我我的错误。我试图搜索但我无法弄明白。感谢。

public static double[][] gaussJordan2(double[][] gauss)
{
    int lead = 0;
    int rowCount = gauss.length;
    int columnCount = gauss[0].length;

    for(int r = 0; r < gauss.length; r++)
    {
        if(columnCount <= lead)
        {
            //return gauss;
        }

        int i = r;

        while(gauss[i][lead] == 0)
        {
            i++;

            if(rowCount == i)
            {
                i = r;
                lead++;

                if(columnCount == lead)
                {
                    //return gauss;
                }
            }
        }

        swapRows(gauss, i, r);

        if(gauss[r][lead] != 0)
        {
            for(int j = 0; j < gauss[r].length; j++)
            {
                gauss[r][j] /= gauss[r][lead];
            }
        }

        for(i = 0; i < rowCount; i++)
        {
            if (i != r)
            {
                double f = gauss[i][lead];

                for (int k = 0; k < gauss[r].length; k++)
                {
                    gauss[i][k] -= f * gauss[r][k];
                    printMatrix(gauss);
                }
            }
        }
        lead++;
    }

    printMatrix(gauss);
    return gauss;
}

//这是我的SWAP ROWS代码:

public static double [] [] swapRows(double [] [] a,int highRow,int lowRow){

            double grab = 0.0;
            double place = 0.0;

            for (int i = highRow; i < a.length; i++) {
                    for (int j = 0; j < a[0].length; j++) {
                            grab = a[highRow][j];
                            place = a[lowRow][j];
                            a[highRow][j] = place;
                            a[lowRow][j] = grab;
                    }
            }
            printMatrix(a);
            return a;

    }

0 个答案:

没有答案