跳转至

strassen矩阵乘法

  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
#include <stdio.h>
#include <stdlib.h>

#define N 512   //定义矩阵的大小(行数),默认参与运算的矩阵为方阵 

void plus(int size, int **A, int **B, int **C) {  //计算 A+B->C
    int i, j;
    for (i = 0; i < size; i++) {
        for (j = 0; j < size; j++) {
            C[i][j] = A[i][j] + B[i][j];
        }
    }
}

void minus(int size, int **A, int **B, int **C) {  //计算 A-B->C
    int i, j;
    for (i = 0; i < size; i++) {
        for (j = 0; j < size; j++) {
            C[i][j] = A[i][j] - B[i][j];
        }
    }
}

void multiply(int size, int **A, int **B, int **C) {  //常规算法计算 A*B->C 
    int i, j, k;
    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] = C[i][j] + A[i][k]*B[k][j];          
           }    
        }          
     }  
}  

void strassen(int size, int **A, int **B, int **C) {  //strassen算法计算 A*B->C 
    int half = size / 2;
    if (size == 2) {  
        multiply(size, A, B, C);  //当矩阵阶数为 2 时,达到递归边界,直接按常规算法计算
    } 
    else {
        int **A11, **A12, **A21, **A22;  //Divide matrix A into 4 sub-matrices 
        int **B11, **B12, **B21, **B22;  //Divide matrix B into 4 sub-matrices
        int **C11, **C12, **C21, **C22;  //Divide matrix C into 4 sub-matrices
        int **AA, **BB;  //AA记录矩阵A的子矩阵运算的中间结果,BB记录矩阵B的子矩阵运算的中间结果 
        int **P1, **P2, **P3, **P4, **P5, **P6, **P7;
        //为上述矩阵开空间 
        A11 = (int**)malloc(sizeof(int*) * half);
        A12 = (int**)malloc(sizeof(int*) * half);
        A21 = (int**)malloc(sizeof(int*) * half);
        A22 = (int**)malloc(sizeof(int*) * half);
        B11 = (int**)malloc(sizeof(int*) * half);
        B12 = (int**)malloc(sizeof(int*) * half);
        B21 = (int**)malloc(sizeof(int*) * half);
        B22 = (int**)malloc(sizeof(int*) * half);
        C11 = (int**)malloc(sizeof(int*) * half);
        C12 = (int**)malloc(sizeof(int*) * half);
        C21 = (int**)malloc(sizeof(int*) * half);
        C22 = (int**)malloc(sizeof(int*) * half);
        AA = (int**)malloc(sizeof(int*) * half);
        BB = (int**)malloc(sizeof(int*) * half);
        P1 = (int**)malloc(sizeof(int*) * half);
        P2 = (int**)malloc(sizeof(int*) * half);
        P3 = (int**)malloc(sizeof(int*) * half);
        P4 = (int**)malloc(sizeof(int*) * half);
        P5 = (int**)malloc(sizeof(int*) * half);
        P6 = (int**)malloc(sizeof(int*) * half);
        P7 = (int**)malloc(sizeof(int*) * half);

        int i, j;   
        for (i = 0; i < half; i++) {
            A11[i] = (int*)malloc(sizeof(int) * half);
            A12[i] = (int*)malloc(sizeof(int) * half);
            A21[i] = (int*)malloc(sizeof(int) * half);
            A22[i] = (int*)malloc(sizeof(int) * half);
            B11[i] = (int*)malloc(sizeof(int) * half);
            B12[i] = (int*)malloc(sizeof(int) * half);
            B21[i] = (int*)malloc(sizeof(int) * half);
            B22[i] = (int*)malloc(sizeof(int) * half);
            C11[i] = (int*)malloc(sizeof(int) * half);
            C12[i] = (int*)malloc(sizeof(int) * half);
            C21[i] = (int*)malloc(sizeof(int) * half);
            C22[i] = (int*)malloc(sizeof(int) * half);
            AA[i] = (int*)malloc(sizeof(int) * half);
            BB[i] = (int*)malloc(sizeof(int) * half);
            P1[i] = (int*)malloc(sizeof(int) * half);
            P2[i] = (int*)malloc(sizeof(int) * half);
            P3[i] = (int*)malloc(sizeof(int) * half);
            P4[i] = (int*)malloc(sizeof(int) * half);
            P5[i] = (int*)malloc(sizeof(int) * half);
            P6[i] = (int*)malloc(sizeof(int) * half);
            P7[i] = (int*)malloc(sizeof(int) * half);
        }

        //将 A, B矩阵填入各自的分块矩阵 
        for (i = 0; i < half; i++) {
            for (j = 0; j < half; j++) {
                A11[i][j] = A[i][j];
                A12[i][j] = A[i][j + half];  
                A21[i][j] = A[i + half][j];  
                A22[i][j] = A[i + half][j + half];  

                B11[i][j] = B[i][j];  
                B12[i][j] = B[i][j + half];  
                B21[i][j] = B[i + half][j];  
                B22[i][j] = B[i + half][j + half]; 
            }
        }

        //计算开始 
        minus(half, B12, B22, BB);  //Calculating P1 = A11 * (B12 - B22)
        strassen(half, A11, BB, P1);

        plus(half, A11, A12, AA);  //Calculating P2 = (A11 + A12) * B22
        strassen(half, AA, B22, P2);

        plus(half, A21, A22, AA);  //Calculating P3 = (A21 + A22) * B11
        strassen(half, AA, B11, P3);

        minus(half, B21, B11, BB);  //Calculating P4 = A22 * (B21 - B11)
        strassen(half, A22, BB, P4);

        plus(half, A11, A22, AA);  //Calculating P5 = (A11 + A22) * (B11 + B22)
        plus(half, B11, B22, BB);
        strassen(half, AA, BB, P5);

        minus(half, A12, A22, AA);  //Calculating P6 = (A12 - A22) * (B21 + B22)
        plus(half, B21, B22, BB);
        strassen(half, AA, BB, P6);

        minus(half, A11, A21, AA);  //Calculating P7 = (A11 - A21) * (B11 + B12)
        plus(half, B11, B12, BB);
        strassen(half, AA, BB, P7);


        plus(half, P5, P4, C11);  //Calculating C11 = P5 + P4 - P2 + P6
        minus(half, C11, P2, C11);
        plus(half, C11, P6, C11);

        plus(half, P1, P2, C12);  //Calculating C12 = P1 + P2

        plus(half, P3, P4, C21);  //Calculating C21 = P3 + P4

        plus(half, P5, P1, C22);  //Calculating C22 = P5 + P1 - P3 - P7
        minus(half, C22, P3, C22);
        minus(half, C22, P7, C22);

        //将矩阵C的四个分块矩阵合并填入C中 
        for (i = 0; i < half; i++) {  
            for (j = 0; j < half; j++) {  
                C[i][j] = C11[i][j];  
                C[i][j + half] = C12[i][j];  
                C[i + half][j] = C21[i][j];  
                C[i + half][j + half] = C22[i][j];          
            }          
        }  
        //释放空间 
        for (i = 0; i < half; i++) {
            free(A11[i]);
            free(A12[i]);
            free(A21[i]);
            free(A22[i]);               
            free(B11[i]);
            free(B12[i]);
            free(B21[i]);               
            free(B22[i]);               
            free(C11[i]);
            free(C12[i]);
            free(C21[i]);               
            free(C22[i]);               
            free(P1[i]);
            free(P2[i]);
            free(P3[i]);
            free(P4[i]);                
            free(P5[i]);
            free(P6[i]);
            free(P7[i]);                
            free(AA[i]);
            free(BB[i]);            
        }
        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(P1);
        free(P2);
        free(P3);
        free(P4);
        free(P5);               
        free(P6);
        free(P7);               
        free(AA);               
        free(BB);
    }
}

