-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrixd.c
143 lines (115 loc) · 2.52 KB
/
matrixd.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
#include "matrixd.h"
md * create_md(int rows, int cols) {
md *ret;
ret = malloc(sizeof(*ret));
ret->rows = rows;
ret->cols = cols;
ret->m = malloc(sizeof(**ret->m) * rows * cols);
// check for failure
if(ret->m == NULL) {
free(ret);
ret = NULL;
}
return ret;
}
md * resize_md(int rows, int cols, md *ret) {
ret->rows = rows;
ret->cols = cols;
ret->m = realloc(ret->m, sizeof(*ret->m) * rows * cols);
// check for failure
if(ret->m == NULL) {
free(ret);
ret = NULL;
}
return ret;
}
int add_md(md *a, md *b, md *ret) {
if(!MD_DIMS_SAME(a, b)) {
return 0;
}
if(!MD_DIMS_SAME(a, ret)) {
ret = resize_md(a->rows, a->cols, ret);
if(ret == NULL) { return 0; }
}
for(int i = 0; i < a->rows; i++) {
for(int j = 0; j < a->cols; j++) {
ret->m[i][j] = a->m[i][j] + b->m[i][j];
}
}
return 1;
}
int sub_md(md *a, md *b, md *ret) {
if(!MD_DIMS_SAME(a, b)) {
return 0;
}
if(!MD_DIMS_SAME(a, ret)) {
ret = resize_md(a->rows, a->cols, ret);
if(ret == NULL) { return 0; }
}
for(int i = 0; i < a->rows; i++) {
for(int j = 0; j < a->cols; j++) {
ret->m[i][j] = a->m[i][j] - b->m[i][j];
}
}
return 1;
}
int mul_mdsc(md *a, double b, md *ret) {
if(!MD_DIMS_SAME(a, ret)) {
ret = resize_md(a->rows, a->cols, ret);
if(ret == NULL) { return 0; }
}
for(int i = 0; i < a->rows; i++) {
for(int j = 0; j < a->cols; j++) {
ret->m[i][j] = b * a->m[i][j];
}
}
return 1;
}
int mul_mdmd(md *a, md *b, md *ret) {
// check if we can do this multiplication
if(a->cols != b->rows) {
return 0;
}
// make ret the required size
if(!(ret->rows == a->rows && ret->cols == b->cols)) {
ret = resize_md(a->rows, b->cols, ret);
if(ret == NULL) { return 0; }
}
// do the multiplication
for(int i = 0; i < ret->rows; i++) {
for(int j = 0; j < ret->cols; j++) {
ret->m[i][j] = 0;
for(int k = 0; k < a->cols; a++) {
// a has the same row as ret, b has the same col as ret
ret->m[i][j] += a->m[i][k] * b->m[k][j];
}
}
}
return 1;
}
double highest_md(md *a) {
double ret = 0;
for(int i = 0; i < a->rows; i++) {
for(int j = 0; j < a->cols; j++) {
ret = MAX(ret, ABS(a->m[i][j]));
}
}
return ret;
}
int print_md(md *a) {
// create format for a regular string
char str[100] = "";
double highest = highest_md(a);
sprintf(str, "%f", highest);
int len = strlen(str);
char fmt[100] = "";
sprintf(fmt, " %%-%d.3", len);
for(int i = 0; i < a->rows; i++) {
printf("| ");
for(int j = 0; j < a->cols; j++) {
printf(fmt, a->m[i][j]);
}
printf(" |\n");
}
return 1;
}