問程式設計客棧題描述
分別實現矩陣相乘的3種演算法,比較三種演算法在矩陣大小分別為22∗2222∗22, 23∗2323∗23, 24∗2424∗24, 25∗2525∗25, 26∗2626∗26, 27∗2727∗27, 28∗2828∗28, 29∗2929∗29時的執行時間與matlab自帶的矩陣相乘的執行時間,繪製時間對比圖。
解題方法
本文採用了以下方法進行求值:矩陣計算法、定義法、分治法和strassen方法。這裡我們使用matlab以及python對這個問題進行處理,比較兩種語言在一樣的條件下,運算速度的差別。
程式語言
python
具體**
#-*- coding: utf-8 -*-
from matplotlib.font_manager import fontproperties
import numpy as np
import time
import random
import math
import copy
import matplotlib.pyplot as plt
#n = [2**2, 2**3, 2**4, 2**5, 2**6, 2**7, 2**8, 2**9, 2**10, 2**11, 2**12]
n = [2**2, 2**3, 2**4, 2**5, 2**6, 2**7, 2**8, 2**9, 2**10, 2**11]
sum_time1 =
sum_time2 =
sum_time3 =
sum_time4 =
for m in n:
a = np.random.randint(0, 2, [m, m])
b = np.random.randint(0, 2, [m, m])
a1 = np.mat(a)
b1 = np.mat(b)
time_start = time.time()
c1 = a1*b1
time_end = time.time()
sum_time1.append(time_end - time_start)
c2 = np.zeros([m, m], dtype = np.int)
time_start = time.time()
for i in range(m):
for k in range(m):
for j in range(m):
c2[i, j] = c2[i, j] + a[i, k] * b[k, j]
time_end = time.time()
sum_time2.append(time_end - time_start)
a11 = np.mat(a[0:m//2, 0:m//2])
a12 = np.mat(a[0:m//2, m//2:m])
a21 = np.mat(a[m//2:m, 0:m//2])
a22 = np.mat(a[m//2:m, m//2:m])
b11 = np.mat(b[0:m//2, 0:m//2])
b12 = np.mat(b[0:m//2, m//2:m])
b21 = np.mat(b[m//2:m, 0:m//2])
b22 = np.mat(b[m//2:m, m//2:m])
time_start = time.time()
c11 = a11 * b11 + a12 * b21
c12 = a11 * b12 + a12 * b22
c21 = a21 * b11 + a22 * b21
c22 = a21 * b12 + a22 * b22
c3 = np.vstack((np.hstack((c11, c12)), np.hstack((c21, c22))程式設計客棧))
time_end = time.time()
sum_time3.append(time_end - time_start)
time_start = time.time()
m1 = a11 * (b12 - b22)
m2 = (a11 + a12) * b22
m3 = (a21 + a22) * b11
m4 = a22 * (b21 - b11)
m5 = (a11 + a22) * (b11 + b22)
m6 = (a12 - a22) * (b21 + b22)
m7 = (a11 - a21) * (b11 + b12)
c11 = m5 + m4 - m2 + m6
c12 = m1 + m2
c21 = m3 + m4
c22 = m5 + m1 - m3 - m7
c4 = np.vstack((np.hstack((c11, c12)), np.hstack((c21, c22))))
time_end = time.time()
sum_time4.append(time_end - time_start)
f1 = open('python_time1.txt', 'w')
for ele in sum_time1:
f1.writelines(str(ele) + '\n')
f1.close()
f2 = open('python_time2.txt', 'w')
for ele in sum_time2:
f2.writelines(str(ele) + '\n')
f2.close()
f3 = open('python_time3.txt', 'w')
for ele in sum_time3:
f3.writelines(str(ele) + '\n')
f3.close()
f4owtiqiiz = open('python_time4.txt', 'w')
for ele 程式設計客棧in sum_time4:
f4.writelines(str(ele) + '\n')
f4.close()
font = fontproperties(fname=r"c:\windows\fonts\simsun.ttc", size=8)
plt.figure(1)
plt.subplot(221)
plt.semilogx(n, sum_time1, 'r-*')
plt.ylabel(u"時間(s)", fontproperties=font)
plt.xlabel(u"矩陣的維度n", fontproperties=font)
plt.title(u'python自帶的方法', fontproperties=font)
plt.subplot(222)
plt.semilogx(n, sum_time2, 'b-*')
plt.ylabel(u"時間(s)", fontproperties=font)
plt.xlabel(u"矩陣的維度n", fontproperties=font)
plt.title(u'定義法', fontproperties=font)
plt.subplot(223)
plt.semilogx(n, sum_time3, 'y-*')
plt.ylabel(u"時間(s)", fontproperties=font)
plt.xlabel(u"矩陣的維度n", fontproperties=font)
plt.程式設計客棧title( u'分治法', fontproperties=font)
plt.subplot(224)
plt.semilogx(n, sum_time4, 'g-*')
plt.ylabel(u"時間(s)", fontproperties=font)
plt.xlabel(u"矩陣的維度n", fontproperties=font)
plt.title( u'strasses法', fontproperties=font)
plt.figure(2)
plt.semilogx(n, sum_time1, 'r-*', n, sum_time2, 'b-+', n, sum_time3, 'y-o', n, sum_time4, 'g-^')
#plt.legend(u'python自帶的方法', u'定義法', u'分治法', u'strasses法', fontproperties=font)
plt.show()
本文標題: python實現矩陣相乘的三種方法小結
本文位址:
矩陣相乘的實現 python
import numpy as np def matrix multi m1,m2 首先建立乙個值都是0的矩陣,矩陣形狀是矩陣1的行數和矩陣2的列數組成 results np.zeros m1.shape 0 m2.shape 1 判斷矩陣1的列和矩陣2的行數是否相同,如果不相同,則兩個矩陣無法相乘...
陣列之三種矩陣
矩陣的壓縮 對於某些特殊的矩陣來說,非零元素較少,大部分元素為0,採用某種演算法,將非零元素儲存在一位陣列裡以達到節省儲存空間的目的的過程,稱為矩陣的壓縮 矩陣的還原 將壓縮後的陣列還原成原始矩陣的過程 所謂對角矩陣 矩陣中的所有非零元素都集中在以主對角線為中心的帶狀區域中,即除了主對角線上和直接在...
Python 回文數的三種實現方法
題目 找出五位數中的回文數,列印並計算個數。思路 回文數是對稱數,指正向讀與反向讀是相同的,如12321,33433等。因此可以利用正向與反向相同或對稱位數字相同來判斷。解法1 利用字串反轉,判斷反轉前後是否相等 count 0for num in range 10000 99999 ifstr n...