forked from sysprog21/matrix_oo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_strassen.c
317 lines (266 loc) · 8.15 KB
/
matrix_strassen.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
#include "matrix.h"
#include <stdio.h>
#include <stdlib.h>
#define ADD 1
#define SUB 0
static char INFO[] = {"Strassen"};
struct naive_priv {
int **values;
};
#define PRIV(x) \
((struct naive_priv *) ((x)->priv))
static void assign(Matrix *thiz, int row, int col, int **data)
{
thiz->row = row;
thiz->col = col;
struct naive_priv *construct = malloc(sizeof(struct naive_priv));
construct->values = (int **) malloc(thiz->row * sizeof(int *));
for (int i = 0; i < thiz->row; i++)
construct->values[i] = (int *) malloc(thiz->col * sizeof(int));
thiz->priv = construct;
for (int i = 0; i < thiz->row; i++)
for (int j = 0; j < thiz->col; j++)
PRIV(thiz)->values[i][j] = data[i][j];
}
static const float epsilon = 1 / 10000.0;
static bool equal(const Matrix *l, const Matrix *r)
{
if (l->row != r->row || l->col != r->col) {
printf("%dx%d matrix and %dx%d matrix are not equal!\n", l->row, l->col, r->row, r->col);
return false;
}
for (int i = 0; i < l->row; i++)
for (int j = 0; j < l->col; j++)
if (PRIV(l)->values[i][j] + epsilon < PRIV(r)->values[i][j] ||
PRIV(r)->values[i][j] + epsilon < PRIV(l)->values[i][j])
return false;
return true;
}
bool create_matrix(Matrix *m, int row, int col)
{
struct naive_priv *construct = malloc(sizeof(struct naive_priv));
if ( !construct )
return false;
construct->values = (int **) malloc(row * sizeof(int *));
for (int i = 0; i < col; i++)
construct->values[i] = (int *) malloc(col * sizeof(int));
m->priv = construct;
return true;
}
bool seperate(const Matrix *src, Matrix *A11, Matrix *A12, Matrix *A21, Matrix *A22)
{
int row, col, nr = 0, nc = 0, edge_r, edge_c;
row = src->row % 2 ? src->row / 2 + 1 : src->row / 2;
col = src->col % 2 ? src->col / 2 + 1 : src->col / 2;
A11->row = row;
A12->row = row;
A21->row = row;
A22->row = row;
A11->col = col;
A12->col = col;
A21->col = col;
A22->col = col;
Matrix *temp = NULL;
for (int m = 0; m < 4; m++) {
switch(m) {
case 0:
create_matrix(A11, A11->row, A11->col);
nr = 0;
nc = 0;
edge_r = row;
edge_c = col;
temp = A11;
break;
case 1:
create_matrix(A12, A12->row, A12->col);
nr = 0;
nc = col;
edge_r = row;
edge_c = src->col;
temp = A12;
for (int i = 0 ; i < row; i++) PRIV(A12)->values[i][col - 1] = 0;
break;
case 2:
create_matrix(A21, A21->row, A21->col);
nr = row;
nc = 0;
edge_r = src->row;
edge_c = col;
temp = A21;
for (int i = 0 ; i < col; i++) PRIV(A21)->values[row - 1][i] = 0;
break;
case 3:
create_matrix(A22, A22->row, A22->col);
nr = row;
nc = col;
edge_r = src->row;
edge_c = src->col;
temp = A22;
for (int i = 0 ; i < col; i++) PRIV(A22)->values[row - 1][i] = 0;
for (int i = 0 ; i < row; i++) PRIV(A22)->values[i][col - 1] = 0;
break;
default:
break;
}
for (int r = nr; r < edge_r; r++)
for (int c = nc; c < edge_c; c++)
PRIV(temp)->values[r - nr][c - nc] = PRIV(src)->values[r][c];
}
return true;
}
bool combine(Matrix *dst, Matrix *C11, Matrix *C12, Matrix *C21, Matrix *C22)
{
Matrix *temp = NULL;
int nr = 0, nc = 0;
int edge_r, edge_c;
int row = C11->row;
int col = C11->col;
for (int m = 0; m < 4; m++) {
switch(m) {
case 0:
temp = C11;
nr = 0;
nc = 0;
edge_r = row;
edge_c = col;
break;
case 1:
temp = C12;
nr = 0;
nc = col;
edge_r = row;
edge_c = dst->col;
break;
case 2:
temp = C21;
nr = row;
nc = 0;
edge_r = dst->row;
edge_c = col;
break;
case 3:
temp = C22;
nr = row;
nc = col;
edge_r = dst->row;
edge_c = dst->col;
break;
default:
break;
}
for (int r = nr; r < edge_r; r++)
for (int c = nc; c < edge_c; c++)
PRIV(dst)->values[r][c] = PRIV(temp)->values[r - nr][c - nc];
}
return true;
}
//op == ADD => add
//op == SUB => sub
bool add_sub_part(Matrix *dst, const Matrix *l, const Matrix *r, int op)
{
if (l->col != r->col || l->row != r->row) {
printf("can't add %dx%d matrix with %dx%d matrix!\n", l->row, l->col, r->row, r->col);
return false;
}
dst->row = l->row;
dst->col = l->col;
if ( !dst->priv )
create_matrix(dst, dst->row, dst->col);
if ( op ) {
for (int i = 0; i < dst->row; i++)
for (int j = 0; j < dst->col; j++)
PRIV(dst)->values[i][j] = PRIV(l)->values[i][j] + PRIV(r)->values[i][j];
} else {
for (int i = 0; i < dst->row; i++)
for (int j = 0; j < dst->col; j++)
PRIV(dst)->values[i][j] = PRIV(l)->values[i][j] - PRIV(r)->values[i][j];
}
return true;
}
bool mul_part(Matrix *dst, const Matrix *l, const Matrix *r)
{
if(l->col != r->row) {
printf("can't operate with %dx%d matrix and %dx%d matrix!\n", l->row, l->col, r->row, r->col);
return false;
}
dst->row = l->row;
dst->col = r->col;
if ( !dst->priv )
create_matrix(dst, dst->row, dst->col);
else
for (int i = 0; i < dst->row; i++)
memset(PRIV(dst)->values[i], 0, dst->col);
for (int i = 0; i < l->row; i++)
for (int j = 0; j < r->col; j++)
for (int k = 0; k < l->col; k++)
PRIV(dst)->values[i][j] += PRIV(l)->values[i][k] *
PRIV(r)->values[k][j];
return true;
}
static bool mul(Matrix *dst, const Matrix *l, const Matrix *r)
{
if(l->col != r->row) {
printf("can't operate with %dx%d matrix and %dx%d matrix!\n", l->row, l->col, r->row, r->col);
return false;
}
Matrix A11, A12, A21, A22;
Matrix B11, B12, B21, B22;
Matrix temp_l, temp_r;
Matrix M1, M2, M3, M4, M5, M6, M7;
Matrix C11, C12, C21, C22;
dst->row = l->row;
dst->col = r->col;
temp_r.priv = NULL;
temp_l.priv = NULL;
M1.priv = NULL;
M2.priv = NULL;
M3.priv = NULL;
M4.priv = NULL;
M5.priv = NULL;
M6.priv = NULL;
M7.priv = NULL;
C11.priv = NULL;
C12.priv = NULL;
C21.priv = NULL;
C22.priv = NULL;
create_matrix(dst, dst->row, dst->col);
seperate(l, &A11, &A12, &A21, &A22);
seperate(r, &B11, &B12, &B21, &B22);
add_sub_part(&temp_l, &A11, &A22, ADD);
add_sub_part(&temp_r, &B11, &B22, ADD);
mul_part(&M1, &temp_l, &temp_r);
add_sub_part(&temp_l, &A21, &A22, ADD);
mul_part(&M2, &temp_l, &B11);
add_sub_part(&temp_r, &B12, &B22, SUB);
mul_part(&M3, &A11, &temp_r);
add_sub_part(&temp_r, &B21, &B11, SUB);
mul_part(&M4, &A22, &temp_r);
add_sub_part(&temp_l, &A11, &A12, ADD);
mul_part(&M5, &temp_l, &B22);
add_sub_part(&temp_l, &A21, &A11, SUB);
add_sub_part(&temp_r, &B11, &B12, ADD);
mul_part(&M6, &temp_l, &temp_r);
add_sub_part(&temp_l, &A12, &A22, SUB);
add_sub_part(&temp_r, &B21, &B22, ADD);
mul_part(&M7, &temp_l, &temp_r);
add_sub_part(&temp_l, &M1, &M4, ADD);
add_sub_part(&temp_r, &M7, &M5, SUB);
add_sub_part(&C11, &temp_l, &temp_r, ADD);
add_sub_part(&C12, &M3, &M5, ADD);
add_sub_part(&C21, &M2, &M4, ADD);
add_sub_part(&temp_l, &M1, &M2, SUB);
add_sub_part(&temp_r, &M3, &M6, ADD);
add_sub_part(&C22, &temp_l, &temp_r, ADD);
combine(dst, &C11, &C12, &C21, &C22);
return true;
}
static void get_info(char *info)
{
strcpy(info, INFO);
}
MatrixAlgo StrassenMatrixProvider = {
.assign = assign,
.equal = equal,
.mul = mul,
.get_info = get_info,
};