求解矩阵的高斯消元问题

时间:2012-06-28 20:33:26

标签: c++ gaussian

float aMatrix[10][11];
float bMatrix[10];

//这样称呼......     solveMatrix(aMatrix, bMatrix, actualCol);

float* solveMatrix(float aMatrix[][DEFCOLS+1],float bMatrix[DEFCOLS], size_t cols){
    std::cout << "\nInside solveMatrix...: " << endl;
    size_t N2 = cols;

std::cout << "\N2 is...: " << N2 << endl;
for(size_t p=0; p<N2; p++){
    //std::cout << "\nInside 1st for loop...: " << endl;
    // find pivot row and swap
    int max = p;

    for(size_t i=p+1; i<N2; i++){
        //std::cout << "\nInside 2nd for loop...: " << endl;
        if ( abs(aMatrix[i][p]) > abs(aMatrix[max][p]) ){
            max = i;
        }
    }

    //std::cout << "\nJust b4 all the swapping...: " << endl;

    float temp[] = { *aMatrix[p] };
    *aMatrix[p] = *aMatrix[max];

    *aMatrix[max] = *temp;

    float t = bMatrix[p];
    bMatrix[p] = bMatrix[max];


    bMatrix[max] = t;
    //std::cout << "\nDone all the swapping...: " << endl;

    if ( abs(aMatrix[p][p]) <= MINISCULE) {
        //std::cout << "***** Error matrix value too small. Matrix is singular" << endl;
        //exit;
    }

    //std::cout << "\nJust the pivoting...: " << endl;

    // Pivot /in A and b
    for(size_t i=p+1; i<N2; i++){
        //std::cout << "\nInside the 1st pivoting loop...: " << endl;

        //std::cout << "\nAbout to do the  [aMatrix[p][p]] division in back subst..: " << endl;
        float alpha = aMatrix[i][p] / aMatrix[p][p];

        bMatrix[i] = alpha * bMatrix[p];

        for(size_t j=p; j<N2; j++){
            //std::cout << "\nInside the 2nd pivoting loop...: " << endl;
            aMatrix[i][j] -= alpha * aMatrix[p][j];
        }

    }
    std::cout << "\nAbout to do the back subst..: " << endl;
    // back subst.
    float outMatrix[DEFROWS] = {0.0};

    for(size_t i=N2-1; i>=0; i--){
        std::cout << "\nInside the 1st back subst for loop..: " << endl;
        float sum = 0.0;

        for(size_t j=i+1; j<N2; j++){
            std::cout << "\nInside the 2nd back subst for loop..: " << endl;
            sum += aMatrix[i][j] * outMatrix[j];
        }

        std::cout << "\nAbout to do the [aMatrix[i][i]] division in back subst..: " << endl;

            std::cout << "\n*outMatrix[i]: " << outMatrix[i] << endl;
            std::cout << "\n( bMatrix[i] - sum ) : " << ( bMatrix[i] - sum )  << endl;
            std::cout << "\n****aMatrix[i][i] : " << aMatrix[i][i]  << endl;            

        if (aMatrix[i][i] > 0){
            std::cout << "\nDid the division [aMatrix[i][i]] > 0 division in back subst..: " << endl;
            std::cout << "\n*outMatrix[i]: " << outMatrix[i] << endl;
            std::cout << "\n( bMatrix[i] - sum ) : " << ( bMatrix[i] - sum )  << endl;
            std::cout << "\naMatrix[i][i] : " << aMatrix[i][i]  << endl;

            outMatrix[i] =  ( bMatrix[i] - sum ) / aMatrix[i][i];

            std::cout << "\nDid the division [aMatrix[i][i]] > 0 division in back subst..DONE: " << endl;

        }else {
            std::cout << "\nDid the divirion [aMatrix[i][i]] = 0 division in back subst..: " << endl;
            outMatrix[i] = 0.0;
            std::cout << "\nDid the divirion [aMatrix[i][i]] = 0 division in back subst..DONE: " << endl;
        }

        std::cout << "\nDid the [aMatrix[i][i]] division in back subst..: " << endl;
    }

    std::cout << "\nLeft the back subst for loops..: " << endl;

    return outMatrix;
}  
}  // end solveMatrix()

我的问题是我的程序似乎运行但似乎已经超过矩阵结束并崩溃。另外,我得到一些大的指数数字,我在数组中的最大数字是1-10。

这里是输出的截图:似乎无法从剪切工具粘贴。但问题似乎是在“反向替代”开始之后开始的。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我不确定所有问题是什么,但一个明确的问题是你的outMatrix。您正在返回指向本地数组的指针。数组在函数结束时被销毁。你要返回的指针现在是垃圾。

更改此行:

float outMatrix[DEFROWS] = {0.0};

要:

float* outMatrix = new float[DEFROWS];

然后你需要将其归零。

注意:如果你使用vector<float>并返回它会更好,但你用原始指针编写它,所以我用原始指针回答。原始指针导致内存泄漏。应尽可能避免使用它们。使用现代C ++,很少需要分配原始指针。

编辑: 见Effective C++ Item #31

EDIT2:更改为使用向量,如果您的编译器支持C ++ 11标准中定义的移动运算符和构造函数,则返回向量是一种廉价的操作。

std::vector<float> solveMatrix(/* Your arguments */)
{

    std::vector<float> outMatrix(DEFROWS, 0.0);

    // Your code

    return outMatrix;
}