-
Notifications
You must be signed in to change notification settings - Fork 23
/
matrix.c
executable file
·463 lines (390 loc) · 12.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
#include "matrix.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*===========================================================================
* assert
* If the assertion is non-zero (i.e. true), then it returns.
* If the assertion is zero (i.e. false), then it display the string and
* aborts the program.
* This is ment to act like Python's assert keyword.
*=========================================================================*/
void assert(int assertion, char* message) {
if (assertion == 0) {
fprintf(stderr, "%s\n", message);
exit(1);
}
}
/*===========================================================================
* readMatrix
* Reads a file containing a Matrix
*=========================================================================*/
matrix* readMatrix(char* filename) {
FILE* fh;
matrix* data;
float myValue;
int width, height, i, elements;
int scan_test;
double *ptr;
if ((fh = fopen(filename, "r")) == NULL) {
fprintf(stderr, "Error: Cannot open %s\n", filename);
exit(1);
}
scan_test = fscanf(fh, "%d", &width);
assert(scan_test != EOF, "Failed to read from file.");
scan_test = fscanf(fh, "%d", &height);
assert(scan_test != EOF, "Failed to read from file.");
data = makeMatrix(width, height);
elements = width * height;
ptr = data->data;
for (i = 0; i < elements; i++) {
scan_test = fscanf(fh, "%f", &myValue);
assert(scan_test != EOF, "Failed to read from file. File is incomplete.");
*(ptr++) = myValue;
}
fclose(fh);
return data;
}
/*===========================================================================
* makeMatrix
* Makes a matrix with a width and height parameters.
*=========================================================================*/
matrix* makeMatrix(int width, int height) {
matrix* out;
assert(width > 0 && height > 0, "New matrix must be at least a 1 by 1");
out = (matrix*) malloc(sizeof(matrix));
assert(out != NULL, "Out of memory.");
out->width = width;
out->height = height;
out->data = (double*) malloc(sizeof(double) * width * height);
assert(out->data != NULL, "Out of memory.");
memset(out->data, 0.0, width * height * sizeof(double));
return out;
}
/*===========================================================================
* copyMatrix
* Copies a matrix. This function uses scaleMatrix, because scaling matrix
* by 1 is the same as a copy.
*=========================================================================*/
matrix* copyMatrix(matrix* m) {
return scaleMatrix(m, 1);
}
/*===========================================================================
* freeMatrix
* Frees the resources of a matrix
*=========================================================================*/
void freeMatrix(matrix* m) {
if (m != NULL) {
if (m->data != NULL) {
free(m->data);
m->data = NULL;
}
free(m);
m = NULL;
}
return;
}
/*===========================================================================
* writeMatrix
* Write a matrix to a file.
*=========================================================================*/
void writeMatrix(matrix* m, char* filename) {
FILE* fh;
int i, j;
double* ptr = m->data;
if ((fh = fopen(filename, "w")) == NULL) {
fprintf(stderr, "Error: Cannot open %s\n", filename);
exit(1);
}
fprintf(fh, "%d %d\n", m->width, m->height);
for (i = 0; i < m->height; i++) {
for (j = 0; j < m->width; j++) {
fprintf(fh, " %2.5f", *(ptr++));
}
fprintf(fh, "\n");
}
close(fh);
return;
}
/*===========================================================================
* printMatrix
* Prints a matrix. Great for debugging.
*=========================================================================*/
void printMatrix(matrix* m) {
int i, j;
double* ptr = m->data;
printf("%d %d\n", m->width, m->height);
for (i = 0; i < m->height; i++) {
for (j = 0; j < m->width; j++) {
printf(" %9.6f", *(ptr++));
}
printf("\n");
}
return;
}
/*===========================================================================
* eyeMatrix
* Returns an identity matrix of size n by n, where n is the input
* parameter.
*=========================================================================*/
matrix* eyeMatrix(int n) {
int i;
matrix *out;
double* ptr;
assert(n > 0, "Identity matrix must have value greater than zero.");
out = makeMatrix(n, n);
ptr = out->data;
for (i = 0; i < n; i++) {
*ptr = 1.0;
ptr += n + 1;
}
return out;
}
/*===========================================================================
* traceMatrix
* Given an "m rows by n columns" matrix, it returns the sum of the elements
* along the diagonal. This is know as the matrix 'trace'.
*=========================================================================*/
double traceMatrix(matrix* m) {
int i;
int size;
double* ptr = m->data;
double sum = 0.0;
if (m->height < m->width) {
size = m->height;
}
else {
size = m->width;
}
for (i = 0; i < size; i++) {
sum += *ptr;
ptr += m->width + 1;
}
return sum;
}
/*===========================================================================
* meanMatrix
* Given an "m rows by n columns" matrix, it returns a matrix with 1 row and
* n columns, where each element represents the mean of that full column.
*=========================================================================*/
matrix* meanMatrix(matrix* m) {
int i, j;
double* ptr;
matrix* out;
assert(m->height > 0, "Height of matrix cannot be zero.");
out = makeMatrix(m->width, 1);
for (i = 0; i < m->width; i++) {
out->data[i] = 0.0;
ptr = &m->data[i];
for (j = 0; j < m->height; j++) {
out->data[i] += *ptr;
ptr += out->width;
}
out->data[i] /= (double) m->height;
}
return out;
}
/*===========================================================================
* covarianceMatrix
* Given an "m rows by n columns" matrix, it returns a matrix with n row and
* n columns, where each element represents covariance of 2 columns.
*=========================================================================*/
matrix* covarianceMatrix(matrix* m) {
int i, j, k, L = 0;
matrix* out;
matrix* mean;
double* ptrA;
double* ptrB;
double* ptrOut;
assert(m->height > 1, "Height of matrix cannot be zero or one.");
mean = meanMatrix(m);
out = makeMatrix(m->width, m->width);
ptrOut = out->data;
for (i = 0; i < m->width; i++) {
for (j = 0; j < m->width; j++) {
ptrA = &m->data[i];
ptrB = &m->data[j];
*ptrOut = 0.0;
for (k = 0; k < m->height; k++) {
*ptrOut += (*ptrA - mean->data[i]) * (*ptrB - mean->data[j]);
ptrA += m->width;
ptrB += m->width;
}
*ptrOut /= m->height - 1;
ptrOut++;
}
}
freeMatrix(mean);
return out;
}
/*===========================================================================
* transposeMatrix
* Given an matrix, returns the transpose.
*=========================================================================*/
matrix* transposeMatrix(matrix* m) {
matrix* out = makeMatrix(m->height, m->width);
double* ptrOut;
double* ptrM = m->data;
int i, j;
for (i = 0; i < m->height; i++) {
ptrOut = &out->data[i];
for (j = 0; j < m->width; j++) {
*ptrOut = *ptrM;
ptrM++;
ptrOut += out->width;
}
}
return out;
}
/*===========================================================================
* multiplyMatrix
* Given a two matrices, returns the multiplication of the two.
*=========================================================================*/
matrix* multiplyMatrix(matrix* a, matrix* b) {
int i, j, k;
matrix* out;
double* ptrOut;
double* ptrA;
double* ptrB;
assert(a->width == b->height, "Matrices have incorrect dimensions. a->width != b->height");
out = makeMatrix(b->width, a->height);
ptrOut = out->data;
for (i = 0; i < a->height; i++) {
for (j = 0; j < b->width; j++) {
ptrA = &a->data[ i * a->width ];
ptrB = &b->data[ j ];
*ptrOut = 0;
for (k = 0; k < a->width; k++) {
*ptrOut += *ptrA * *ptrB;
ptrA++;
ptrB += b->width;
}
ptrOut++;
}
}
return out;
}
/*===========================================================================
* scaleMatrix
* Given a matrix and a double value, this returns a new matrix where each
* element in the input matrix is multiplied by the double value
*=========================================================================*/
matrix* scaleMatrix(matrix* m, double value) {
int i, elements = m->width * m->height;
matrix* out = makeMatrix(m->width, m->height);
double* ptrM = m->data;
double* ptrOut = out->data;
for (i = 0; i < elements; i++) {
*(ptrOut++) = *(ptrM++) * value;
}
return out;
}
/*===========================================================================
* rowSwap
* Given a matrix, this algorithm will swap rows p and q, provided
* that p and q are less than or equal to the height of matrix A and p
* and q are different values.
*=========================================================================*/
void rowSwap(matrix* a, int p, int q) {
int i;
double temp;
double* pRow;
double* qRow;
assert(a->height > 2, "Matrix must have at least two rows to swap.");
assert(p < a->height && q < a->height, "Values p and q must be less than the height of the matrix.");
// If p and q are equal, do nothing.
if (p == q) {
return;
}
pRow = a->data + (p * a->width);
qRow = a->data + (q * a->width);
// Swap!
for (i = 0; i < a->width; i++) {
temp = *pRow;
*pRow = *qRow;
*qRow = temp;
pRow++;
qRow++;
}
return;
}
/*===========================================================================
* dotProductMatrix
* Given a two matrices (or the same matrix twice) with identical widths and
* different heights, this method returns a a->height by b->height matrix of
* the cross product of each matrix.
*
* Dot product is essentially the sum-of-squares of two vectors.
*
* Also, if the second paramter is NULL, it is assumed that we
* are performing a cross product with itself.
*=========================================================================*/
matrix* dotProductMatrix(matrix* a, matrix* b) {
matrix* out;
double* ptrOut;
double* ptrA;
double* ptrB;
int i, j, k;
if (b != NULL) {
assert(a->width == b->width, "Matrices must be of the same dimensionality.");
}
// Are we computing the sum of squares of the same matrix?
if (a == b || b == NULL) {
b = a; // May not appear safe, but we can do this without risk of losing b.
}
out = makeMatrix(b->height, a->height);
ptrOut = out->data;
for (i = 0; i < a->height; i++) {
ptrB = b->data;
for (j = 0; j < b->height; j++) {
ptrA = &a->data[ i * a->width ];
*ptrOut = 0;
for (k = 0; k < a->width; k++) {
*ptrOut += *ptrA * *ptrB;
ptrA++;
ptrB++;
}
ptrOut++;
}
}
return out;
}
/*===========================================================================
* matrixDotDiagonal
* Given a two matrices (or the same matrix twice) with identical widths and
* heights, this method returns a 1 by a->height matrix of the cross
* product of each matrix along the diagonal.
*
* Dot product is essentially the sum-of-squares of two vectors.
*
* If the second paramter is NULL, it is assumed that we
* are performing a cross product with itself.
*=========================================================================*/
matrix* dotDiagonalMatrix(matrix* a, matrix* b) {
matrix* out;
double* ptrOut;
double* ptrA;
double* ptrB;
int i, j;
if (b != NULL) {
assert(a->width == b->width && a->height == b->height, "Matrices must be of the same dimensionality.");
}
// Are we computing the sum of squares of the same matrix?
if (a == b || b == NULL) {
b = a; // May not appear safe, but we can do this without risk of losing b.
}
out = makeMatrix(1, a->height);
ptrOut = out->data;
ptrA = a->data;
ptrB = b->data;
for (i = 0; i < a->height; i++) {
*ptrOut = 0;
for (j = 0; j < a->width; j++) {
*ptrOut += *ptrA * *ptrB;
ptrA++;
ptrB++;
}
ptrOut++;
}
return out;
}