头文件如下:
#ifndef StrassenAlgorithms_Algorithm_h
#define StrassenAlgorithms_Algorithm_h
template<int size>
void strassen_matrix_multiplication(int A[size][size], int B[size][size], int C[size][size])
#include "Algorithm.cpp"
#endif
第二个是对应的 .cpp 文件:
template<int size>
void strassen_matrix_multiplication(int A[size][size], int B[size][size], int C[size][size]){
if (size == 1) {
return;
}
const int n = size / 2;
int A11[n][n];
int A12[n][n];
int A21[n][n];
int A22[n] [n];
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
A11[i][j] = A[i][j];
A12[i][j] = A[i + n][j];
A21[i][j] = A[i][j + n];
A22[i][j] = A[i + n][j + n];
}
}
int B11[n][n], B12[n][n], B21[n][n], B22[n][n];
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
B11[i][j] = A[i][j];
B12[i][j] = A[i + n][j];
B21[i][j] = A[i][j + n];
B22[i][j] = A[i + n][j + n];
}
}
/** S1 = B12 - B22 */
int S1[n][n];
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
S1[i][j] = B[i + n][j] - B[i + n][j + n];
}
}
/** S2 = A11 + A12 */
int S2[n][n];
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
S2[i][j] = A[i][j] + A[i + n][j];
}
}
/** S3 = A21 + A22 */
int S3[n][n];
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
S3[i][j] = A[i][j + n] + A[i + n][j + n];
}
}
/** S4 = B21 - B11 */
int S4[n][n];
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
S4[i][j] = B[i][j + n] - B[i][j];
}
}
/** S5 = A11 + A22 */
int S5[n][n];
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
S5[i][j] = A[i][j] + A[i + n][j + n];
}
}
/** S6 = B11 + B22 */
int S6[n][n];
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
S6[i][j] = B[i][j] + B[i + n][j + n];
}
}
/** S7 = A12 - A22 */
int S7[n][n];
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
S7[i][j] = A[i + n][j] - A[i + n][j + n];
}
}
/** S8 = B21 + B22 */
int S8[n][n];
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
S8[i][j] = B[n][j + n] + B[i + n][j + n];
}
}
/** S9 = A11 - A21 */
int S9[n][n];
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
S9[i][j] = A[i][j] - A[i][j + n];
}
}
/** S10 = B11 + B12 */
int S10[n][n];
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
S10[i][j] = B[i][j] + B[i + n][j];
}
}
int P1[n][n];
strassen_matrix_multiplication<n>(A11, S1, P1);
int P2[n][n];
strassen_matrix_multiplication<n>(S2, B22, P2);
int P3[n][n];
strassen_matrix_multiplication<n>(S3, B11, P3);
int P4[n][n];
strassen_matrix_multiplication<n>(A22, S4, P4);
int P5[n][n];
strassen_matrix_multiplication<n>(S5, S6, P5);
int P6[n][n];
strassen_matrix_multiplication<n>(S7, S8, P6);
int P7[n][n];
strassen_matrix_multiplication<n>(S9, S10, P7);
/** C11 = P5 + P4 - P2 + P6 **/
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
C[i][j] = P5[i][j] + P4[i][j] - P2[i][j] + P6[i][j];
}
}
/** C12 = P1 + P2 **/
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
C[i + n][j] = P1[i][j] + P2[i][j];
}
}
/** C21 = P3 + P4 **/
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
C[i][j + n] = P3[i][j] + P4[i][j];
}
}
/** C22 = P5 + P1 - P3 - P7 **/
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
C[i + n][j + n] = P5[i][j] + P1[i][j] - P3[i][j] - P7[i][j];
}
}
return;
}
最后是主文件:
#include <iostream>
#include "Algorithm.h"
using namespace std;
int main(int argc, const char * argv[])
{
const int size = 2;
int A[size][size] = {{1, 2}, {3, 4}};
int B[size][size] = {{1, 2}, {3, 4}};
int C[size][size] = {0, 0, 0, 0};
strassen_matrix_multiplication<2>(A, B, C);
for(int i=0; i<size; i++){
for (int j=0; j<size; j++) {
cout << C[i][j] << "\t";
}
cout << endl;
}
}
我有两个编译错误,一个在 .cpp 文件中说
"Expected initializer before 'template'"
在第一行。我不知道这是什么意思。另一个在主文件中说
"'strassen_matrix_multiplication' not declared in this scope"
我该如何解决这些错误?非常感谢。