gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:27:32 27 4
gpt4 key购买 nike

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

任何类型的改进都是非常受欢迎的。我刚刚从 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);
}
}
}

最佳答案

你的代码做了一些划分:

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

如果碰巧除以零,就会发生错误。我猜零矩阵会发生这种情况。

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

关于在 C 中使用部分旋转高斯计算矩阵行列式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7824691/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com