-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP4.c
More file actions
463 lines (440 loc) · 13.6 KB
/
P4.c
File metadata and controls
463 lines (440 loc) · 13.6 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
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define M_SIZE 8 //Number of row and column of the Matrix
#define RAN 1000 //rand
void printm(int**,int);
int **Standard(int**, int**, int);
int **Addm(int**,int**,int); // Returns A+B
int **Subm(int**,int**,int); // Returns A-B
int **Recursion(int **, int **,int);
int **Strassen(int**, int**,int);
int main()
{
int **a,**b,**c;
int i,j;
a = (int**)malloc(sizeof(int*)*M_SIZE);
b = (int**)malloc(sizeof(int*)*M_SIZE);
c = (int**)malloc(sizeof(int*)*M_SIZE);
srand(time(NULL));
for(i=0;i<M_SIZE;i++)
{
a[i]=(int*)malloc(sizeof(int)*M_SIZE);
b[i]=(int*)malloc(sizeof(int)*M_SIZE);
c[i]=(int*)malloc(sizeof(int)*M_SIZE);
}
for(i=0;i<M_SIZE;i++)
{
for(j=0;j<M_SIZE;j++)
{
a[i][j] = rand()%RAN;
b[i][j] = rand()%RAN;
}
}
/*
Please Select function
Standard,
Recursion,
Strassen
number of parameter and type of parameter
is all the same.
Just Change the function name below.
Thank you.
*/
c = Strassen(a,b,M_SIZE);
//Prints Matrices A,B, and C
printf("A->\n");
printm(a,M_SIZE);
printf("\n");
printf("B->\n");
printm(b,M_SIZE);
printf("C->\n");
printf("\n");
printm(c,M_SIZE);
free(*a);
free(*b);
free(*c);
free(a);
free(b);
free(c);
return 0;
}
void printm(int **x,int size)
{
int i,j;
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
printf("%8d ",x[i][j]);
}
printf("\n");
}
}
int **Standard(int **a, int **b, int size)
{
int i,j,k;
int **c = (int**)malloc(sizeof(int*)*size);
for(i=0;i<size;i++)
{
c[i] = (int*)malloc(sizeof(int)*size);
}
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
c[i][j] = 0;
for(k=0;k<size;k++)
{
c[i][j] += a[i][k] * b[k][j];
}
}
}
return c;
}
int **Addm(int **a1, int **a2, int size)
{
int i,j;
int **res = (int**)malloc(sizeof(int*)*size);
for(i=0;i<size;i++)
{
res[i] = (int*)malloc(sizeof(int)*size);
}
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
res[i][j] = a1[i][j] + a2[i][j];
}
}
return res;
}
int **Subm(int **a1,int **a2,int size)
{
int i,j;
int **res = (int**)malloc(sizeof(int*)*size);
for(i=0;i<size;i++)
{
res[i] = (int*)malloc(sizeof(int)*size);
}
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
res[i][j] = a1[i][j] - a2[i][j];
}
}
return res;
}
int **Recursion(int **a, int **b,int size)
{
static int count = 0;
int i,j;
int **c = (int**)malloc(sizeof(int*)*size);
for(i=0;i<size;i++)
{
c[i] = (int*)malloc(sizeof(int)*size);
}
if(size == 1)
{
c[0][0] = a[0][0] * b[0][0];
printf("Recursive Partial Matrix Constructed! #%d\n",++count);
return c;
}
else // NOTICE! At Below of the Code
{
//Allocating Memory
int **a11 = (int**)malloc(sizeof(int*)*(size/2));
int **a12 = (int**)malloc(sizeof(int*)*(size/2));
int **a21 = (int**)malloc(sizeof(int*)*(size/2));
int **a22 = (int**)malloc(sizeof(int*)*(size/2));
int **b11 = (int**)malloc(sizeof(int*)*(size/2));
int **b12 = (int**)malloc(sizeof(int*)*(size/2));
int **b21 = (int**)malloc(sizeof(int*)*(size/2));
int **b22 = (int**)malloc(sizeof(int*)*(size/2));
int **c11 = (int**)malloc(sizeof(int*)*(size/2));
int **c12 = (int**)malloc(sizeof(int*)*(size/2));
int **c21 = (int**)malloc(sizeof(int*)*(size/2));
int **c22 = (int**)malloc(sizeof(int*)*(size/2));
for(i=0;i<size/2;i++)
{
a11[i] = (int*)malloc(sizeof(int)*(size/2));
a12[i] = (int*)malloc(sizeof(int)*(size/2));
a21[i] = (int*)malloc(sizeof(int)*(size/2));
a22[i] = (int*)malloc(sizeof(int)*(size/2));
b11[i] = (int*)malloc(sizeof(int)*(size/2));
b12[i] = (int*)malloc(sizeof(int)*(size/2));
b21[i] = (int*)malloc(sizeof(int)*(size/2));
b22[i] = (int*)malloc(sizeof(int)*(size/2));
c11[i] = (int*)malloc(sizeof(int)*(size/2));
c12[i] = (int*)malloc(sizeof(int)*(size/2));
c21[i] = (int*)malloc(sizeof(int)*(size/2));
c22[i] = (int*)malloc(sizeof(int)*(size/2));
}
for(i=0;i<size/2;i++)
{
for(j=0;j<size/2;j++)
{
a11[i][j] = a[i][j];
a12[i][j] = a[i][j+size/2];
a21[i][j] = a[i+size/2][j];
a22[i][j] = a[i+size/2][j+size/2];
b11[i][j] = b[i][j];
b12[i][j] = b[i][j+size/2];
b21[i][j] = b[i+size/2][j];
b22[i][j] = b[i+size/2][j+size/2];
}
}
c11 = Addm(Recursion(a11,b11,size/2),Recursion(a12,b21,size/2),size/2);
printf("Recursive Partial Matrix Constructed! #%d\n",++count);
c12 = Addm(Recursion(a11,b12,size/2),Recursion(a12,b22,size/2),size/2);
printf("Recursive Partial Matrix Constructed! #%d\n",++count);
c21 = Addm(Recursion(a21,b11,size/2),Recursion(a22,b21,size/2),size/2);
printf("Recursive Partial Matrix Constructed! #%d\n",++count);
c22 = Addm(Recursion(a21,b12,size/2),Recursion(a22,b22,size/2),size/2);
printf("Recursive Partial Matrix Constructed! #%d\n",++count);
for(i=0;i<size/2;i++)
{
for(j=0;j<size/2;j++)
{
c[i][j] = c11[i][j];
c[i][j+size/2] = c12[i][j];
c[i+size/2][j] = c21[i][j];
c[i+size/2][j+size/2] = c22[i][j];
}
}
//Let the heap become free
free(*a11);
free(*a12);
free(*a21);
free(*a22);
free(*b11);
free(*b12);
free(*b21);
free(*b22);
free(*c11);
free(*c12);
free(*c21);
free(*c22);
free(a11);
free(a12);
free(a21);
free(a22);
free(b11);
free(b12);
free(b21);
free(b22);
free(c11);
free(c12);
free(c21);
free(c22);
return c;
}
}
int **Strassen(int **a, int **b,int size)
{
static int counts = 0;
int i,j;
int **c = (int**)malloc(sizeof(int*)*size);
for(i=0;i<size;i++)
{
c[i] = (int*)malloc(sizeof(int)*size);
}
if(size == 1)
{
c[0][0] = a[0][0] * b[0][0];
printf("Strassen Partial Matrix Constructed! #%d\n",++counts);
return c;
}
else // NOTICE! At Below of the Code
{
//Stage 1 and some declaration, allocating memory
int **a11 = (int**)malloc(sizeof(int*)*(size/2));
int **a12 = (int**)malloc(sizeof(int*)*(size/2));
int **a21 = (int**)malloc(sizeof(int*)*(size/2));
int **a22 = (int**)malloc(sizeof(int*)*(size/2));
int **b11 = (int**)malloc(sizeof(int*)*(size/2));
int **b12 = (int**)malloc(sizeof(int*)*(size/2));
int **b21 = (int**)malloc(sizeof(int*)*(size/2));
int **b22 = (int**)malloc(sizeof(int*)*(size/2));
int **c11 = (int**)malloc(sizeof(int*)*(size/2));
int **c12 = (int**)malloc(sizeof(int*)*(size/2));
int **c21 = (int**)malloc(sizeof(int*)*(size/2));
int **c22 = (int**)malloc(sizeof(int*)*(size/2));
int **s1 = (int**)malloc(sizeof(int*)*(size/2));
int **s2 = (int**)malloc(sizeof(int*)*(size/2));
int **s3 = (int**)malloc(sizeof(int*)*(size/2));
int **s4 = (int**)malloc(sizeof(int*)*(size/2));
int **s5 = (int**)malloc(sizeof(int*)*(size/2));
int **s6 = (int**)malloc(sizeof(int*)*(size/2));
int **s7 = (int**)malloc(sizeof(int*)*(size/2));
int **s8 = (int**)malloc(sizeof(int*)*(size/2));
int **s9 = (int**)malloc(sizeof(int*)*(size/2));
int **s10 = (int**)malloc(sizeof(int*)*(size/2));
int **p1 = (int**)malloc(sizeof(int*)*(size/2));
int **p2 = (int**)malloc(sizeof(int*)*(size/2));
int **p3 = (int**)malloc(sizeof(int*)*(size/2));
int **p4 = (int**)malloc(sizeof(int*)*(size/2));
int **p5 = (int**)malloc(sizeof(int*)*(size/2));
int **p6 = (int**)malloc(sizeof(int*)*(size/2));
int **p7 = (int**)malloc(sizeof(int*)*(size/2));
for(i=0;i<size/2;i++)
{
a11[i] = (int*)malloc(sizeof(int)*(size/2));
a12[i] = (int*)malloc(sizeof(int)*(size/2));
a21[i] = (int*)malloc(sizeof(int)*(size/2));
a22[i] = (int*)malloc(sizeof(int)*(size/2));
b11[i] = (int*)malloc(sizeof(int)*(size/2));
b12[i] = (int*)malloc(sizeof(int)*(size/2));
b21[i] = (int*)malloc(sizeof(int)*(size/2));
b22[i] = (int*)malloc(sizeof(int)*(size/2));
c11[i] = (int*)malloc(sizeof(int)*(size/2));
c12[i] = (int*)malloc(sizeof(int)*(size/2));
c21[i] = (int*)malloc(sizeof(int)*(size/2));
c22[i] = (int*)malloc(sizeof(int)*(size/2));
s1[i] = (int*)malloc(sizeof(int)*(size/2));
s2[i] = (int*)malloc(sizeof(int)*(size/2));
s3[i] = (int*)malloc(sizeof(int)*(size/2));
s4[i] = (int*)malloc(sizeof(int)*(size/2));
s5[i] = (int*)malloc(sizeof(int)*(size/2));
s6[i] = (int*)malloc(sizeof(int)*(size/2));
s7[i] = (int*)malloc(sizeof(int)*(size/2));
s8[i] = (int*)malloc(sizeof(int)*(size/2));
s9[i] = (int*)malloc(sizeof(int)*(size/2));
s10[i] = (int*)malloc(sizeof(int)*(size/2));
p1[i] = (int*)malloc(sizeof(int)*(size/2));
p2[i] = (int*)malloc(sizeof(int)*(size/2));
p3[i] = (int*)malloc(sizeof(int)*(size/2));
p4[i] = (int*)malloc(sizeof(int)*(size/2));
p5[i] = (int*)malloc(sizeof(int)*(size/2));
p6[i] = (int*)malloc(sizeof(int)*(size/2));
p7[i] = (int*)malloc(sizeof(int)*(size/2));
}
for(i=0;i<size/2;i++)
{
for(j=0;j<size/2;j++)
{
a11[i][j] = a[i][j];
a12[i][j] = a[i][j+size/2];
a21[i][j] = a[i+size/2][j];
a22[i][j] = a[i+size/2][j+size/2];
b11[i][j] = b[i][j];
b12[i][j] = b[i][j+size/2];
b21[i][j] = b[i+size/2][j];
b22[i][j] = b[i+size/2][j+size/2];
}
}
//Now for the Stage 2
s1 = Subm(b12,b22,size/2);
s2 = Addm(a11,a12,size/2);
s3 = Addm(a21,a22,size/2);
s4 = Subm(b21,b11,size/2);
s5 = Addm(a11,a22,size/2);
s6 = Addm(b11,b22,size/2);
s7 = Subm(a12,a22,size/2);
s8 = Addm(b21,b22,size/2);
s9 = Subm(a11,a21,size/2);
s10 = Addm(b11,b12,size/2);
//Now for the Stage 3
p1 = Strassen(a11,s1,size/2);
p2 = Strassen(s2,b22,size/2);
p3 = Strassen(s3,b11,size/2);
p4 = Strassen(a22,s4,size/2);
p5 = Strassen(s5,s6,size/2);
p6 = Strassen(s7,s8,size/2);
p7 = Strassen(s9,s10,size/2);
//Now for the Stage 4
c11 = Subm(Addm(p5,p4,size/2),Subm(p2,p6,size/2),size/2);
printf("Strassen Partial Matrix Constructed! #%d\n",++counts);
c12 = Addm(p1,p2,size/2);
printf("Strassen Partial Matrix Constructed! #%d\n",++counts);
c21 = Addm(p3,p4,size/2);
printf("Strassen Partial Matrix Constructed! #%d\n",++counts);
c22 = Subm(Addm(p5,p1,size/2),Addm(p3,p7,size/2),size/2);
printf("Strassen Partial Matrix Constructed! #%d\n",++counts);
for(i=0;i<size/2;i++)
{
for(j=0;j<size/2;j++)
{
c[i][j] = c11[i][j];
c[i][j+size/2] = c12[i][j];
c[i+size/2][j] = c21[i][j];
c[i+size/2][j+size/2] = c22[i][j];
}
}
//Let the heap become free
free(*a11);
free(*a12);
free(*a21);
free(*a22);
free(*b11);
free(*b12);
free(*b21);
free(*b22);
free(*s1);
free(*s2);
free(*s3);
free(*s4);
free(*s5);
free(*s6);
free(*s7);
free(*s8);
free(*s9);
free(*s10);
free(*p1);
free(*p2);
free(*p3);
free(*p4);
free(*p5);
free(*p6);
free(*p7);
free(*c11);
free(*c12);
free(*c21);
free(*c22);
free(a11);
free(a12);
free(a21);
free(a22);
free(b11);
free(b12);
free(b21);
free(b22);
free(s1);
free(s2);
free(s3);
free(s4);
free(s5);
free(s6);
free(s7);
free(s8);
free(s9);
free(s10);
free(p1);
free(p2);
free(p3);
free(p4);
free(p5);
free(p6);
free(p7);
free(c11);
free(c12);
free(c21);
free(c22);
return c;
}
}
/*
NOTICE!
In Recursion and Strassen's Method,
I think I have to Divide differently
whether size(or n) is an even number,
or an non-1 odd number.
But, in the question, size(or n) is always
even number, or 1, even the Recursion is made.
So I just divided into two n/2s.
And Also, All the matrices are square.
So I didn't implemented termination code
when the multiplication, addition, subtraction
between two matrices are not defined.
*/