-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecomposicaoLU.c
62 lines (49 loc) · 1.1 KB
/
decomposicaoLU.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "matrix.h"
void decomposicaoLU(matrix *A, int n, matrix *L, matrix *U){
int i, j, k;
double soma;
printf("\nEntrada - Decomposição LU\n");
printf("\nA =\n");
printMatrix(A);
setElement(L, 1, 1, 1);
setElement(L, 2, 2, 1);
setElement(L, 3, 3, 1);
printf("\nL =\n");
printMatrix(L);
printf("\nU =\n");
printMatrix(U);
for(j=1; j<=n; j++){
for(i=1; i<=j; i++){
soma = 0;
for(k=1; k<=i-1; k++){
double a_ik, a_kj;
getElement(L, i, k, &a_ik);
getElement(U, k, j, &a_kj);
soma += a_ik * a_kj;
}
double a_ij;
getElement(A, i, j, &a_ij);
setElement(U, i, j, a_ij - soma);
} // U
for(i=j+1; i<=n; i++){
soma = 0;
for(k=1; k<=j-1; k++){
double a_ik, a_kj;
getElement(L, i, k, &a_ik);
getElement(U, k, j, &a_kj);
soma += a_ik * a_kj;
}
double a_ij, u_jj;
getElement(A, i, j, &a_ij);
getElement(U, j, j, &u_jj);
setElement(L, i, j, (a_ij - soma)/u_jj );
} // L
}
printf("\nSaída - Decomposição LU\n");
printf("\nA =\n");
printMatrix(A);
printf("\nL =\n");
printMatrix(L);
printf("\nU =\n");
printMatrix(U);
}