-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathyasML.h
566 lines (527 loc) · 13.1 KB
/
yasML.h
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
562
563
564
565
566
#ifndef yasML_h
#define yasML_h
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#define SUCC 1
#define FAIL -1
/*
* a matrix is
columns
pointer . . . .
rows |
|
V
.
.
.
the matrix is an array of array pointers where each array pointer corresponds to a vector
*/
#ifdef __cplusplus
extern "C" {
#endif
/* current representation of a matrix in my mind */
typedef struct Matrix{
int rows;
int columns;
double **numbers;
} Matrix;
Matrix *identity(int length);
Matrix *inversion(Matrix *m);
Matrix *constructor(int r, int c);
int destroy_matrix(Matrix *m);
int print(Matrix *m);
int row_swap(Matrix *m, int a, int b);
int scalar_multiply(Matrix *m, float f);
int reduce(Matrix *m, int a, int b, float factor);
int equals(Matrix *m1, Matrix *m2);
/* we shouldn`t use clone keyword because it`s extensively used in c++ */
Matrix *clonemx(Matrix *m);
Matrix *transpose(Matrix *m);
Matrix *multiply(Matrix *m1, Matrix *m2);
int add(Matrix *m1, Matrix *m2);
int subtract(Matrix *, Matrix *);
Matrix *gram_schmidt(Matrix *);
double *projection(Matrix *, double *, int length);
int zero_vector(Matrix *);
Matrix *orthonormal_basis(Matrix *);
double determinant(Matrix *m);
Matrix *solved_aug_matrix(Matrix *);
void manual_entry(Matrix **m);
double *eigenvalues(Matrix *m);
static int row_scalar_multiply(Matrix *m, int row, float factor);
static double vector_multiply(double *col, double *row, int length);
static void vector_addition(double *v1, double *v2, int length);
static void scalar_vector_multiplication(double factor, double *vector, int length);
static void vector_subtraction(double *v1, double *v2, int length);
/* return success if there is at least one zero vector in the matrix */
int zero_vector(Matrix *m){
unsigned int i, j, counter;
if(m == NULL)
return FAIL;
for(i = 0; i < m->columns; i++){
counter = 0;
for(j = 0; j < m->rows; j++){
if(m->numbers[i][j] == 0)
counter++;
}
if(counter == 3)
return SUCC;
}
return FAIL;
}
/* make a zero matrix of given dimensions */
Matrix *constructor(int r, int c){
unsigned int i;
Matrix *m;
if(r <= 0 || c <= 0){
perror("Give me positive values for dimensions genius");
return NULL;
}
m = malloc(sizeof(Matrix));
m->rows = r;
m->columns = c;
m->numbers = malloc(sizeof(double *)*c);
for(i = 0; i < c; i++)
m->numbers[i] = calloc(sizeof(double), r);
return m;
}
/* enter 1s along the main diagonal */
Matrix *identity(int length){
unsigned int i, j;
Matrix *m;
m = constructor(length, length);
for(i = 0; i < length; i++){
j = i;
(m->numbers[i])[j] = 1;
}
return m;
}
/* free memory associated with the matrix */
int destroy_matrix(Matrix *m){
unsigned int i;
if(m == NULL)
return FAIL;
for(i = 0; i < m->columns; i++)
free(m->numbers[i]);
free(m->numbers);
free(m);
return SUCC;
}
/* print the matrix */
int print(Matrix *m){
unsigned int i, j;
if(m == NULL)
return FAIL;
for(i = 0; i < m->rows; i++){
for(j = 0; j < m->columns; j++){
printf("%f ", m->numbers[j][i]);
}
printf("\n");
}
return SUCC;
}
int row_swap(Matrix *m, int a, int b){
double temp;
unsigned int i;
if(m == NULL)
return FAIL;
if(m->rows <= a || m->rows <= b)
return FAIL;
for(i = 0; i < m->columns; i++){
temp = m->numbers[i][a];
m->numbers[i][a] = m->numbers[i][b];
m->numbers[i][b] = temp;
}
return SUCC;
}
int scalar_multiply(Matrix *m, float scalar){
int i, j;
if(m == NULL)
return FAIL;
for(i = 0; i < m->columns; i++){
for(j = 0; j < m->rows; j++)
(m->numbers[i])[j] *= scalar;
}
return SUCC;
}
/* reduce row b by factor*a */
int reduce(Matrix *m, int a, int b, float factor){
int i;
if(m == NULL)
return FAIL;
if(m->rows < a || m->rows < b)
return FAIL;
for(i = 0; i < m->columns; i++){
m->numbers[i][b] -= m->numbers[i][a]*factor;
}
return SUCC;
}
/* matrix m will become the identity so the caller must save their matrix themselves */
Matrix *inversion(Matrix *m){
Matrix *invert;
unsigned int i, j, l;
double factor;
if(m == NULL)
return NULL;
if((m)->columns != (m)->rows)
return NULL;
invert = identity((m)->rows);
/* reduce each of the rows to get a lower triangle */
for(i = 0; i < (m)->columns; i++){
for(j = i + 1; j < (m)->rows; j++){
if((m)->numbers[i][i] == 0){
for(l=i+1; l < m->rows; l++){
if(m->numbers[l][l] != 0){
row_swap(m, i, l);
break;
}
}
continue;
}
factor = (m)->numbers[i][j]/((m)->numbers[i][i]);
reduce(invert, i, j, factor);
reduce((m), i, j, factor);
}
}
/* now finish the upper triangle */
for(i = (m)->columns - 1; i > 0; i--){
for(j = i-1; j>=0; j--){
if((m)->numbers[i][i] == 0)
continue;
if(j == -1)
break;
factor = (m)->numbers[i][j]/((m)->numbers[i][i]);
reduce(invert, i, j, factor);
reduce((m), i, j, factor);
}
}
/* scale everything to 1 */
for(i = 0; i < (m)->columns; i++){
if((m)->numbers[i][i]==0)
continue;
factor = 1/((m)->numbers[i][i]);
row_scalar_multiply(invert, i, factor);
row_scalar_multiply((m), i, factor);
}
return invert;
}
static int row_scalar_multiply(Matrix *m, int row, float factor){
int i;
if(m == NULL)
return FAIL;
if(m->rows <= row)
return FAIL;
for(i = 0; i < m->columns; i++)
m->numbers[i][row] *= factor;
return SUCC;
}
int equals(Matrix *m1, Matrix *m2){
unsigned int i, j;
if(m1 == NULL || m2 == NULL)
return FAIL;
if(m1->columns != m2->columns || m1->rows != m2->rows)
return FAIL;
for(i = 0; i < m1->columns; i++){
for(j = 0; j < m1->rows; j++){
if(m1->numbers[i][j] != m2->numbers[i][j])
return FAIL;
}
}
return SUCC;
}
Matrix *clonemx(Matrix *m){
Matrix *copy;
unsigned int i, j;
copy = constructor(m->rows, m->columns);
for(i = 0; i < m->columns; i++)
for(j = 0; j < m->rows; j++)
copy->numbers[i][j] = m->numbers[i][j];
return copy;
}
Matrix *transpose(Matrix *m){
Matrix *trans;
unsigned int i, j;
if(m == NULL)
return NULL;
trans = constructor(m->columns, m->rows);
for(i = 0; i < trans->columns; i++){
for(j = 0; j < trans->rows; j++)
trans->numbers[i][j] = m->numbers[j][i];
}
return trans;
}
/* m1 x m2 */
Matrix *multiply(Matrix *m1, Matrix *m2){
Matrix *product, *trans;
unsigned int i, j;
if(m1 == NULL || m2 == NULL)
return NULL;
if(m1->columns != m2->rows)
return NULL;
trans = transpose(m1);
product = constructor(m1->rows, m2->columns);
for(i = 0; i < product->columns; i++){
for(j = 0; j < product->rows; j++){
product->numbers[i][j] = vector_multiply(trans->numbers[j], m2->numbers[i], m2->rows);
}
}
destroy_matrix(trans);
return product;
}
/* v1 x v2 -- simply a helper function -- computes dot product between two vectors*/
static double vector_multiply(double *col, double *row, int length){
double sum;
unsigned int i;
sum = 0;
for(i = 0; i < length; i++){
sum += col[i] * row[i];
}
return sum;
}
/* m1 += m2 */
int add(Matrix *m1, Matrix *m2){
unsigned int i, j;
if(m1 == NULL || m2 == NULL)
return FAIL;
if(m1->rows != m2->rows || m1->columns != m2->columns)
return FAIL;
for(i = 0; i < m1->columns; i++){
for(j = 0; j < m1->rows; j++)
m1->numbers[i][j] += m2->numbers[i][j];
}
return SUCC;
}
int subtract(Matrix *m1, Matrix *m2){
unsigned int i, j;
if(m1 == NULL || m2 == NULL)
return FAIL;
if(m1->rows != m2->rows || m1->columns != m2->columns)
return FAIL;
for(i = 0; i < m1->columns; i++){
for(j = 0; j < m1->rows; j++)
m1->numbers[i][j] -= m2->numbers[i][j];
}
return SUCC;
}
/* change m into an orthogonal matrix */
Matrix *gram_schmidt(Matrix *m){
Matrix *ortho;
double *ortho_vector, *temp;
unsigned int i, j;
if(m != NULL || m->rows == m->columns || zero_vector(m) != 1){
/* create my empy matrix to have new orthogonal vector be added to */
ortho = constructor(m->rows, 1);
/* initialize with the first vector */
free(ortho->numbers[0]);
ortho_vector = malloc(sizeof(double)*m->rows);
for(i = 0; i < m->rows; i++)
ortho_vector[i] = m->numbers[0][i];
ortho->numbers[0] = ortho_vector;
/* now loop and go through the gs system */
for(i = 1; i < m->columns; i++){
/* first initialize to the regular vector */
ortho_vector = malloc(sizeof(double)*m->rows);
for(j = 0; j < m->rows; j++)
ortho_vector[j] = m->numbers[i][j];
/* get the subtracting factor */
temp = projection(ortho, ortho_vector, m->rows);
/* expand the matrix */
ortho->columns++;
ortho->numbers = realloc(ortho->numbers, sizeof(double *)*ortho->columns);
ortho->numbers[ortho->columns - 1] = ortho_vector;
vector_subtraction(ortho_vector, temp, m->rows);
}
return ortho;
}
return NULL;
}
double *projection(Matrix *m, double *v, int length){
unsigned int i, j;
double *sum, *copy, *vector, factor;
if(m->rows != length)
return NULL;
if(m == NULL || v == NULL)
return NULL;
sum = calloc(sizeof(double), m->rows);
copy = malloc(sizeof(double)*m->rows);
for(i = 0; i < m->columns; i++){
for(j = 0; j < m->rows; j++)
copy[j] = m->numbers[i][j];
vector = copy;
factor = vector_multiply(v, vector, m->rows)/vector_multiply(vector, vector, m->rows);
scalar_vector_multiplication(factor, vector, m->rows);
vector_addition(sum, vector, m->rows);
}
free(copy);
return sum;
}
/* v1 *= v2 */
static void scalar_vector_multiplication(double factor, double *vector, int length){
unsigned int i;
for(i = 0; i < length; i++)
vector[i] *= factor;
}
/* v1 += v2 */
static void vector_addition(double *v1, double *v2, int length){
unsigned int i;
for(i = 0; i < length; i++){
v1[i] += v2[i];
}
}
static void vector_subtraction(double *v1, double *v2, int length){
unsigned int i;
for(i = 0; i < length; i++){
v1[i] -= v2[i];
}
}
double determinant(Matrix *m){
Matrix *copy;
unsigned int i, j;
double det, factor;
if(m == NULL)
return -1;
if(m->columns != m->rows)
return -1;
copy = clonemx(m);
det = 1;
/* reduce each of the rows to get a lower triangle */
for(i = 0; i < copy->columns; i++){
for(j = i + 1; j < copy->rows; j++){
if(copy->numbers[i][i] == 0)
continue;
factor = copy->numbers[i][j]/(copy->numbers[i][i]);
reduce(copy, i, j, factor);
}
}
for(i = 0; i < copy->columns; i++)
det *= copy->numbers[i][i];
destroy_matrix(copy);
return det;
}
Matrix *orthonormal_basis(Matrix *m){
Matrix *orthog;
unsigned int i, j;
double factor;
if(m == NULL)
return NULL;
orthog = gram_schmidt(m);
for(i = 0; i < m->columns; i++){
factor = 0;
for(j = 0; j < m->rows; j++)
factor += orthog->numbers[i][j]*orthog->numbers[i][j];
factor = sqrt(factor);
for(j = 0; j < m->rows; j++)
orthog->numbers[i][j] /= factor;
}
return orthog;
}
Matrix *solved_aug_matrix(Matrix *m){
Matrix *low;
double factor, absolute;
unsigned int i, j, k, l;
if(m == NULL)
return NULL;
low = clonemx(m);
absolute = abs(m->rows - m->columns);
for(k = 0; k < absolute; k++){
/* reduce each of the rows to get a lower triangle */
for(i = 0; i < low->columns && i<low->rows; i++){
for(j = i + 1; j < low->rows; j++){
if(low->numbers[i][i] == 0){
for(l = i+1; l < low->rows; l++){
if(m->numbers[l][l]!=0){
row_swap(low, i, l);
break;
}
}
continue;
}
factor = low->numbers[i][j]/(low->numbers[i][i]);
reduce(low, i, j, factor);
}
}
/* now finish the upper triangle */
for(i = (low->rows>low->columns)?low->columns-1:low->rows-1; i > 0; i--){
for(j = i-1; j>=0; j--){
if(low->numbers[i][i] == 0)
continue;
if(j == -1)
break;
factor = low->numbers[i][j]/(low->numbers[i][i]);
reduce(low, i, j, factor);
}
}
/* scale everything to 1 */
for(i = 0; i < low->columns; i++){
if(low->numbers[i][i]==0)
continue;
factor = 1/(low->numbers[i][i]);
row_scalar_multiply(low, i, factor);
}
}
return low;
}
/* Return an array of all of the possible eigenvalues */
double *eigenvalues(Matrix *m){
double *values, factor;
Matrix *red;
unsigned int i, j, l;
if(m == NULL)
return NULL;
if(m->rows != m->columns)
return NULL;
values = malloc(sizeof(double)*m->rows);
red = clonemx(m);
/* reduce each of the rows to get a lower triangle */
for(i = 0; i < red->columns; i++){
for(j = i + 1; j < red->rows; j++){
if(red->numbers[i][i] == 0){
for(l = i+1; l < red->rows; l++){
if(red->numbers[l][l] != 0){
row_swap(red, i, l);
break;
}
}
continue;
}
factor = red->numbers[i][j]/(red->numbers[i][i]);
reduce(red, i, j, factor);
}
}
for(i = 0; i < red->columns; i++)
values[i] = red->numbers[i][i];
return values;
}
/* make your own matrix */
void manual_entry(Matrix **m){
Matrix *temp;
int i, j, rows, cols;
double number;
char buffer[12];
printf("Rows | Columns\n");
/* should only execute once but I need to do error detection */
rows = -1; cols = -1;
while(fgets(buffer, 12, stdin) != NULL){
sscanf(buffer, "%d | %d", &rows, &cols);
break;
}
if(rows == -1 || cols == -1)
perror("bad input");
temp = constructor(rows, cols);
if(temp == NULL)
return;
i = 0; j = 0;
printf("start entering numbers from left to right, top to bottom\nand use either EOF to end input\n");
while(fgets(buffer, 6, stdin) != NULL){
number = atof(buffer);
temp->numbers[i%cols][(int) floor(j/rows)] = number;
i++;
j++;
}
*m = temp;
}
#ifdef __cplusplus
}
#endif
#endif // yasML