-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGaussianElimination.c
310 lines (284 loc) · 7.69 KB
/
GaussianElimination.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int compare(double x,double y);
void findParticularSolution(int row,int col,double** a,double* b);
int checkPossibleColumns(int r1,int c1,int row,int col,double** a,double* b);
int r_exchange(int r,int c,int r1,int c1,double** a,double* b);
void printMatrix(int r,int c,double** a,double* b);
void solve(int r,int c,double** a,double* b);
int solveTallMatrix(int r,int c,double** a,double* b);
void backSubstitution(int c,int r,double** a,double* b);
int nullSpaceFinder(int row,int col,double** a);
int compare(double x,double y){ // compares whether x=y precisely
double epsilon = 1e-8;
if(fabs(x-y)<epsilon) return 1;
else return 0;
}
void findParticularSolution(int r,int c,double** a,double* b) // finds particular solution of system of equations
{
double value[c],sum=0;
int i,j;
for(i=r-1;i>=0;i--)
{
for(j=0;j<c;j++)
{
value[j] = 0;
}
sum=0;
int zz = i;
for(j=c-1;j>=0;j--){
if(i!=j && !compare(a[i][j],0) && !compare(b[i],0))
{
value[j] = 1;
}
sum+=value[j]*a[i][j];
}
value[zz] = (b[zz]-sum)/a[i][i];
}
int alpha = 97;
printf("\nParticular Solution is \n");
printf("-----------------------------------------\n");
for(i=0;i<c;i++)
{
printf("%c = %f\n",alpha,value[i]);
alpha++;
}
printf("-----------------------------------------\n");
}
int checkPossibleColumns(int r1,int c1,int row,int col,double** a,double* b) //checks whether there are any other columns such that its value for row1 is nonzero.(Possibility of pivot for FAT matrix)
{
int i;
for (i = c1+1; i < col; ++i)
{
if(a[r1][i]!=0)
{
return 1;
}
}
return -1;
}
int matrixType(int row,int col,double** a,double* b) //Returns type of matrix.
{
if(row>col)
return 1; //Tall Matrix
else if(row<col)
return -1; //Fat Matrix
else
return 0; //Square Matrix
}
void GaussianElimination(int r,int c,double** a,double* b){ //Performs Gaussian Elimination
int type = matrixType(r,c,a,b);
if(type==1) //Tall Matrix
{
printf("\n--------Tall Matrix-------\n");
int i,j,k,z;
for (i = 0,j=0; j < c; ++i,++j)
{
if(!compare(a[i][j],0)) //Pivot is nonzero
{
for (k = i+1; k < r; ++k) //Makes all elements below pivot element zero
{
double ratio = (!compare(a[k][j],0)) ? a[k][j]/a[i][j] : 0;
for(z = 0;z<c;z++)
{
a[k][z] -= a[i][z]*ratio;
}
b[k] = b[k] - b[i]*ratio;
}
//printMatrix(r,c,a,b);
}
else{ //Pivot is zero
int flag = r_exchange(r,c,i,j,a,b); //Check if row exchange is possible
if(flag>=0 && compare(b[i],0)) //Row exchange not possible and consistent equation
{
printMatrix(r,c,a,b);
printf("\n------------------------------\n");
printf(" Infinite Solutions \n");
printf("------------------------------\n");
findParticularSolution(r,c,a,b);
return;
} //Inconsistent equation
else if(flag>=0){
printMatrix(r,c,a,b);
printf("\n------------------------------\n");
printf(" NO Solutions\n");
printf("------------------------------\n");
return;
}
else{ //Rows exchanged successfully.
i--;
j--;
//printMatrix(r,c,a,b);
}
}
}
printMatrix(r,c,a,b);
int flag = solveTallMatrix(r,c,a,b);
if(flag==-1)
{
printMatrix(r,c,a,b);
printf("\n-----------------------------------\n");
printf(" No solutions exists\n");
printf("-----------------------------------\n");
return;
}
}
else if(type==-1) //Fat Matrix
{
printf("\n-------FAT matrix-------\n");
//printMatrix(r,c,a,b);
int i,j;
for (i = 0,j=0; i < r; ++i,++j)
{
double pivotValue = a[i][j];
if(compare(a[i][j],0)) //Pivot is zero
{
int flag = r_exchange(r,c,i,j,a,b); //Check for row exchange
if(flag>=0)
{
if(j!=(c-1)) //Row exchange not possible but next column is nonzero
{
i--;
}
else{
printMatrix(r,c,a,b);
if(b[i]==0){ //Consistent equation but pivot is zero
printf("\n-----------------------------------\n");
printf(" Infinite Solutions possible\n");
printf("-----------------------------------\n");
findParticularSolution(r,c,a,b);
return;
}
else{ //Inconsistent equation
printf("\n-----------------------------------\n");
printf(" No solutions possible\n");
printf("-----------------------------------\n");
return;
}
}
}
//printMatrix(r,c,a,b);
}
else{ //Nonzero pivot
int z;
for (z = 0; z < c; ++z)
{
if(!compare(a[i][z],0))
{
a[i][z]/=pivotValue;
}
}
if(!compare(b[i],0)){
b[i]/=pivotValue;
}
//printMatrix(r,c,a,b);
int k;
for (k = i+1; k < r; ++k) //Make all elements below the pivot as 0
{
double ratio = (!compare(a[k][j],0)) ? (double)a[k][j]/a[i][j] : 0;
for (z = 0; z < c; ++z)
{
if(!compare(a[i][z],0) && !compare(ratio,0)){
a[k][z] -= a[i][z]*ratio;
}
if(compare(a[k][z],0))
{
a[k][z] = 0;
}
}
b[k] -= b[i]*ratio;
if(compare(b[k],0))
b[k] = 0;
//printMatrix(r,c,a,b);
}
}
}
printMatrix(r,c,a,b);
backSubstitution(c,r,a,b);
}
else{ //for square matrix
int i,k,z,j;
for (i = 0,j=0; i < r; ++i,++j)
{
if(!compare(a[i][j],0)){ //Nonzero pivot
for (k = i+1; k < r; ++k) //Elimination
{
double ratio = (double)a[k][j]/a[i][j];
for (z = j; z < c; ++z)
{
if(!compare(a[i][z],0) && !compare(ratio,0)){
a[k][z] = a[k][z] - a[i][z]*ratio;
}
}
if(!compare(b[i],0) && !compare(ratio,0)){
b[k] = b[k] - b[i]*ratio;
}
//printMatrix(r,c,a,b);
}
}
else{ //Pivot is zero
int flag = r_exchange(r,c,i,j,a,b); //Check if row exchange is possible
if(flag>=0 && compare(b[i],0)) //Consistent equation but pivot is zero
{
printMatrix(r,c,a,b);
printf("\n-------------------------------\n");
printf(" Infinte Solutions \n");
printf("-------------------------------\n");
findParticularSolution(r,c,a,b);
return;
}
else if(flag>=0) //Inconsistent Equations
{
printf("\n-------------------------------\n");
printf(" NO Solutions \n");
printf("-------------------------------\n");
return;
}
else{ //Row exchanged successfully
i--;
j--;
}
}
}
printMatrix(r,c,a,b);
backSubstitution(c,r,a,b);
}
}
int main()
{
printf("\n\n--------------------Linear Equation Solver--------------------\n\n");
printf("\nEnter rows and columns of matrix A : ");
int r,c;
scanf("%d %d",&r,&c);
printf("Enter %dx%d augmented matrix A \n", r,c+1);
double** a;
a = (double**)calloc(r,sizeof(double));
double** nullSpaceMatrix;
nullSpaceMatrix = (double**)calloc(r,sizeof(double));
int i;
for (i = 0; i < r; ++i)
{
a[i] = (double*)calloc(c,sizeof(double));
nullSpaceMatrix[i] = (double*)calloc(c,sizeof(double));
}
double *b;
b = (double*)calloc(r,sizeof(double));
int j;
for (i = 0; i < r; ++i)
{
for ( j = 0; j < c; ++j)
{
scanf("%lf",&a[i][j]);
nullSpaceMatrix[i][j] = a[i][j];
}
scanf("%lf",&b[i]);
}
GaussianElimination(r,c,a,b);
printf("\n\n------------Now calculating nullspace----------\n");
nullSpaceFinder(r,c,nullSpaceMatrix);
free(a);
free(b);
free(nullSpaceMatrix);
return 0;
}