一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 使用java寫的矩陣乘法實例(Strassen算法)

使用java寫的矩陣乘法實例(Strassen算法)

2021-08-04 09:50Jack_Weng Java教程

這篇文章主要給大家介紹了關于如何使用java寫的矩陣乘法(Strassen算法)的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

Strassen算法于1969年由德國數學家Strassen提出,該方法引入七個中間變量,每個中間變量都只需要進行一次乘法運算。而樸素算法卻需要進行8次乘法運算。

原理

 

Strassen算法的原理如下所示,使用sympy驗證Strassen算法的正確性

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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算法)

 

代碼如下,可以看看數據結構的定義,時間換空間。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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算法)內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://blog.csdn.net/wj310298/article/details/44857175

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: fc2免费人成为视频 eeuss18影院www国产 | 网站色小妹 | 国内久久精品视频 | 四虎免费在线观看视频 | 手机跑分排行最新排名 | 日韩在线毛片 | 亚洲色域网 | 国产香蕉视频在线观看 | 岛国a香蕉片不卡在线观看 荡女淫春2古装 | 精品久久久久久久久久香蕉 | 日本三级欧美三级人妇英文 | 波多野结中文字幕在线69视频 | 亚洲国产精品无码中文字幕 | 精品国产理论在线观看不卡 | 国产日韩欧美在线一二三四 | 白俄罗斯bbbsss | 97久久精品午夜一区二区 | 欧美人在线一区二区三区 | 免费观看在线 | 东北恋哥在线播放免费播放 | 青青草在视线频久久 | 国产尤物精品视频 | 欧美国产日韩1区俺去了 | 草大逼 | 羞羞视频麻豆 | 男女性潮高片无遮挡禁18 | 60老妇性xxxxhd | aigao视频 | 国产二区三区 | xx18-19xxxxhd| 亚洲国产成人久久综合一 | 国产精品视频视频久久 | 精品无码久久久久久久久 | 俄罗斯美女尿尿 | 亚洲AV 日韩 国产 有码 | 9久re热视频这里只有精品 | 国产成人精品三级在线 | 欧美激情影音先锋 | 国产福利一区二区精品视频 | 精品在线99 | 14一15sexvideo日本|