-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmatrix.c
543 lines (497 loc) · 11 KB
/
matrix.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "matrix.h"
#include "qc_mdpc.h"
#include "utility.h"
//initialize the matrix
bin_matrix mat_init(int rows, int cols)
{
if(rows <= 0 || cols <= 0)
{
return NULL;
}
bin_matrix A;
A = (bin_matrix)safe_malloc(sizeof(struct matrix));
A->cols = cols;
A->rows = rows;
A->data = (unsigned short *)safe_malloc(rows*cols*sizeof(unsigned short));
return A;
}
#define mat_element(mat, row_idx, col_idx) \
mat->data[row_idx * (mat->cols) + col_idx]
//Return the matrix element at position given by the indices
unsigned short get_matrix_element(bin_matrix mat, int row_idx, int col_idx)
{
if(row_idx < 0 || row_idx >= mat->rows || col_idx < 0 || col_idx >= mat->cols)
{
printf("Matrix index out of range\n");
exit(0);
}
return mat->data[row_idx * (mat->cols) + col_idx];
}
//Set the value of matix element at position given by the indices to "val"
void set_matrix_element(bin_matrix A, int row_idx, int col_idx, unsigned short val)
{
if(row_idx < 0 || row_idx >= A->rows || col_idx < 0 || col_idx >= A->cols)
{
printf("Matrix index out of range\n");
exit(0);
}
mat_element(A, row_idx, col_idx) = val;
}
//Set the indicated row of the matrix A equal to the vector vec
void set_matrix_row(bin_matrix A, int row, unsigned short* vec)
{
if(row < 0 || row >= A->rows)
{
printf("Row index out of range\n");
exit(0);
}
for(int i = 0; i < A->cols; i++)
{
set_matrix_element(A, row, i, vec[i]);
}
}
//Delete the matrix and free the space in memory
void delete_matrix(bin_matrix A)
{
free(A);
}
//Return the transpose of the matrix A
bin_matrix transpose(bin_matrix A)
{
bin_matrix B;
B = mat_init(A->cols, A->rows);
for(int i = 0; i < A->rows; i++)
{
for(int j = 0; j < A->cols; j++)
{
set_matrix_element(B, j, i, mat_element(A, i, j));
}
}
return B;
}
//Copy the data of matrix A to matrix B
bin_matrix mat_copy(bin_matrix A)
{
bin_matrix B;
int i;
B = mat_init(A->rows, A->cols);
memcpy(B->data, A->data, (A->rows)*(A->cols)*(sizeof(unsigned short)));
return B;
}
//Add row1 to row2 of matrix A
bin_matrix add_rows(bin_matrix A,int row1, int row2)
{
if(row1 < 0 || row1 >= A->rows || row2 < 0 || row2 >= A->rows)
{
printf("Matrix index out of range\n");
exit(0);
}
for(int i = 0; i < A->cols; i++)
{
mat_element(A, row2, i) = (mat_element(A, row1, i) ^ mat_element(A, row2, i));
}
return A;
}
//Add two matrices
bin_matrix add_matrix(bin_matrix A, bin_matrix B)
{
if(A->rows != B->rows || A->cols != B->cols)
{
printf("Incompatible dimenions for matrix addition.\n");
exit(0);
}
bin_matrix temp = mat_init(A->rows, A->cols);
for(int i = 0; i < A->rows; i++)
{
for(int j = 0; j < A->cols; j++)
{
set_matrix_element(temp, i, j, (mat_element(A, i, j) ^ mat_element(B, i, j)));
}
}
return temp;
}
//Function to swap two rows of matrix A
void swap(bin_matrix A, int row1, int row2)
{
if(row1 < 0 || row1 >= A->rows || row2 < 0 || row2 >= A->rows)
{
printf("Matrix index out of range\n");
exit(0);
}
int temp;
for(int i = 0; i < A->cols; i++)
{
temp = mat_element(A, row1, i);
mat_element(A, row1, i) = mat_element(A, row2, i);
mat_element(A, row2, i) = temp;
}
}
//Function to obtain the row reduced echlon form of a matrix A
bin_matrix matrix_rref(bin_matrix A)
{
int lead = 0;
int row_count = A->rows;
int col_count = A->cols;
bin_matrix temp = mat_init(row_count, col_count);
temp = mat_copy(A);
int r = 0;
while(r < row_count)
{
if(mat_element(temp, r, r) == 0)
{
int i;
for(i = r + 1; i < temp->rows; i++)
{
if(mat_element(temp, i, r) == 1)
{
swap(temp, r, i);
break;
}
}
if(i == row_count)
{
printf("Matix cannot be transformed into row echlon form...");
exit(1);
}
}
else
{
for(int i = 0; i < row_count; i++)
{
if(mat_element(temp, i, r) == 1 && i != r)
{
add_rows(temp, r, i);
}
}
r++;
}
}
return temp;
}
//Multiplication of two matrices A and B stored in C
bin_matrix matrix_mult(bin_matrix A, bin_matrix B)
{
if (A->cols != B->rows)
{
printf("Matrices are incompatible, check dimensions...\n");
exit(0);
}
bin_matrix C;
C = mat_init(A->rows, B->cols);
bin_matrix B_temp = transpose(B);
for(int i = 0; i < A->rows; i++)
{
for(int j = 0 ; j < B->cols; j++)
{
unsigned short val = 0;
for(int k = 0; k < B->rows; k++)
{
val = (val ^ (mat_element(A, i, k) & mat_element(B_temp, j, k)));
}
mat_element(C, i, j) = val;
}
}
return C;
}
//Set matrix as identity matrix
void make_indentity(bin_matrix A)
{
for(int i = 0; i < A->rows; i++)
{
for(int j = 0; j < A->cols; j++)
{
if(i == j)
{
mat_element(A, i, j) = 1;
}
else
{
mat_element(A, i, j) = 0;
}
}
}
}
bool is_identity(bin_matrix A)
{
bool flag = true;
for(int i = 0; i < A->rows; i++)
{
for(int j = 0; j < A->cols; j++)
{
if(i == j)
{
if(mat_element(A, i, j) == 0)
{
flag = false;
return flag;
}
}
else
{
if(mat_element(A, i, j) == 1)
{
flag = false;
return flag;
}
}
}
}
return flag;
}
//Checks if the matrix is a zero matrix
int is_zero_matrix(bin_matrix A)
{
int flag = 1;
for(int i = 0; i < A->rows; i++)
{
for(int j = 0; j < A->cols; j++)
{
if(mat_element(A, i, j) != 0)
{
flag = 0;
return flag;
}
}
}
return flag;
}
//Checks if two matrices are equal
int mat_is_equal(bin_matrix A, bin_matrix B)
{
int flag = 1;
if(A->rows != B->rows || A->cols != B->cols)
{
flag = 0;
return flag;
}
for(int i = 0; i < A->rows; i++)
{
for(int j = 0; j < A->cols; j++)
{
if(mat_element(A, i, j) != mat_element(B, i, j))
{
flag = 0;
return flag;
}
}
}
return flag;
}
//Add the elements of row1 to row2 in the column index range [a,b]
bin_matrix add_rows_new(bin_matrix A,int row1, int row2, int a, int b)
{
if(row1 < 0 || row1 >= A->rows || row2 < 0 || row2 >= A->cols)
{
printf("Matrix index out of range\n");
exit(0);
}
for(int i = a; i < b; i++)
{
mat_element(A, row2, i) = (mat_element(A, row1, i) ^ mat_element(A, row2, i));
}
return A;
}
//Add col1 and col2 from in the row index range [a,b]
bin_matrix add_cols(bin_matrix A,int col1, int col2, int a, int b)
{
if(col1 < 0 || col1 >= A->cols || col2 < 0 || col2 >= A->cols)
{
printf("Matrix index out of range\n");
exit(0);
}
for(int i = a; i < b; i++)
{
mat_element(A, i, col2) = (mat_element(A, i, col1) ^ mat_element(A, i, col2));
}
return A;
}
//Inverse of matrix
bin_matrix circ_matrix_inverse(bin_matrix A)
{
if(A->rows != A->cols)
{
printf("Inverse not possible...\n");
exit(0);
}
if(is_identity(A))
{
return A;
}
bin_matrix B;
B = mat_init(A->rows, A->cols);
make_indentity(B);
int i;
int flag, prev_flag = 0;
for(i = 0; i < A->cols; i++)
{
if(mat_element(A, i, i) == 1)
{
for(int j = 0; j < A->rows; j++)
{
if(i != j && mat_element(A, j, i) == 1)
{
add_rows_new(B, i, j, 0, A->cols);
add_rows_new(A, i, j, i, A->cols);
}
}
}
else
{
int k;
for(k = i + 1; k < A->rows; k++)
{
if(mat_element(A, k, i) == 1)
{
add_rows(B, k, i);
add_rows(A, k, i);
i = i - 1;
break;
}
}
}
}
//printf("Out of for loop...\n");
if(!is_identity(A))
{
printf("Could not find inverse, exiting...\n");
exit(-1);
}
return B;
}
//Obtain the specified number of rows and columns
bin_matrix mat_splice(bin_matrix A, int row1, int row2, int col1, int col2)
{
int row_count = row2 - row1 + 1;
int col_count = col2 - col1 + 1;
int idx1, idx2;
bin_matrix t = mat_init(row_count, col_count);
for(int i = 0; i < row_count; i++)
{
idx1 = row1 + i;
for(int j = 0; j < col_count; j++)
{
idx2 = col1 + j;
set_matrix_element(t, i, j, mat_element(A, idx1, idx2));
}
}
return t;
}
//Finding the basis of the kernel space of a matrix A
bin_matrix mat_kernel(bin_matrix A)
{
int row_count = A->rows;
int col_count = A->cols;
bin_matrix temp = mat_init(col_count, row_count + col_count);
bin_matrix ans = mat_init(col_count, col_count - row_count);
for(int i = 0; i < temp->rows; i++)
{
for(int j = 0; j < row_count; j++)
{
set_matrix_element(temp, i, j, mat_element(A, j, i));
}
}
for(int i = 0; i < col_count; i++)
{
set_matrix_element(temp, i, i + row_count, 1);
}
int r = 0;
while(r < row_count)
{
if(mat_element(temp, r, r) == 0)
{
int i;
for(i = r + 1; i < temp->rows; i++)
{
if(mat_element(temp, i, r))
{
swap(temp, r, i);
break;
}
}
if(i == temp->rows)
{
ans = mat_splice(temp, row_count, col_count - 1, row_count, row_count + col_count - 1);
return (matrix_rref(ans));
}
}
else
{
for(int i = 0; i < temp->rows; i++)
{
if(mat_element(temp, i, r) && i != r)
{
add_rows(temp, r, i);
}
}
r++;
}
}
ans = mat_splice(temp, row_count, col_count - 1, row_count, row_count + col_count - 1);
return (matrix_rref(ans));
}
//Concatenate the matrices A and B as [A|B]
bin_matrix concat_horizontal(bin_matrix A, bin_matrix B)
{
if(A->rows != B->rows)
{
printf("Incompatible dimensions of the two matrices. Number of rows should be same.\n");
exit(0);
}
bin_matrix temp = mat_init(A->rows, A->cols + B->cols);
for(int i = 0; i < temp->rows; i++)
{
for(int j = 0; j < temp->cols; j++)
{
if(j < A->cols)
{
set_matrix_element(temp, i, j, mat_element(A, i, j));
}
else
{
set_matrix_element(temp, i, j, mat_element(B, i, j - A->cols));
}
}
}
return temp;
}
//Concatenate the matrices A and B vertically
bin_matrix concat_vertical(bin_matrix A, bin_matrix B)
{
if(A->cols != B->cols)
{
printf("Incompatible dimensions of the two matrices. Number of rows should be same.\n");
exit(0);
}
bin_matrix temp = mat_init(A->rows + B->rows, A->cols);
for(int i = 0; i < temp->rows; i++)
{
for(int j = 0; j < temp->cols; j++)
{
if(i < A->rows)
{
set_matrix_element(temp, i, j, mat_element(A, i, j));
}
else
{
set_matrix_element(temp, i, j, mat_element(B, i - A->rows, j));
}
}
}
return temp;
}
//Printing the matrix
void print_matrix(bin_matrix A)
{
for(int i = 0; i < A->rows; i++)
{
for (int j = 0; j < A->cols; j++)
{
printf("%hu ", mat_element(A, i, j));
}
printf("\n");
}
}