02.線性組合 張成空間和基

本節主要內容:基向量->線性無關->線性組合->張成空間->不同基向量下的張成空間.

1.基向量

我們一般使用vec i和vec j兩個向量構成2維平面的基向量.\vec i是x正方向的單位向量,vec j是y方向的單位向量.\ 在python中,\ 表示vec i可以用向量形式的[1,0]或者二維矩陣形式的[[1],[0]]\表示vec j可以用向量形式的[0,1]或者二維矩陣形式的[[0],[1]] \ 2*2矩陣,假如第一組數據是3vec i,第二組數據是2vec i+5vec j,可以寫作[[3,0],[2,5]].\ 對應數學中的 egin {bmatrix} 3 &2 \ 0&5 end {bmatrix}.\ 注意理解這段話,對於理解python線性代數非常重要!

但2維平面的基向量可以有很多種,只要滿足線性無關即可,但是線性無關是什麼呢?

2.線性無關的定義

註:線性無關是指結合中任意一個向量都不能用其他向量的線性組合表示.(線性組合指數乘或向量加減法,如 vec a + 5vec b

3.線性組合的和張成空間

線性組合的一種理解:可以看到固定一個,另一個數乘後終點仍在同一條線上.

張成空間是多個向量數乘和加法,以某一點為起點,所有可能的終點構成的空間.

4.不同情況下線性組合的張成空間 

(1)基向量不共線

基向量不共線,線性組合能表達可以組合平面內任意一點,注意灰色基向量斜率不變! 

(2)基向量共線

基向量共線,線性組合只能表達直線上的向量!

(3)兩個都是零向量

兩個都是零向量,只能乖乖呆在原點了!

(4)3D向量落可以處於一個平面

3D向量落可以處於一個平面,其張成空間也只能是這個平面!

(5)3D向量落不可處於一個平面

3D向量落不可處於一個平面,張成空間為整個空間!

下一篇地址:

袁傑雄:03.矩陣與線性變換(矩陣乘法)<python線性代數>?

zhuanlan.zhihu.com圖標

動畫代碼:

代碼附註1:

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 *zhfont = 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")def set_axis_middle(fig): ax = SubplotZero(fig, 111) fig.add_subplot(ax) for direction in ["xzero", "yzero"]: # adds arrows at the ends of each axis ax.axis[direction].set_axisline_style("-|>") # adds X and Y-axis from the origin ax.axis[direction].set_visible(True) for direction in ["left", "right", "bottom", "top"]: # hides borders ax.axis[direction].set_visible(False) ax.set_aspect(1.) #設置高寬比 ax.grid(True, zorder=0) ax.set_xlim(-2, 4) ax.set_ylim(-4, 2) 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 == "": 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): num = second U,V = 2, 3 rate = -1.5 ax = set_axis_middle(fig) draw_vector(ax,0,0,1,0,"r","i") draw_vector(ax,0,0,0,1,"r","j") U2,V2 = 3,-2 if second <= 1: pass elif second <= 2: draw_vector(ax,0,0,U2,V2,"y") elif second <= 3: draw_vector(ax,0,0,U2,V2,"y") UI,UJ = (second-2)*U2,(second-2)*V2 draw_vector(ax,0,0,UI,0,"g","3i") draw_vector(ax,0,0,0,UJ,"g","-2j") else: second = 3 draw_vector(ax,0,0,U2,V2,"y") UI,UJ = (second-2)*U2,(second-2)*V2 draw_vector(ax,0,0,UI,0,"g","3i") draw_vector(ax,0,0,0,UJ,"g","-2j") print (num) return mplfig_to_npimage(fig)if 1: animation = VideoClip(update, duration = 5) animation.write_gif("test.gif", fps=5)

代碼附註2:

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 *zhfont = 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")def set_axis_middle(fig): ax = SubplotZero(fig, 111) fig.add_subplot(ax) for direction in ["xzero", "yzero"]: # adds arrows at the ends of each axis ax.axis[direction].set_axisline_style("-|>") # adds X and Y-axis from the origin ax.axis[direction].set_visible(True) for direction in ["left", "right", "bottom", "top"]: # hides borders ax.axis[direction].set_visible(False) ax.set_aspect(1.) #設置高寬比 ax.grid(True, zorder=0) ax.set_xlim(-2, 8) ax.set_ylim(-2, 8) 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 == "": 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): num = second U,V = 1, 2 U2,V2 = 3,-1 ax = set_axis_middle(fig) if second <=1: draw_vector(ax,0,0,U,V ,"r","v","1.00") draw_vector(ax,U,V,U2,V2,"y","w","1.00") draw_vector(ax,0,0,round(U+U2,3),round(V+V2,3),"g") elif second <=3: plt.plot([U-10*U2,U+10*U2],[V-10*V2,V+10*V2]) if second <=2: rate2 = second-1 + 1 elif second <=3: rate2 = 2 -(second-2) U2,V2 = U2*rate2,V2*rate2 draw_vector(ax,0,0,U,V ,"r","v","1.00") draw_vector(ax,U,V,U2,V2,"y","w",str(round(rate2,2))) draw_vector(ax,0,0,round(U+U2,3),round(V+V2,3),"g") elif second <=5: plt.plot([U2-10*U,U2+10*U],[V2-10*V,V2+10*V]) if second <=4: rate1 = second-3 + 1 elif second <=5: rate1 = 2 -(second-4) U,V = U*rate1,V*rate1 draw_vector(ax,0,0,U,V ,"r","v",str(round(rate1,2))) draw_vector(ax,U,V,U2,V2,"y","w","1.00") draw_vector(ax,0,0,round(U+U2,3),round(V+V2,3),"g") print (num) return mplfig_to_npimage(fig)if 1: animation = VideoClip(update, duration = 5) animation.write_gif("test.gif", fps=5)

代碼附註3:

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")def set_axis_middle(fig): ax = SubplotZero(fig, 111) fig.add_subplot(ax) for direction in ["xzero", "yzero"]: # adds arrows at the ends of each axis ax.axis[direction].set_axisline_style("-|>") # adds X and Y-axis from the origin ax.axis[direction].set_visible(True) for direction in ["left", "right", "bottom", "top"]: # hides borders ax.axis[direction].set_visible(False) ax.set_aspect(1.) #設置高寬比 ax.grid(True, zorder=0) ax.set_xlim(-4, 4) ax.set_ylim(-4, 4) 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 == "": 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): num = second U,V = 1, 2 U2,V2 = -1,3 ax = set_axis_middle(fig) rate1,rate2 = sin(2*math.pi*second/5),cos(2*math.pi*second/5) U *= rate1 V *= rate1 U2 *= rate2 V2 *= rate2 draw_vector(ax,0,0,U,V ,"slategray","v",str(round(rate1,2))) draw_vector(ax,U,V,U2,V2,"slategray","w",str(round(rate2,2))) draw_vector(ax,0,0,round(U+U2,3),round(V+V2,3),"g") print (num) return mplfig_to_npimage(fig)if 1: animation = VideoClip(update, duration = 5) animation.write_gif("test.gif", fps=5)

推薦閱讀:

數據預處理中的數據編碼問題|python數據挖掘思考筆記(2)
使用 Python 會降低程序員的編程能力嗎?
這或許是對小白最友好的python入門了吧——14,遍歷字典
Python從零開始系列連載(26)——Python特色數據類型(函數)(上)
利用Requests+正則表達式爬取貓眼電影,學習全過程記錄與體會。

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