-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLUDecomposition.c
More file actions
141 lines (124 loc) · 3.13 KB
/
LUDecomposition.c
File metadata and controls
141 lines (124 loc) · 3.13 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
main()
{
int i,j,k,n,i_max;
float**A,**U,**L,*x,*y,*b,temp,max;
FILE*f_in,*f_out,*f_t;
f_in=fopen("inputMatrix.txt","r");
fscanf(f_in,"%d",&n);
A=(float**)malloc(n*sizeof(float*));
for(i=0;i<n;i++)
{
A[i]=(float*)malloc(n*sizeof(float));
}
L=(float**)malloc(n*sizeof(float*));
for(i=0;i<n;i++)
{
L[i]=(float*)malloc(n*sizeof(float));
}
U=(float**)malloc(n*sizeof(float*));
for(i=0;i<n;i++)
{
U[i]=(float*)malloc(n*sizeof(float));
}
b=(float*)malloc(n*sizeof(float));
x=(float*)malloc(n*sizeof(float));
y=(float*)malloc(n*sizeof(float));
printf("Initializing Input\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
fscanf(f_in,"%f",&A[i][j]);
}
}
for(i=0;i<n;i++)
{
fscanf(f_in,"%f",&b[i]);
}
printf("Input Successfully Completed\n");
printf("Initializing Solution of the equations\n");
printf("\tInitializing LU Decomposition\n");
clock_t start_t, end_t, total_t;
start_t = clock();
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
L[i][j]=0;
U[i][j]=0;
}
}
for(i=0;i<n;i++)
{
U[i][i]=1;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(j<=i)
{
L[i][j]=A[i][j];
for(k=0;k<j;k++)
{
L[i][j]-=L[i][k]*U[k][j];
}
}
else
{
U[i][j]=A[i][j];
for(k=0;k<i;k++)
{
U[i][j]-=L[i][k]*U[k][j];
}
U[i][j]/=L[i][i];
}
}
}
printf("\tLU Decomposition Successfull\n");
// for(i=0;i<n;i++)
// {
// for(j=0;j<n;j++)
// {
// printf("%f\t",L[i][j]);
// }
// printf("\n");
// }
printf("\tInitializing Forward Substitution\n");
for(i=0;i<n;i++)
{
y[i]=b[i];
for(j=0;j<i;j++)
{
y[i]-=L[i][j]*y[j];
}
y[i]/=L[i][i];
}
printf("\tForward Substitution Successfully Completed\n");
printf("\tInitializing Backward Substitution\n");
for(i=n-1;i>=0;i--)
{
x[i]=y[i];
for(j=n-1;j>i;j--)
{
x[i]-=U[i][j]*x[j];
}
}
printf("\tBackward Substitution Successfully Completed\n");
printf("Solution of the Equations Obtained Successfully\n");
end_t = clock();
total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC *1000;
f_t=fopen("LUD_time.txt","a");
fprintf(f_t,"%d\t%f\n",n,(double)total_t);
printf("Total time taken for the Computation was %f ms\n", (double)total_t );
f_out=fopen("LUDecompositionSolution.txt","w");
printf("Initializing Output of the Solution\n");
for(i=0;i<n;i++)
{
fprintf(f_out,"%f\n",x[i]);
}
printf("Output Successfully Completed\n");
}