使用java写的矩阵乘法实例(Strassen算法)
程序员文章站
2022-07-08 22:33:54
strassen算法于1969年由德国数学家strassen提出,该方法引入七个中间变量,每个中间变量都只需要进行一次乘法运算。而朴素算法却需要进行8次乘法运算。原理strassen算法的原理如下所示...
strassen算法于1969年由德国数学家strassen提出,该方法引入七个中间变量,每个中间变量都只需要进行一次乘法运算。而朴素算法却需要进行8次乘法运算。
原理
strassen算法的原理如下所示,使用sympy验证strassen算法的正确性
import sympy as s a = s.symbol("a") b = s.symbol("b") c = s.symbol("c") d = s.symbol("d") e = s.symbol("e") f = s.symbol("f") g = s.symbol("g") h = s.symbol("h") p1 = a * (f - h) p2 = (a + b) * h p3 = (c + d) * e p4 = d * (g - e) p5 = (a + d) * (e + h) p6 = (b - d) * (g + h) p7 = (a - c) * (e + f) print(a * e + b * g, (p5 + p4 - p2 + p6).simplify()) print(a * f + b * h, (p1 + p2).simplify()) print(c * e + d * g, (p3 + p4).simplify()) print(c * f + d * h, (p1 + p5 - p3 - p7).simplify())
复杂度分析
$$f(n)=7\times f(\frac{n}{2})=7^2\times f(\frac{n}{4})=...=7^k\times f(\frac{n}{2^k})$$
最终复杂度为$7^{log_2 n}=n^{log_2 7}$
java矩阵乘法(strassen算法)
代码如下,可以看看数据结构的定义,时间换空间。
public class matrix { private final matrix[] _matrixarray; private final int n; private int element; public matrix(int n) { this.n = n; if (n != 1) { this._matrixarray = new matrix[4]; for (int i = 0; i < 4; i++) { this._matrixarray[i] = new matrix(n / 2); } } else { this._matrixarray = null; } } private matrix(int n, boolean needinit) { this.n = n; if (n != 1) { this._matrixarray = new matrix[4]; } else { this._matrixarray = null; } } public void set(int i, int j, int a) { if (n == 1) { element = a; } else { int size = n / 2; this._matrixarray[(i / size) * 2 + (j / size)].set(i % size, j % size, a); } } public matrix multi(matrix m) { matrix result = null; if (n == 1) { result = new matrix(1); result.set(0, 0, (element * m.element)); } else { result = new matrix(n, false); result._matrixarray[0] = p5(m).add(p4(m)).minus(p2(m)).add(p6(m)); result._matrixarray[1] = p1(m).add(p2(m)); result._matrixarray[2] = p3(m).add(p4(m)); result._matrixarray[3] = p5(m).add(p1(m)).minus(p3(m)).minus(p7(m)); } return result; } public matrix add(matrix m) { matrix result = null; if (n == 1) { result = new matrix(1); result.set(0, 0, (element + m.element)); } else { result = new matrix(n, false); result._matrixarray[0] = this._matrixarray[0].add(m._matrixarray[0]); result._matrixarray[1] = this._matrixarray[1].add(m._matrixarray[1]); result._matrixarray[2] = this._matrixarray[2].add(m._matrixarray[2]); result._matrixarray[3] = this._matrixarray[3].add(m._matrixarray[3]);; } return result; } public matrix minus(matrix m) { matrix result = null; if (n == 1) { result = new matrix(1); result.set(0, 0, (element - m.element)); } else { result = new matrix(n, false); result._matrixarray[0] = this._matrixarray[0].minus(m._matrixarray[0]); result._matrixarray[1] = this._matrixarray[1].minus(m._matrixarray[1]); result._matrixarray[2] = this._matrixarray[2].minus(m._matrixarray[2]); result._matrixarray[3] = this._matrixarray[3].minus(m._matrixarray[3]);; } return result; } protected matrix p1(matrix m) { return _matrixarray[0].multi(m._matrixarray[1]).minus(_matrixarray[0].multi(m._matrixarray[3])); } protected matrix p2(matrix m) { return _matrixarray[0].multi(m._matrixarray[3]).add(_matrixarray[1].multi(m._matrixarray[3])); } protected matrix p3(matrix m) { return _matrixarray[2].multi(m._matrixarray[0]).add(_matrixarray[3].multi(m._matrixarray[0])); } protected matrix p4(matrix m) { return _matrixarray[3].multi(m._matrixarray[2]).minus(_matrixarray[3].multi(m._matrixarray[0])); } protected matrix p5(matrix m) { return (_matrixarray[0].add(_matrixarray[3])).multi(m._matrixarray[0].add(m._matrixarray[3])); } protected matrix p6(matrix m) { return (_matrixarray[1].minus(_matrixarray[3])).multi(m._matrixarray[2].add(m._matrixarray[3])); } protected matrix p7(matrix m) { return (_matrixarray[0].minus(_matrixarray[2])).multi(m._matrixarray[0].add(m._matrixarray[1])); } public int get(int i, int j) { if (n == 1) { return element; } else { int size = n / 2; return this._matrixarray[(i / size) * 2 + (j / size)].get(i % size, j % size); } } public void display() { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { system.out.print(get(i, j)); system.out.print(" "); } system.out.println(); } } public static void main(string[] args) { matrix m = new matrix(2); matrix n = new matrix(2); m.set(0, 0, 1); m.set(0, 1, 3); m.set(1, 0, 5); m.set(1, 1, 7); n.set(0, 0, 8); n.set(0, 1, 4); n.set(1, 0, 6); n.set(1, 1, 2); matrix res = m.multi(n); res.display(); } }
总结
到此这篇关于使用java写的矩阵乘法的文章就介绍到这了,更多相关java矩阵乘法(strassen算法)内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 快速掌握和使用Flyway的详细教程