-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.c
205 lines (173 loc) · 4.41 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
#include "matrix.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
// result of void operations will push into the last matrix param
void mat_put(Matrix *mat_a, Matrix *mat_b) {
if (mat_a->m != mat_b->m || mat_a->n != mat_b->n) {
mat_free_inner(mat_b);
mat_new_inner(mat_b, mat_a->m, mat_a->n, 0);
}
for (int i = 0; i < mat_b->m; i++) {
for (int j = 0; j < mat_b->n; j++) {
mat_b->data[i][j] = mat_a->data[i][j];
}
}
}
void mat_dot(Matrix *mat_a, Matrix *mat_b) {
assert(mat_a->n == mat_b->m);
Matrix *res = mat_new(mat_a->m, mat_b->n, 0);
for (int i = 0; i < mat_a->m; i++) {
for (int j = 0; j < mat_b->n; j++) {
for (int k = 0; k < mat_a->n; k++) {
res->data[i][j] += mat_a->data[i][k] * mat_b->data[k][j];
}
}
}
mat_put(res, mat_b);
mat_free(res);
}
void mat_cross(Matrix *mat_a, Matrix *mat_b) {
assert(mat_a->m == mat_b->m);
assert(mat_a->n == mat_b->n);
for (int i = 0; i < mat_a->m; i++) {
for (int j = 0; j < mat_a->n; j++) {
mat_b->data[i][j] *= mat_a->data[i][j];
}
}
}
void mat_add(Matrix *mat_a, Matrix *mat_b) {
assert(mat_a->m == mat_b->m);
assert(mat_a->n == mat_b->n);
for (int i = 0; i < mat_a->m; i++) {
for (int j = 0; j < mat_a->n; j++) {
mat_b->data[i][j] += mat_a->data[i][j];
}
}
}
void mat_trans(Matrix *mat) {
Matrix *cpy = mat_copy(mat);
mat_free_inner(mat);
mat_new_inner(mat, cpy->n, cpy->m, 0);
for (int i = 0; i < cpy->m; i++) {
for (int j = 0; j < cpy->n; j++) {
mat->data[j][i] = cpy->data[i][j];
}
}
mat_free(cpy);
}
void mat_scale(Matrix *mat, float val) {
for (int i = 0; i < mat->m; i++) {
for (int j = 0; j < mat->n; j++) {
mat->data[i][j] *= val;
}
}
}
void mat_inc(Matrix *mat, float val) {
for (int i = 0; i < mat->m; i++) {
for (int j = 0; j < mat->n; j++) {
mat->data[i][j] += val;
}
}
}
Matrix *mat_copy(Matrix *mat) {
Matrix *res = mat_new(mat->m, mat->n, 0);
for (int i = 0; i < res->m; i++) {
for (int j = 0; j < res->n; j++) {
res->data[i][j] = mat->data[i][j];
}
}
return res;
}
void mat_new_inner(Matrix *mat, int m, int n, float val) {
mat->m = m;
mat->n = n;
mat->data = malloc(m * sizeof(float*));
if (mat->data == NULL) {
printf("failed to set row data for %d by %d matrix with value %f", m, n, val);
return;
}
for (int i = 0; i < m; i++) {
mat->data[i] = malloc(n * sizeof(float));
if (mat->data[i] == NULL) {
printf("failed to set col %d for %d by %d matrix with value %f", i, m, n, val);
return;
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
mat->data[i][j] = val;
}
}
}
Matrix *mat_set_new(int m, int n, float vals[m][n]) {
Matrix* res = mat_new(m, n, 0);
mat_set(res, m, n, vals);
return res;
}
void mat_set(Matrix *mat, int m, int n, float vals[m][n]) {
for (int i = 0; i < mat->m; i++) {
for (int j = 0; j < mat->n; j++) {
mat->data[i][j] = vals[i][j];
}
}
}
Matrix *mat_new(int m, int n, float val) {
Matrix *res = malloc(sizeof(Matrix));
if (res == NULL) {
printf("failed to create %d by %d matrix with value %f", m, n, val);
return NULL;
}
mat_new_inner(res, m, n, val);
return res;
}
Matrix *mat_rand(int m, int n) {
Matrix *res = malloc(sizeof(Matrix));
if (res == NULL) {
printf("failed to create random %d by %d matrix", m, n);
return NULL;
}
res->m = m;
res->n = n;
res->data = malloc(m * sizeof(float*));
if (res->data == NULL) {
printf("failed to create row data for random %d by %d matrix", m, n);
return NULL;
}
for (int i = 0; i < m; i++) {
res->data[i] = malloc(n * sizeof(float));
if (res->data[i] == NULL) {
printf("failed to create col %d for random %d by %d matrix", i, m, n);
return NULL;
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
res->data[i][j] = rand() / (float) RAND_MAX;
}
}
return res;
}
void mat_print(Matrix *mat, const char* tag) {
printf("%s:\n", tag);
for (int i = 0; i < mat->m; i++) {
for (int j = 0; j < mat->n; j++) {
printf("%f ", mat->data[i][j]);
}
printf("\n");
}
printf("\n");
}
void mat_free_inner(Matrix *mat) {
for (int i = 0; i < mat->m; i++) {
free(mat->data[i]);
}
free(mat->data);
mat->data = NULL;
}
void mat_free(Matrix *mat) {
mat_free_inner(mat);
free(mat);
mat = NULL;
}