Java实现任意矩阵Strassen算法

来源:互联网 发布:ipad 电容笔 知乎 编辑:程序博客网 时间:2024/06/07 09:53
本例输入为两个任意尺寸的矩阵m * n, n * m,输出为两个矩阵的乘积。计算任意尺寸矩阵相乘时,使用了Strassen算法。程序为自编,经过测试,请放心使用。基本算法是:
1.对于方阵(正方形矩阵),找到最大的l, 使得l = 2 ^ k, k为整数并且l < m。边长为l的方形矩阵则采用Strassen算法,其余部分以及方形矩阵中遗漏的部分用蛮力法。
2.对于非方阵,依照行列相应添加0使其成为方阵。


参考资料:1. http://wenku.baidu.com/link?url=qVuG2cKevujSNCSVtK1uS8k5VJu3gzJ_ZNs99un3uv5v5Mw2YEY2NlZbY9CI52HMYADnnovZNXgfEAfYSFIK3EPp9su5zhkJdgwStxxFDPy
2.Introduction to Algorithms, 3rd Edition


This program implements m * n matrices by using Strassen's algorithm. It is tested. The key is to divide the matrices into 2 cases:
1. If the matrices are square matrices. This means that we can find sub-matrice which has the largest edge length (size) in the code that is of length that can be expressed as form 2 ^ k, whereas k is an integer.
2. If the matrices are not square matrices, add 0 to rows and columns to make it like square m
0 0