/
matrix.c
561 lines (500 loc) · 14.9 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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
#include "matrix.h"
#include "pch.h"
// Utility Func
// Swap data of two variables
void swap(dtype *a, dtype *b) {
*a = *a + *b;
*b = *a - *b;
*a = *a - *b;
}
// Matrix Management Function
// Initialise Data inside Matrix
void constructmatrix(matrix *m, long row1, long col1) {
m->row = row1;
m->col = col1;
m->arr = calloc(m->row * m->col, sizeof(dtype));
}
void FREE(matrix *m) {
if (m != NULL) {
free(m->arr);
free(m);
m = NULL;
}
}
// Initialise Matrix in memory
matrix *init(long row, long col) {
if (row > 0 && col > 0) {
matrix *m = calloc(1, sizeof(matrix));
constructmatrix(m, row, col);
return m;
}
if (debug == 1) {
printf("\nWarning:Tried to Init Matrix zero rows or zero cols\n\n");
printf("\nYoure seeing this warning because debug is defined to be "
"1\n\n");
}
return NULL;
}
matrix *initones(long row, long col) {
if (row > 0 && col > 0) {
matrix *m = calloc(1, sizeof(matrix));
constructmatrix(m, row, col);
for (long i = 0; i < m->row; i++) {
for (long j = 0; j < m->col; j++) {
elem(m, i, j) = 1;
}
}
return m;
}
if (debug == 1) {
printf("\nWarning:Tried to Init Matrix zero rows or zero cols\n\n");
printf("\nYoure seeing this warning because debug is defined to be "
"1\n\n");
}
return NULL;
}
// Function to return a copy of matrix
matrix *copy(matrix *m) {
matrix *ret = init(m->row, m->col);
for (long i = 0; i < m->row; i++) {
for (long j = 0; j < m->col; j++) {
elem(ret, i, j) = elem(m, i, j);
}
}
return ret;
}
// create Identity matrix from zero matrix
void eye(matrix *m) {
long dim = (m->row <= m->col) ? m->row : m->col;
for (long i = 0; i < dim; i++) {
elem(m, i, i) = 1;
}
}
// Intialise and return an identity matrix of size NxM
matrix *eyeinit(long row, long col) {
matrix *ret = init(row, col);
eye(ret);
return ret;
}
matrix *concatmat(matrix *one, matrix *two) {
matrix *new = init(one->row, one->col + two->col);
for (long i = 0; i < one->row; i++) {
for (long j = 0; j < one->col; j++) {
elem(new, i, j) = elem(one, i, j);
}
}
for (long i = 0; i < two->row; i++) {
for (long j = 0; j < two->col; j++) {
elem(new, i, j + one->col) = elem(two, i, j);
}
}
return new;
}
matrix *transpose(matrix *m) {
matrix *new = init(m->col, m->row);
for (long i = 0; i < m->row; i++) {
for (long j = 0; j < m->col; j++) {
elem(new, j, i) = elem(m, i, j);
}
}
return new;
}
matrix *multiplymat(matrix *m, matrix *n) {
if (m->col != n->row) {
printf("M col %ld N row %ld\n", m->col, n->row);
printf("Cannot Multipy matrices \n!");
return NULL;
}
matrix *new = init(m->row, n->col);
for (long i = 0; i < m->row; ++i) {
for (long j = 0; j < n->col; ++j) {
for (long k = 0; k < m->col; ++k) {
// result[i][j]+=a[i][k]*b[k][j];
elem(new, i, j) += elem(m, i, k) * elem(n, k, j);
}
}
}
return new;
}
// Row Swap Operation
void rowswap(matrix *m, long r1, long r2) {
for (long i = 0; i < m->col; i++) {
swap(&elem(m, r1, i), &elem(m, r2, i));
}
}
// Input Function For Matrix
matrix *input_matrix(matrix *m) {
/**
* takes String input of rows
* tokenises it converts string to dtype(double as
* of now) saves it in Matrix->arr(row Major type)
*/
char buf[BUFSIZ] = {0};
char *token = NULL;
const long NUMBASE = 10;
long row;
long col;
printf("Enter row and column of matrix seperated by space: ");
fgets(buf, BUFSIZ, stdin);
token = buf;
row = strtol(strtok(token, " "), NULL, NUMBASE);
col = strtol(strtok(NULL, " "), NULL, NUMBASE);
m = init(row, col);
// constructmatrix(m,m->row,m->col);
printf("constructed matrix of %ld %ld\n", m->row, m->col);
printf("Enter values in matrix in the form \na00 a01 a02 .. a0n\n... ... "
"... ... ...\nan0 an1 an2 .. ann\n");
for (long i = 0; i < m->row; i++) {
fgets(buf, BUFSIZ, stdin);
token = buf;
elem(m, i, 0) = (double)strtol(strtok(token, " "), NULL, NUMBASE);
// elem(m,i,0) = 1;
for (long j = 1; j < m->col; j++) {
elem(m, i, j) = (double)strtol(strtok(NULL, " "), NULL, NUMBASE);
// elem(m,i,j)=1;
}
}
return m;
}
matrix *input_vector(matrix *m) {
/**
* takes String input of rows
* tokenises it converts string to dtype(double as
* of now) saves it in Matrix->arr(row Major type)
*/
char buf[BUFSIZ] = {0};
char *token = NULL;
const long NUMBASE = 10;
long row;
long col = 1;
printf("Enter elements of vector\n");
fgets(buf, BUFSIZ, stdin);
token = buf;
row = strtol(strtok(token, " "), NULL, NUMBASE);
m = init(row, col);
// constructmatrix(m,m->row,m->col);
printf("constructed matrix of %ld %ld\n", m->row, m->col);
printf("Enter values in vector in the form \na0 \na1\na2 \n.. \nan\n");
for (long i = 0; i < m->row; i++) {
fgets(buf, BUFSIZ, stdin);
token = buf;
elem(m, i, 0) = (double)strtol(strtok(token, " "), NULL, NUMBASE);
// elem(m,i,0) = 1;
for (long j = 1; j < m->col; j++) {
elem(m, i, j) = (double)strtol(strtok(NULL, " "), NULL, NUMBASE);
// elem(m,i,j)=1;
}
}
return m;
}
// Function to Print Data of Matrix
void printmat(matrix *m) {
for (long i = 0; i < m->row; i++) {
for (long j = 0; j < m->col; j++) {
printf("%8.2lf ", elem(m, i, j));
}
printf("\n");
}
}
void printmataccurate(matrix *m) {
for (long i = 0; i < m->row; i++) {
for (long j = 0; j < m->col; j++) {
printf("%18.8lf ", elem(m, i, j));
}
printf("\n");
}
}
// Matrix Function to drop Cols
matrix *dropcol(matrix *m, long colindex) {
long k = 0;
matrix *newm = init(m->row, m->col - 1);
if (newm != NULL) {
for (long i = 0; i < m->row; i++) {
k = 0;
for (long j = 0; j < m->col; j++) {
if (j != colindex) {
elem(newm, i, k) = elem(m, i, j);
k++;
}
}
}
}
FREE(m);
return newm;
}
// Subtract Rows r2=r2-r1 with pivot on diagonal (for U)
void subrow(matrix *m, long r1, long r2) {
double mul = elem(m, r2, r1) / elem(m, r1, r1);
double arr[m->col];
for (long i = 0; i < m->col; i++) {
arr[i] = elem(m, r1, i) * mul;
}
for (long i = 0; i < m->col; i++) {
elem(m, r2, i) -= arr[i];
}
// return m;
}
// Matrix function to drop rows
matrix *droprow(matrix *m, long row) {
matrix *newm = init(m->row - 1, m->col);
if (newm != NULL) {
long k = 0;
long flag = 0;
for (long i = 0; i < m->row; i++) {
for (long j = 0; j < m->col; j++) {
if (i != row) {
elem(newm, k, j) = elem(m, i, j);
flag = 1;
}
}
if (flag == 1) {
k++;
flag = 0;
}
}
}
FREE(m);
return newm;
}
// Subtract Rows r2=r2-r1 with pivot not on diagonal (for R)
void subrowR(matrix *m, long r1, long r2, long piv) {
double mul = elem(m, r2, piv) / elem(m, r1, piv);
double arr[m->col];
for (long i = 0; i < m->col; i++) {
arr[i] = elem(m, r1, i) * mul;
}
for (long i = 0; i < m->col; i++) {
elem(m, r2, i) -= arr[i];
}
// return m;
}
// Scalar Multiplication on Rows Of Matrix
void rowmulconst(matrix *m, long row, double scalar) {
for (long i = 0; i < m->col; i++) {
elem(m, row, i) *= scalar;
}
}
// Check If a Row is zero return 1 if row is not 0
long iszerorow(matrix *m, long row, long aug) {
long flag = 0;
for (long i = 0; i < m->col - aug; i++) {
if (elem(m, row, i) != 0) {
flag = 1;
}
}
return flag;
}
// Delete duplicate pivots in pivotindex array and reduce the value of num_pivot
void normalizepivotdata(pivotdata *p) {
for (long i = 0; i < p->num_pivot; i++) {
for (long j = i + 1; j < p->num_pivot; j++) {
/* If any duplicate found */
if (p->pivotindex[i] == p->pivotindex[j]) {
/* Delete the current duplicate element */
for (long k = j; k < p->num_pivot; k++) {
p->pivotindex[k] = p->pivotindex[k + 1];
}
/* Decrement size after removing duplicate element */
p->num_pivot--;
/* If shifting of elements occur then don't increment j */
j--;
}
}
}
}
// divide all rows by 1/pivot[row]
void scalerref(matrix *m, long aug) {
for (long i = 0; i < m->row; i++) {
for (long j = 0; j < m->col - aug; j++) {
if (elem(m, i, j) != 0) {
rowmulconst(m, i, 1 / elem(m, i, j));
break;
}
}
}
}
// Check for Zero Column returns 1 if col is not zero
long iszerocol(matrix *m, long i, long from) {
// long zero = 0;
for (int j = from; j < m->row; j++) {
if (elem(m, j, i) != 0) {
return 1;
}
}
return 0;
}
// Function to Compute RREF of a Matrix and return number of pivots and position
// of pivots
pivotdata *rref(matrix *m, long aug) {
// initialise Pivotdata pointer in the memory
pivotdata *ret;
ret = calloc(1, sizeof(pivotdata));
long dim = m->col;
ret->pivotindex = calloc(dim * 2, sizeof(long));
// Logic to compute U.
long ndim = (m->row > m->col - aug) ? m->col - aug : m->row;
long pivstart = 0;
for (long i = 0; i < ndim; i++) {
for (long z = i; z < m->col - aug; z++) {
if (iszerocol(m, z, i) == 1) {
pivstart = z;
// printf("\nFirst Pivot %d \n",z);
break;
}
pivstart = i;
}
if (elem(m, i, pivstart) == 0) {
long j = 0;
long flag = 0;
for (j = i + 1; j < m->row; j++) {
if (elem(m, j, pivstart) != 0) {
break;
flag = 1;
}
}
if (j == m->row && flag == 0) {
break;
}
rowswap(m, j, i);
// debug comments
// printf("\n printed u debug %d %d \n",j,i);
// printmat(m);
// printf("\n\n ");
}
// Save Pivots
ret->pivotindex[ret->num_pivot] = pivstart;
ret->num_pivot++;
// printf("\nnum_pivot %d pivindes %d\n ",ret->num_pivot,i);
for (long k = i + 1; k < m->row; k++) {
// printf("\n printed u debug %d %d %d %lf\n
// ",i,k,m->row,elem(m,k,i));
if (elem(m, k, pivstart) != 0) {
subrowR(m, i, k, pivstart);
// printf("\n printed u debug %d %d %d \n",i,k,pivstart);
// ",i,k,m->row,elem(m,k,i)); printmat(m); printf("\n\n");
}
}
}
// printf("\n printed u debug\n");
// printmat(m);
// printf("\n\n");
// printmat(m);
// start calculation for R
// find first non zero row from botton and it first non zero element from
// left
long rstart = 0;
for (long i = m->row - 1; i > 0; i--) {
// printf("\n %d \n",iszerorow(m,i,aug));
if (iszerorow(m, i, aug) == 1) {
rstart = i;
break;
}
}
long piv = 0;
for (long i = 0; i < m->col - aug; i++) {
if (elem(m, rstart, i) != 0) {
// printf("\nrstart %d piv %d\n ",rstart,i);
piv = i;
ret->pivotindex[ret->num_pivot] = piv;
ret->num_pivot++;
break;
}
}
// printf("\n %d %d\n", rstart, piv);
long i = 0;
long j = 0;
long k = 0;
// keep finding non zero pivots in non zero rows above
for (i = rstart; i > 0; i--) {
if (iszerorow(m, i, aug) != 1) {
// if (i < 1)
// break;
continue;
// printf("\nsubtracted i\n");
}
for (k = 0; k < m->col - aug; k++) {
if (elem(m, i, k) != 0) {
piv = k;
// printf("\nrref calculation debug \n%d\n",piv);
// printf("\nrstart %d piv %d\n ",i,piv);
ret->pivotindex[ret->num_pivot] = piv;
ret->num_pivot++;
break;
}
}
for (j = i - 1; j >= 0; j--) {
// printf("\ninside rref %d %d elem\n",i,piv);
if (elem(m, j, piv) != 0) {
subrowR(m, i, j, piv);
}
// printf("\ninside rref %d %d elem\n",i,piv);
// printmat(m);
// printf("\n\n");
}
}
normalizepivotdata(ret);
scalerref(m, aug);
return ret;
}
// Matrix function to create an augmented matrix
matrix *augmented_matrix(matrix *m, const dtype *a) {
matrix *new_m = init(m->row, m->col + 1);
if (new_m != NULL) {
for (long i = 0; i < new_m->row; i++) {
for (long j = 0; j < new_m->col; j++) {
elem(new_m, i, j) = elem(m, i, j);
}
}
for (long i = 0; i < new_m->row; i++) {
elem(new_m, i, new_m->col - 1) = a[i];
}
}
// free(m->arr);
// free(m);
FREE(m);
return new_m;
}
// Matrix utilty function to take input into augmented matrix
matrix *input_augmentcolumn(matrix *m) {
matrix *ret;
dtype *col = calloc(m->row, sizeof(dtype));
printf("enter b in column form like\nb0\nb1\n.\nbn\n");
for (long i = 0; i < m->row; i++) {
scanf("%lf", &col[i]);
}
ret = augmented_matrix(m, col);
free(col);
return ret;
}
// Matrix Function to copy rows from one matrix into other matrix
void mat_rowcopy(matrix *m, long rm, matrix *n, long rn) {
if (m->col != n->col) {
return;
}
for (long i = 0; i < m->col; i++) {
elem(m, rm, i) = elem(n, rn, i);
}
}
matrix *inverse(matrix *m) {
matrix *e = eyeinit(m->row, m->col);
matrix *cat = concatmat(m, e);
FREE(e);
rref(cat, 0);
matrix *inv = init(m->row, m->col);
for (long i = 0; i < m->row; i++) {
for (long j = 0; j < m->col; j++) {
elem(inv, i, j) = elem(cat, i, j + m->col);
}
}
FREE(cat);
return inv;
}
matrix *inplacepowermat(matrix *m, long matpow) {
matrix *new = init(m->row, m->col);
for (long i = 0; i < m->row; i++) {
for (long j = 0; j < m->col; j++) {
elem(new, i, j) = pow(elem(m, i, j), matpow);
}
}
return new;
}