-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlu.c
executable file
·101 lines (83 loc) · 2.04 KB
/
lu.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// For now use 2-dimension matrix, try array to see if more efficient
typedef double **mat;
// Fill the matrix with 0
void initZero(mat x, int n) {
int i,j;
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
x[i][j] = 0;
}
// Initialize matrix and fill with 0
mat initNew(int n) {
mat x = malloc(sizeof(double*) * n);
x[0] = malloc(sizeof(double) * n * n);
int i;
for (i = 0; i < n; i++)
x[i] = x[0] + n * i;
initZero(x,n);
return x;
}
// Clean up
void matDel(mat x) {
free(x[0]);
free(x);
}
// Copy matrix to proper type
mat matCopy(void *s, int n) {
mat x = initNew(n);
int i,j;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
x[i][j] = ((double(*)[n])s)[i][j];
return x;
}
// Actual Decompsition
// Now, matrix L should have all 1 on diagonal entries. And U is the
// upper triangular matrix of A after algorithm.
int LUDecomp(mat A, mat L, int n) {
// Declarations
int i, j, k, p;
double *ptr, *ptrRow, *ptrCol, temp;
// Standard Gaussian Elimination
for (k = 0; k < n - 1; k++) {
// Pivoting necessary (just return failure)
if (A[k][k] == 0) return -1;
L[k][k] = 1.0;
for (i = k + 1; i < n; i++)
l[i][k] = A[i][k] / A[k][k]; // Compute current column of L
for (j = k + 1; j < n; j++) {
for (i = k + 1; i < n; i++) {
A[i][j] -= l[i][k] * A[k][j]; // Update submatrix of A
}
}
}
// Success
return 1;
}
void printMatrix(mat x, int n) {
int i,j;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%8.4g", x[i][j]);
printf(j < n - 1 ? " " : i == n - 1 ? "\n\n" : "\n");
}
}
}
int main() {
double A1[][3] = {{8,2,9}, {4,9,4}, {6,7,9}};
mat A = matCopy(A1,3);
mat L1 = initNew(3);
int res = LUDecomp(A,L1,3);
if (res == 1) {
printMatrix(A,3);
printMatrix(L1,3);
}
else
printf("Need pivoting.");
matDel(A);
matDel(L1);
return 0;
}