04.複合線性變換

本文主要內容:驗證並可視化體驗,複合線性變換(矩陣乘法)不符合交換率,但符合結合率.

1.複合矩陣乘法不符合交換律

M1M2 
e M2M1

證明:

2.複合矩陣乘法符合結合律

3.三維複合矩陣乘法也不符合交換律,符合結合率

4.複合線性變換例子

這裡M1設為[[0,-1],[1,0]],這裡M2設為[[1,1],[0,1]].

下面兩張圖紅色向量結果不同,證明 M2M1vec v 
eq M1M2vec v ,不符合交換律.

注意 M2M1vec v 直觀理解是 M2(M1vec v) ,其次因其符合結合律,可以認為是 (M2M1)vec v ,從上面sympy演算法推導我們已經知道(M2M1) 
e (M1M2),這裡通過實例進行可視化演示.

圖1:如下為 M_2M_1 vec v (紅色向量為最終結果),結果為[[-3],[-1]].

圖2:如下為 M_1M2_vec v (紅色向量為最終結果),結果為[[-2],[1]],顯然 M_1M2_vec v 
e M_2M1_vec v ,不符合交換率.

下一節內容見下:

袁傑雄:05.行列式[python線性代數]?

zhuanlan.zhihu.com圖標

附註代碼:

import numpy as npimport matplotlib.pyplot as pltfrom mpl_toolkits.axisartist.axislines import SubplotZerofrom moviepy.editor import VideoClipfrom moviepy.video.io.bindings import mplfig_to_npimagefrom pylab import *import mathzhfont = mpl.font_manager.FontProperties(fname=/usr/share/fonts/truetype/wqy/wqy-microhei.ttc,size=13)plt.rcParams[axes.unicode_minus]=False #負號顯示解決方案plt.style.use("seaborn")X_MIN,X_MAX = -4,3Y_MIN,Y_MAX = -1.5,3def set_axis_middle(fig): ax = SubplotZero(fig, 111) fig.add_subplot(ax) ax.set_aspect(1.) #設置高寬比 ax.set_xlim(X_MIN,X_MAX) ax.set_ylim(Y_MIN,Y_MAX) return axfig = plt.figure()def draw_vector(ax,x,y,u,v,c,s="",s2=""): ax.quiver(x,y, u,v,angles=xy, scale_units=xy, scale=1,color = c) if s == "": try : u = u[0] v = v[0] except: pass plt.text(x+u/2,y+v/2,r"$[^{"+str(u)+"}_{"+str(v)+"}]$", fontproperties=zhfont) else: plt.text(x+u/2,y+v/2,s2 + r"$vec{"+s+"}$", fontproperties=zhfont)def update(second): static = False if second>5: rate = 2 if second>6: static = True elif 1<=second<=3: rate =second-1 elif 3<second<=5: rate =second-3 elif second<1: rate = 0 ax = set_axis_middle(fig) i = np.array([[1],[0]]) j = np.array([[0],[1]]) UV = np.array([[-1],[2]]) liner_transform2 = np.array([[0,-1], [1,0]]) liner_transform = np.array([[1,1], [0,1]]) if second<=3: cur_transform = np.array([[1,0],[0,1]]) + (liner_transform-np.array([[1,0],[0,1]]))/2*rate else: cur_transform = liner_transform + (np.dot(liner_transform2, liner_transform)-liner_transform)/2*rate#注意這裡時np.dot(liner_transform2,liner_transform),因為正常是M2乘以M1乘以V i = np.dot(cur_transform,i) j = np.dot(cur_transform,j) draw_vector(ax,0,0,UV[0]*i[0],UV[0]*i[1] ,"g","i",str(UV[0][0])) draw_vector(ax,UV[0]*i[0],UV[0]*i[1] ,UV[1]*j[0],UV[1]*j[1] ,"y","j",str(UV[1][0])) draw_vector(ax,0,0,i[0],i[1] ,"g","i" if not static else "") draw_vector(ax,0,0,j[0],j[1] ,"y","j" if not static else "") UV = np.dot(cur_transform,UV) draw_vector(ax,0,0,UV[0],UV[1] ,"r","v" if not static else "") return mplfig_to_npimage(fig)if 1: animation = VideoClip(update, duration = 8) animation.write_gif("test.gif", fps=7)

附註代碼2:

from sympy import *init_printing(use_unicode=True)a,b,c,d,e,f,g,h = symbols("a,b,c,d,e,f,g,h")M1=Matrix([[a, b], [c, d]])M2=Matrix([[e, f], [g, h]])M1,M2 #printM3 = M1*M2#交換律M4 = M2*M1M3,M4 #printfor i in (M3-M4): assert i.simplify() == 0 print (i)

附註代碼3:

from sympy import *init_printing(use_unicode=True)a,b,c,d,e,f,g,h,x,y = symbols("a,b,c,d,e,f,g,h,x,y")V = Matrix([[x], [y]])M1=Matrix([[a, b], [c, d]])M2=Matrix([[e, f], [g, h]])M3 = (M2*M1)*VM4 = M2*(M1*V) #結合律M1,M2,M3,M4for i in (M3-M4): assert i.simplify() == 0 print (i)

附註代碼4:

from sympy import *init_printing(use_unicode=True)a1,b1,c1,d1,e1,f1,g1,h1,i1 = symbols("a1,b1,c1,d1,e1,f1,g1,h1,i1")a2,b2,c2,d2,e2,f2,g2,h2,i2 = symbols("a2,b2,c2,d2,e2,f2,g2,h2,i2")x,y,z= symbols("x,y,z")M1=Matrix([[a1, b1,c1], [d1, e1,f1], [g1, h1,i1]])M2=Matrix([[a2, b2,c2], [d2, e2,f2], [g2, h2,i2]])V = Matrix([[x],[y],[z]])M1,M2 #printM3 = M1*M2#交換律M4 = M2*M1for i in (M3-M4): assert i.simplify()!= 0M3 = (M2*M1)*V#結合律M4 = M2*(M1*V)for i in (M3-M4): assert i.simplify()== 0

推薦閱讀:

【實戰案例+代碼】梯度下降法求解線性回歸的python實現及其結果可視化(一)
詩豪:劉禹錫寫詩用字分析(唐代詩人寫詩用字分析之三)
【R可視化】將薪比薪
2018年全球互聯網發展數據分析
06.大雜燴 線性方程組 逆矩陣 列空間 零空間 非方陣

TAG:Python | 線性代數 | 可視化 |