-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransform_To_RREF.c
280 lines (254 loc) · 5.43 KB
/
Transform_To_RREF.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
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
int n,pt=-1;
int compare(double x,double y);
void nullSpaceMatrix(int row,int col,double** a,double* F);
void rref(int row,int col,double** a,int* columnSwaps);
int checkColumnExchange(int pos,int row,int col,double** a,int* columnSwaps);
int checkRow(int pos,int row,int col,double** a,int* columnSwaps);
void swaprow(int r1,int r2,int row,int col,double** a,int* columnSwaps);
int shiftRow(int pos,int row,int col,double** a,int* columnSwaps);
void columnExchange(int col1,int col2,int row,int col,double** a,int* columnSwaps);
void extractF(int pos,int row,int col,double** a,int* columnSwaps);
void printMatrixx(int r,int c,double** a);
int isZero(int row,int col,double** a);
int isZero(int row,int col,double** a) //Checks if matrix is zero
{
int cc=0,i,j;
for (i = 0; i < row; ++i)
{
for (j = 0; j < col; ++j)
{
if(compare(a[i][j],0))
{
cc++;
}
}
}
if(cc==(row*col))
return 1;
else
return 0;
}
void printMatrixx(int r,int c,double** a) //prints rref form of matrix
{
printf("\nMatrix is \n");
int i,j;
for (i = 0; i < r; ++i)
{
for (j = 0; j < c; ++j)
{
printf("%f ",a[i][j] );
}
printf("\n");
}
printf("\n");
}
int checkColumnExchange(int pos,int row,int col,double** a,int* columnSwaps) //checks if column exchange is possible
{
int i;
for (i = pos+1; i < col; ++i)
{
double tmp_pivot = a[pos][i];
if(!compare(tmp_pivot,0)) //nonzero
{
columnExchange(pos,i,row,col,a,columnSwaps);
return 1;
}
}
int flag = shiftRow(pos,row,col,a,columnSwaps);
return flag;
}
int checkRow(int pos,int row,int col,double** a,int* columnSwaps) //checks if row is zero
{
int c=0,i;
for (i = 0; i < col; ++i)
{
if(compare(a[pos][i],0)) //zero
{
c++;
}
}
if(c==col)
return 1;
else
return 0;
}
void swaprow(int r1,int r2,int row,int col,double** a,int* columnSwaps) //swaps row r1 and r2
{
double* tmp;
tmp = (double*)calloc(col,sizeof(double));
int i;
for (i = 0; i < col; ++i)
{
tmp[i] = a[r1][i];
}
for (i = 0; i < col; ++i)
{
a[r1][i] = a[r2][i];
}
for (i = 0; i < col; ++i)
{
a[r2][i] = tmp[i];
}
free(tmp);
}
int shiftRow(int pos,int row,int col,double** a,int* columnSwaps) //shifts all zero rows to the bottom of matrix
{
int i;
for (i = pos+1; i < row; ++i)
{
if(!checkRow(i,row,col,a,columnSwaps))
{
swaprow(pos,i,row,col,a,columnSwaps);
return 0;
}
}
return -1;
}
void columnExchange(int col1,int col2,int row,int col,double** a,int* columnSwaps) //exchange col1,col2
{
double* temp;
temp = (double*)calloc(row,sizeof(double));
int i;
for (i = 0; i < row; ++i)
{
temp[i] = a[i][col1];
}
for (i = 0; i < row; ++i)
{
a[i][col1] = a[i][col2];
}
for (i = 0; i < row; ++i)
{
a[i][col2] = temp[i];
}
//printMatrixx(row,col,a);
pt++;
columnSwaps[pt] = col1;
pt++;
columnSwaps[pt] = col2;
free(temp);
}
void extractF(int rank,int row,int col,double** a,int* columnSwaps) //Forms F from rref form of matrix [I F]
{
printMatrixx(row,col,a);
int c = col-rank;
double** F;
F = (double**)calloc(col,sizeof(double));
int i,j;
for (i = 0; i < col; ++i)
{
F[i] = (double*)calloc(c,sizeof(double));
}
for (i = 0; i < col; ++i)
{
if(i<rank){
for (j = 0; j < c; ++j)
{
F[i][j] = a[i][rank+j];
if(F[i][j]!=0)
{
F[i][j] = -F[i][j];
}
}
}
else{
for (j = 0; j < c; ++j)
{
if(j==(i-rank)){
F[i][j] = 1;
}
else{
F[i][j] = 0;
}
}
}
}
if(columnSwaps[0]!=-1) //columns were exchanged
{
printf("\nSwapping rows as per previous operations\n");
while(pt>0)
{
swaprow(columnSwaps[pt],columnSwaps[pt-1],col,c,F,columnSwaps);
pt = pt - 2;
}
}
printf("\n---------------Nullspace--------------- \n");
printMatrixx(col,c,F);
free(F);
}
void rref(int row,int col,double** a,int* columnSwaps) //forms rref form of matrix
{
if(isZero(row,col,a))
{
printf("\n--------------------------------------\n");
printf(" Nullspace belongs to whole R%d\n",col );
printf("--------------------------------------\n");
return;
}
int rank=0,i,j;
int out = 0;
int nullity=0;
int maxPossiblePivots = 0;
if(row<col)
maxPossiblePivots = row;
else
maxPossiblePivots = col;
for (i = 0; i < maxPossiblePivots; ++i)
{
double pivot = a[i][i];
if(pivot==0)
{
int flag = checkColumnExchange(i,row,col,a,columnSwaps);
if(flag==-1) // exchange possible
{
out = i;
break;
}
else{ // pivot exchanged
i--;
}
}
else{ //elimination to form rref form
rank++;
double div = a[i][i];
for (j = 0; j < col; ++j)
{
if(a[i][j]!=0){
a[i][j]/=div;
}
}
for (j = 0; j < row; ++j)
{
if(j!=i){
double k = a[j][i]/a[i][i];
for (int z = i; z < col; ++z)
{
a[j][z] = a[j][z] - a[i][z]*k;
}
//printMatrixx(row,col,a);
}
}
}
}
printf("\nrank is %d\n",rank);
nullity = col-rank;
if(nullity==0)
{
printMatrixx(row,col,a);
printf("\nNull Space consists of zero vector only\n");
}
else if(rank==maxPossiblePivots)
{
extractF(rank,row,col,a,columnSwaps);
}
else{
if(out>0)
{
extractF(out,row,col,a,columnSwaps);
}
}
}