Strassen方法求矩阵乘法

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值