int main() {
    int **A = (int**)malloc(sizeof(int*) * N); 
    int **B = (int**)malloc(sizeof(int*) * N);
    int **C = (int**)malloc(sizeof(int*) * N); 
    int i, j;
    for (i = 0; i < N; i++) {
        A[i] = (int*)malloc(sizeof(int) * N);
        B[i] = (int*)malloc(sizeof(int) * N);
        C[i] = (int*)malloc(sizeof(int) * N);
    }

    //从文件中读取矩阵A 
    FILE *fa;
    fa = fopen("la.txt", "r");
    if (fa == NULL) {
        printf("Cannot get matrix A!\n");
        exit(0);
    }
    else {
        for (i = 0; i < N; i++) {
            for (j = 0; j < N; j++) {
                fscanf(fa, "%d", &A[i][j]);
            }
        }
    }
    fclose(fa);

    //从文件中读取矩阵B 
    FILE *fb;
    fb = fopen("lb.txt", "r");
    if (fb == NULL) {
        printf("Cannot get matrix B!\n");
        exit(0);
    }
    else {
        for (i = 0; i < N; i++) {
            for (j = 0; j < N; j++) {
                fscanf(fb, "%d", &B[i][j]);
            }
        }
    }
    fclose(fb);

    strassen(N, A, B, C);  //strassen算法计算 A*B->C 

    //打印结果到屏幕的同时,格式化输出到文件 
    FILE *fc = fopen("lc.txt", "w");
    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            printf("%d ", C[i][j]);
            if (j != N - 1) {
                fprintf(fc, "%d\t", C[i][j]);
            }
            else {
                fprintf(fc, "%d\n", C[i][j]);
            }
        }
        printf("\n");
    }
    return 0;
}