-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.c
More file actions
176 lines (143 loc) · 3.75 KB
/
Copy pathmatrix.c
File metadata and controls
176 lines (143 loc) · 3.75 KB
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
#include "matrix.h"
#include <stdlib.h>
static unsigned int seed = 2121;
static unsigned int gen_rand() {
seed ^= seed >> 16;
seed *= 17;
seed ^= seed << 15;
seed += 15;
return seed;
}
int matrix_alloc(int row, int col, float **mat) {
if (row <= 0 || col <= 0 || mat == NULL)
return 1;
*mat = malloc(row * col * sizeof(float));
if (*mat == NULL)
return 2;
return 0;
}
int matrix_rand(float *mat, int row, int col) {
if (row <= 0 || col <= 0 || mat == NULL)
return 1;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
unsigned int r = gen_rand();
float val = ((float)r / (float)0xFFFFFFFF) * 2.0f - 1.0f;
mat[i * col + j] = val;
}
}
return 0;
}
void matrix_free(float **mat) {
if (mat != NULL && *mat != NULL) {
free(*mat);
*mat = NULL;
}
}
int transpose(const float *mat, int row, int col, float **out_mat) {
if (row <= 0 || col <= 0 || mat == NULL || out_mat == NULL)
return 1;
float *new_mat = NULL;
if (matrix_alloc(col, row, &new_mat) != 0) {
return 2;
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
new_mat[j * row + i] = mat[i * col + j];
}
}
*out_mat = new_mat;
return 0;
}
int matrix_mult(const float *mat_a, int row_a, int col_a, const float *mat_b,
int row_b, int col_b, float **out_mat) {
if (row_a <= 0 || row_b <= 0 || col_a <= 0 || col_b <= 0 || mat_a == NULL ||
mat_b == NULL || out_mat == NULL || col_a != row_b)
return 1;
float *mat = NULL;
if (matrix_alloc(row_a, col_b, &mat) != 0) {
return 2;
}
for (int i = 0; i < row_a; i++) {
for (int j = 0; j < col_b; j++) {
mat[i * col_b + j] = 0.0f;
for (int k = 0; k < col_a; k++) {
mat[i * col_b + j] += mat_a[i * col_a + k] * mat_b[k * col_b + j];
}
}
}
*out_mat = mat;
return 0;
}
int matrix_add(const float *mat_a, const float *mat_b, int row, int col,
float **out_mat) {
if (row <= 0 || col <= 0 || mat_a == NULL || mat_b == NULL ||
out_mat == NULL) {
return 1;
}
float *mat = NULL;
if (matrix_alloc(row, col, &mat) != 0) {
return 2;
}
int total = row * col;
for (int i = 0; i < total; i++) {
mat[i] = mat_a[i] + mat_b[i];
}
*out_mat = mat;
return 0;
}
int matrix_sub(const float *mat_a, const float *mat_b, int row, int col,
float **out_mat) {
if (row <= 0 || col <= 0 || mat_a == NULL || mat_b == NULL ||
out_mat == NULL) {
return 1;
}
float *mat = NULL;
if (matrix_alloc(row, col, &mat) != 0) {
return 2;
}
int total = row * col;
for (int i = 0; i < total; i++) {
mat[i] = mat_a[i] - mat_b[i];
}
*out_mat = mat;
return 0;
}
int matrix_map(const float *mat, int row, int col, float (*func)(float),
float **out_mat) {
if (row <= 0 || col <= 0 || mat == NULL || func == NULL || out_mat == NULL)
return 1;
float *new_mat = NULL;
if (matrix_alloc(row, col, &new_mat) != 0) {
return 2;
}
int total_elements = row * col;
for (int i = 0; i < total_elements; i++) {
new_mat[i] = func(mat[i]);
}
*out_mat = new_mat;
return 0;
}
int matrix_hadamard(const float *mat_a, const float *mat_b, int row, int col,
float **out_mat) {
if (row <= 0 || col <= 0 || mat_a == NULL || mat_b == NULL || out_mat == NULL)
return 1;
float *mat = NULL;
if (matrix_alloc(row, col, &mat) != 0)
return 2;
int total = row * col;
for (int i = 0; i < total; i++) {
mat[i] = mat_a[i] * mat_b[i];
}
*out_mat = mat;
return 0;
}
int matrix_scale(float *mat, int row, int col, float scalar) {
if (row <= 0 || col <= 0 || mat == NULL)
return 1;
int total = row * col;
for (int i = 0; i < total; i++) {
mat[i] *= scalar;
}
return 0;
}