-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
230 lines (169 loc) · 4.94 KB
/
main.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* TODO:
* - Parser dos comandos da linha de comando
* - Salvar matriz no arquivo
* - Ler matriz da stdin
* - Imprimir resultado na stdout
* - Validar se a matriz é inversivel
* - Implementar refinamento
* - Implementar a medição de tempo
* - Implementar a geração de matrizes aleatórias
* - Implementar os comentários dyoxigen
* FIXME:
* - ✓ [DONE] Função LU_decomposition: Esqueci de implementar o pivotamento do vetor b onde (Ax = b)
* - melhorar backward and forward substitution
* -
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <unistd.h>
#include <ctype.h>
#include "matrix.h"
double *mallocAndCheck();
void forwardSubstitutionIdentity();
void backwardSubstitution();
void LU_Decomposition();
void swap();
void print_matrix();
double *verifyIdentity();
int main(int argc, char *argv[]){
srand( 20172 );
int c;
int iterations; //iterative refinement
int n;
double *matrix;
while ((c = getopt(argc, argv, "i:o:r:")) != -1){
switch(c){
case 'i':
if( atoi(optarg) )
iterations = atoi(optarg);
else{
printf("set file %s\n", optarg);
}
break;
case 'o': printf("set output file");break;
case 'r':
n = atoi(optarg);
if( !(n > 1) || !(n < 32768) ) {
fprintf(stderr, "option [-r N] must be a valid number (1 < N < 32768) \n");
exit(1);
}
break;
}
}
if(!iterations){
fprintf(stderr, "The refinement iterations -i option must be provided\n");
exit(1);
}
/*Seção para testes do programa*/
double *U = generateSquareRandomMatrix(n);
double *T = mallocAndCheck(n);
//copy matrix U to Test
for(int i=0; i < n * n; ++i)
T[i] = U[i];
double *I = identityMatrix(n);
double *L = mallocAndCheck(n);
double *Z = mallocAndCheck(n);
double *inv = mallocAndCheck(n);
LU_Decomposition(U, L, I, n);
forwardSubstitutionIdentity(L, Z, I, n);
backwardSubstitution(U, Z, inv, n);
double *res = verifyIdentity(T, inv , n);
return 0;
}
/*==========================================================================================
* verifyIdentity
* Given a two matrices, returns the multiplication of the two.
*======================================================================================== */
double * verifyIdentity(double *a, double *b, int n){
double *out = malloc(sizeof(double) * n * n);
double *ptrA;
double *ptrB;
double *ptrOut = out;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
ptrA = &a[i * n];
ptrB = &b[j];
*ptrOut = 0;
for(int k = 0; k < n; k++){
*ptrOut += *ptrA * *ptrB;
ptrA++;
ptrB += n;
}
ptrOut++;
}
}
printf("Matriz A: \n");
print_matrix(a, n);
printf("Matriz B: \n");
print_matrix(b, n);
printf("Resultado \n");
print_matrix(out, n);
return (out);
}
void backwardSubstitution(double *U, double *Z, double *inv, int n){
for(int k=0; k < n; k++){
inv[(n-1) * n + k] = Z[(n-1) * n + k] / U[n * n - 1];
for(int i=n-2; i >= 0; i--){
inv[i*n + k] = Z[i*n + k];
for(int j = (n-1); j > i; --j)
inv[i*n + k] -= U[i*n + j] * inv[j*n + k];
inv[i*n + k] /= U[i*n + i];
}
}
}
void forwardSubstitutionIdentity(double *L, double *Z, double *I, int n){
//Laço mais externo itera sob cada coluna da matriz identidade
for (int k = 0; k < n; ++k){
Z[k] = I[k] / L[0];
for ( int i = 1; i < n; ++i ){
Z[i*n + k] = I[i*n+ k];
for( int j = 0; j < i; ++j)
Z[i*n + k] -= L[i*n + j] * Z[j* n + k];
Z[i * n + k] /= L[i*n + i];
}
}
}
void LU_Decomposition(double *U, double *L, double *I, int n){
double m;
int imax;
//fill up the main diagonal of L matrix with 1.0
for ( int i = 0 ; i < n ; i++)
L[i*n+i] = 1.0;
for ( int j = 0; j < n-1; ++j){
imax = j;
//checking pivoting criteria
for( int i = j+1; i < n ; ++i)
if( fabs(U[i*n + j]) > fabs(U[imax*n + j]))
imax = i;
if(imax != j){
swap(U, I, imax, j, n);
}
//NAÏVE GAUSS Elimination
for ( int i = j+1; i < n; ++i){
m = U[i*n + j] / U[j*n + j];
L[i*n + j] = m;
U[i*n + j] = 0.0;
for(int k = j+1; k < n; ++k)
U[i*n + k] -= m * U[j*n + k];
}
}
// print_matrix(U, n);
// print_matrix(L, n);
}
void swap(double* A, double * I, int imax, int j, int n){
double aux;
for(int i = 0; i < n; i++){
aux = A[imax * n + i];
A[imax * n + i] = A[j * n + i];
A[j * n + i] = aux;
/*
pivoting the Identity matrix to follow the pivoting in A
*/
aux = I[imax * n + i];
I[imax * n + i] = I[j * n + i];
I[j * n + i] = aux;
}
}