Strassen方法求矩阵乘法
a: mxn b:nxk ,a和b相乘后c的维数为mxk
矩阵维数要求 m, n, k均为2的幂
#include<iostream>
using namespace std;
//typedef a data type to make the input matrix type flexible
typedef int DATATYPE;
//define a data structure to make the code simple
class SubMat
{
public:
DATATYPE *p;
int row, col;//total row and column
int subsr, subsc;//start row index and column index of sub matrix
int subRow, subCol;//row and column of sub matrix
SubMat(DATATYPE *datap,int dataRow,int dataCol) : subsr(0), subsc(0), subRow(0), subCol(0)
{
row = subRow = dataRow;
col = subCol = dataCol;
p = datap;
}
DATATYPE GetData(int i, int j)
{
return p[(subsr + i) * col + subsc + j];
}
void SetData(int i, int j, DATATYPE val)
{
p[(subsr + i) * col + subsc + j] = val;
}
};
void MatrixAddAB(SubMat& a,SubMat& b,SubMat& c)
{
for (int i = 0; i < a.subRow; i++)
{
for (int j = 0; j < a.subCol; j++)
{
c.SetData(i, j, a.GetData(i, j) + b.GetData(i, j));
}
}
}
void MatrixMinusAB(SubMat& a, SubMat& b, SubMat& c)
{
for (int i = 0; i < a.subRow; i++)
{
for (int j = 0; j < a.subCol; j++)
{
c.SetData(i, j, a.GetData(i, j) - b.GetData(i, j));
}
}
}
//recursive function to solve the matrix multiplication
void MatrixMultiplyAB(SubMat &a,SubMat &b,SubMat &c)
{
if (a.subCol == 0 || a.subRow == 0 || b.subRow == 0 || b.subCol == 0)
{
return;
}
else if (a.subCol == 1 || a.subRow == 1 || b.subRow == 1 || b.subCol == 1