LU-분해법
다인
질문 제목 : lu-분해법질문 요약 :하부삼각행렬 l에서 0이 제대로 출력이 안됨질문 내용 :
#include stdio.h
#include stdlib.h
#include time.h
static double determinant(double** matrixa, int uesrn);
static int lufactorization(double** matrixa, double** matrixb, int usern);
static int multiplicationmatrix(double** matrixa, double** matrixb, double** resultmatrix, int usern);
static int findmatrixd(double** matrixl, double* matrixd, int count, int usern);
static int findmatrixx(double** matrixu, double* matrixd, double* matrixx, int usern);
int main(void)
{
int usern = 0, userm = 0, row = 0, column = 0;
double determinantresult = 0;
double* matrixd;
double* matrixx;
double** matrixa;
double** matrixl = 0;
double** matrixm = 0;
double** matrixinversea = 0;
printf(n by n = );
scanf(%d, &usern);
userm = usern - 1;
matrixd = (double*)malloc(sizeof(double) * usern);
matrixx = (double*)malloc(sizeof(double) * usern);
matrixa = (double**)malloc(sizeof(double*) * usern);
matrixl = (double**)malloc(sizeof(double*) * usern);
matrixm = (double**)malloc(sizeof(double*) * usern);
matrixinversea = (double**)malloc(sizeof(double*) * usern);
for (row = 0; row usern; row++)
{
matrixa[row] = (double*)malloc(sizeof(double) * usern);
matrixl[row] = (double*)malloc(sizeof(double) * usern);
matrixm[row] = (double*)malloc(sizeof(double) * usern);
matrixinversea[row] = (double*)malloc(sizeof(double) * usern);
}
for (row = 0; row usern; row++)
{
matrixl[row][row] = 1;
}
srand(time(null));
for (row = 0; row usern; row++)
{
for (column = 0; column usern; column++)
{
matrixa[row][column] = rand() % (20 -10) / 1.0;
}
}
printf(\na =\n);
for (row = 0; row usern; row++)
{
for (column = 0; column usern; column++)
{
printf(%10.5lf , matrixa[row][column]);
if (column % usern == userm)
{
printf(\n);
}
}
}
determinantresult = determinant(matrixa, usern);
printf(\n|a| = %.5g\n, determinantresult);
if (!determinantresult)
{
printf(\nno determinant(a)\n);
}
else
{
lufactorization(matrixa, matrixl, usern);
printf(\nl =\n);
for (row = 0; row usern; row++)
{
for (column = 0; column usern; column++)
{
printf(%10.5lf , matrixl[row][column]);
if(column % usern == userm)
{
printf(\n);
}
}
}
printf(\nu =\n);
for (row = 0; row usern; row++)
{
for (column = 0; column usern; column++)
{
printf(%10.5lf , matrixa[row][column]);
if(column % usern == userm)
{
printf(\n);
}
}
}
multiplicationmatrix(matrixl, matrixa, matrixm, usern);
printf(\nlu =\n);
for (row = 0; row usern; row++)
{
for (column = 0; column usern; column++)
{
printf(%10.5lf , matrixm[row][column]);
if(column % usern == userm)
{
printf(\n);
}
}
}
for(row = 0; row usern; row ++)
/{
findmatrixd(matrixl, matrixd, row, usern);
findmatrixx(matrixa, matrixd, matrixx, usern);
for (column = 0; column usern; column++)
{
matrixinversea[column][row] = matrixx[column];
}
}
printf(\ninv(a) =\n);
for (row = 0; row usern; row++)
{
for (column = 0; column usern; column++)
{
printf(%10.5lf , matrixinversea[row][column]);
if (column % usern == userm)
{
printf(\n);
}
}
}
}
for (row = 0; row usern; row++)
{
free(matrixa[row]);
free(matrixl[row]);
free(matrixm[row]);
free(matrixinversea[row]);
}
free(matrixa);
free(matrixl);
free(matrixm);
free(matrixinversea);
return 0;
}
static double determinant (double** matrixa, int usern)
{
int userm = 0;
int counter = 0;
int determinantacheck = 0;
int determinantarow = 0;
int determinantacolumn = 0;
int sign = 1;
double determinantresult = 0;
double** matrixdeterminanta;
userm = usern - 1;
matrixdeterminanta = (double**)malloc(sizeof(double*) * userm);
for (counter = 0; counter userm; counter++)
{
matrixdeterminanta[counter] = (double*)malloc(sizeof(double) * userm);
}
if (usern == 2)
{
determinantresult = (matrixa[0][0] * matrixa[1][1]) - (matrixa[0][1] * matrixa[1][0]);
}
else
{
for (counter = 0; counter usern; counter++)
{
for (determinantarow = 0; determinantarow userm; determinantarow++)
{
determinantacheck = 0;
for (determinantacolumn = 0; determinantacolumn userm; determinantacolumn++)
{
if (determinantacolumn == counter)
{
determinantacheck++;
}
matrixdeterminanta[determinantarow][determinantacolumn] = matrixa[determinantarow + 1][determinantacheck];
determinantacheck++;
}
}
if (counter % 2 == 1)
{
sign = -1;
}
else
{
sign = 1;
}
determinantresult += sign * matrixa[0][counter] * determinant(matrixdeterminanta,userm);
}
}
for(counter = 0; counter userm; counter++)
{
free(matrixdeterminanta[counter]);
}
free(matrixdeterminanta);
return determinantresult;
}
static int lufactorization(double** matrixu, double** matrixl, int usern)
{
int row = 0;
int column = 0;
int count = 0;
double division=0;
for (count = 0; count (usern - 1); count++)
{
for (row = count; row (usern -1); row++)
{
division = matrixu[row + 1][count] / matrixu[count][count];
for (column = 0; column usern; column++)
{
matrixu[row + 1][column] -= division * matrixu[count][column];
}
matrixl[row + 1][count] = division;
}
}
return 0;
}
static int multiplicationmatrix(double** matrixa, double** matrixb, double** resultmatrix, int usern)
{
int row = 0;
int column = 0;
int check = 0;
for (row = 0; row usern; row++)
{
for (column = 0; column usern; column++)
{
for (check = 0; check usern; check++)
{
resultmatrix[row][column] += matrixa[row][check] * matrixb[check][column];
}
}
}
return 0;
}
static int findmatrixd(double** matrixl, double* matrixd,int count, int usern)
{
int row = 0;
int column = 0;
double sum = 0;
double* matrixi = 0;
matrixi = (double*)malloc(sizeof(double) * usern);
matrixi[count] = 1;
matrixd[0] = matrixi[0] / matrixl[0][0];
for (row = 1; row usern; row++)
{
sum = matrixi[row];
for (column = 0 ; column row; column++)
{
sum -= ( matrixl[row][column] * matrixd[column] );
}
matrixd[row] = sum / matrixl[row][row];
}
return 0;
}
static int findmatrixx(double** matrixu, double* matrixd, double* matrixx, int usern)
{
int row = 0;
int column = 0;
double sum = 0;
matrixx[usern - 1] = matrixd[usern - 1] / matrixu[usern - 1][usern - 1];
for (row = (usern - 2); row = 0; row--)
{
sum = matrixd[row];
for (column = (usern - 1); column row; column--)
{
sum -= (matrixu[row][column] * matrixx[column]);
}
matrixx[row] = sum / matrixu[row][row];
}
return 0;
}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2695766 | 달팽이 배열 어디서 틀렸는지 모르겠습니다ㅠㅠ | 연분홍 | 2025-05-23 |
2695738 | fopen과fclose질문~~ (5) | 희선 | 2025-05-23 |
2695707 | 3의 배수 나타내기. (2) | 수리 | 2025-05-23 |
2695626 | 피보나치수열 과제 때문에 질문 드립니다. (6) | 옆집언니 | 2025-05-22 |
2695595 | 포인트공부중입니다 int형에서 4=1 인가요? (3) | 족장 | 2025-05-22 |
2695567 | 드라이브 고유번호를 가져오는 함수 (2) | 초코맛사탕 | 2025-05-21 |
2695533 | 음수의 산술변환! 질문이요 ㅠㅠ... (4) | 꽃여름 | 2025-05-21 |
2695506 | 구조체 배열 이용 도서목록 출력 프로그램 (1) | 가을귀 | 2025-05-21 |
2695450 | c언어 함수 질문이요.... | 이슬비 | 2025-05-20 |
2695403 | VirtualAlloc함수 및 메모리 질문 | 크리에이터 | 2025-05-20 |
2695355 | c언어 for함수 | 미쿡 | 2025-05-19 |
2695327 | 안녕하세요 제가 이번에 좀 큰 프로그램을.. | 악당 | 2025-05-19 |
2695295 | mutex동기화의 thread기반 채팅 서버소스 질문입니다 | 그루터기 | 2025-05-19 |
2695270 | 질문이요..swap 관한겁니다..ㅠㅠ (3) | 콩알녀 | 2025-05-19 |
2695244 | 노땅초보궁금한게 하나 있는데요..반복문(while문)초보자질문 (6) | 큰꽃늘 | 2025-05-18 |
2695166 | do while 문 어떤것이잘못된건지 모르겠어요 (2) | 아이폰 | 2025-05-18 |
2695122 | 구조체에 대해 물어보고 싶은게 있습니다 ^^^.. (7) | 수련 | 2025-05-17 |
2695091 | txt 파일 입출력 후 2차 배열에 저장하기입니다. (3) | 헛장사 | 2025-05-17 |
2695063 | 수도요금 프로그램좀 짜주세요. | 시내 | 2025-05-17 |
2695033 | 답변좀요ㅠㅠ (1) | 비사벌 | 2025-05-16 |