用C中的部分旋转高斯计算矩阵行列式

时间:2011-10-19 16:29:16

标签: c matrix partial gaussian determinants

我正在尝试在C中创建一个简单的控制台应用程序,它将使用高斯部分旋转消除方法计算矩阵的行列式。我遇到的两个问题是: - 有人告诉我,某些矩阵不适用于这种方法(数学上讲),在谷歌上阅读文章后,我找不到那个特殊情况 - 经过大量测试后,我发现我的程序不能用于某些矩阵,经过2天的“浪费”时间编辑和撤消后,我找不到问题。

任何类型的改进都受到欢迎。我刚开始用C。

#include<stdio.h>
#include<cstdlib>
#include<math.h>
#include<conio.h>
#include<windows.h>

// calculate biggest element on column

int indice_max(int dim, int col, float coloana[20][20]) {

    float max = 0;
    int indice;

    for(int i = 1; i <= dim; i++)
        if(fabs(max) < fabs(coloana[i][col])) {
            max = coloana[i][col];
            indice = i;
        }

    return indice;

}

// permute 2 lines

void permutare_linie(int linie1, int linie2, int dim, float matrice[20][20]) {

    float aux;

    for(int i = 1; i <= dim; i++) {
        aux = matrice[linie1][i];
        matrice[linie1][i] = matrice[linie2][i];
        matrice[linie2][i] = aux;
    }

}

// print matrix

void afisare_matrice(int dimensiune, float matrice[20][20], int lpiv) {

    for(int i = 1; i<= dimensiune; i++) {
        for(int j = 1; j <= dimensiune; j++) {
            if(i == lpiv)
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_GREEN);
            else
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE );
            printf("%4.2f ", matrice[i][j]);
        }
        printf("\n");
    }

}


void main(void) {

    float matrice[20][20];
    int dimensiune ;
    float rezultat = 1;
    float pivot;
    int lpiv;
    int cpiv;
    int optiune;
    while(1) {

        // MENU

        printf("ALEGET OPTIUNEA:\n");
        printf("1) Calculate matrix determinant\n");
        printf("2) Exit\n");
        scanf("%d", &optiune);

        if(optiune == 1) {

            // Read determinant dimension

            printf("Matrix dimension:");
            scanf("%d", &dimensiune);

            // Read determinant

            for(int i = 1; i <= dimensiune; i++)
                for(int j = 1; j <= dimensiune; j++) {
                    printf("M[%d][%d]=", i, j);
                    scanf("%f", &matrice[i][j]);
            }

            // pivot initial coords

            lpiv = 1;
            cpiv = 1;

            printf("\n----- Entered Matrix -----\n\n");
            afisare_matrice(dimensiune, matrice, 0);
            printf("\n");

            for(int pas = 1; pas <= dimensiune - 1; pas++) {

                if(fabs(matrice[lpiv][cpiv]) > fabs(matrice[indice_max(dimensiune, cpiv, matrice)][cpiv])) {
                    permutare_linie(lpiv, indice_max(dimensiune, cpiv, matrice), dimensiune, matrice);
                    rezultat = -(rezultat);
                }

                pivot = matrice[lpiv][cpiv];


                for(int inm = 1; inm <= dimensiune; inm++) {
                    matrice[lpiv][inm] = matrice[lpiv][inm] / pivot;
                }

                rezultat *= fabs(pivot);

                // transform matrix to a superior triangular 
                for(int l = lpiv+1; l <= dimensiune; l++)
                    for(int c=cpiv+1; c <= dimensiune; c++) {
                        matrice[l][c] -= matrice[l][cpiv] * matrice[lpiv][c] / matrice[lpiv][cpiv];
                    }

                for(int i = lpiv + 1; i <= dimensiune; i++)
                    matrice[i][cpiv] = 0;
                // afisam rezultat / pas

                printf("----- Step %d -----\n\n", pas);
                afisare_matrice(dimensiune, matrice, lpiv);
                printf("\nResult after step %d : %4.2f\n\n", pas, rezultat);
                lpiv++;
                cpiv++;
            }

            // final result

            rezultat = rezultat * matrice[dimensiune][dimensiune];
            printf("----- REZULTAT FINAL -----\n\n");
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_INTENSITY);
            printf("Rezultat = %4.2f\nRezultat rotunjit:%4.0f\n\n", rezultat, floorf(rezultat * 100 +  0.5) / 100);
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE );

        }
        else {
            exit(0);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

你的代码做了一些分工:

matrice[lpiv][inm] = matrice[lpiv][inm] / pivot;

如果它恰好除以零,则会发生错误。我猜这将发生在零矩阵上。

似乎您的代码实际上是在尝试反转矩阵,而不仅仅是计算行列式。