向量
import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sb %matplotlib inline # 向量 a = np.array([3,4]) plt.plot(0,0,'or') plt.arrow(0,0,*a,linewidth=1,head_width=0.2,head_length=0.2,color='b') plt.xlim(0,4) plt.ylim(0,5) plt.arrow(3,0,0,a[1],linewidth=1,head_width=0.1,head_length=0.1,color='r',linestyle='dotted') plt.arrow(0,0,a[0],0,linewidth=1,head_width=0.1,head_length=0.1,color='r',linestyle='dotted') plt.grid()
向量相加
a = np.array([1,2]) b = np.array([2,1]) c = a + b display(c) plt.plot(0,0,'or') plt.xlim(0,4) plt.ylim(0,4) plt.arrow(0,0,*a,linewidth=1,head_width=0.1,head_length=0.1,color='r') plt.arrow(a[0],a[1],*b,linewidth=1,head_width=0.1,head_length=0.1,color='r') plt.arrow(0,0,*c,linewidth=1,head_width=0.1,head_length=0.1,color='b',linestyle='dotted') plt.text(0.5,1.3,'$\overrightarrow{a}$',color='r',fontsize=15) plt.text(2.0,2.6,'$\overrightarrow{b}$',color='r',fontsize=15) plt.text(1.5,1.1,'$\overrightarrow{a}+\overrightarrow{b}$',color='b',fontsize=15) plt.grid()
标量乘向量
a = 3 b = np.array([2,3]) c = a*b plt.plot(0,0,'or') plt.arrow(0,0,*b,linewidth=1,head_width=0.2,head_length=0.2,color='r') plt.arrow(0,0,*c,linewidth=1,head_width=0.2,head_length=0.2,color='b',linestyle='dotted') plt.text(1,2,'$\overrightarrow{a}$') plt.text(5,8,'$3\overrightarrow{a}$') plt.xlim(0,8) plt.ylim(0,12) plt.grid()
线性无关&线性相关
a与b线性相关,a与c线性无关
a = np.array([1,2]) b = np.array([2,4]) c = np.array([1,4]) plt.plot(0,0,'or') plt.arrow(0,0,*a,linewidth=1,head_width=0.1,head_length=0.1,color='r') plt.arrow(1,1,*b,linewidth=1,head_width=0.1,head_length=0.1,color='g') plt.arrow(0.5,2,*c,linewidth=1,head_width=0.1,head_length=0.1,color='b') plt.text(0.5,1.2,'$\overrightarrow{a}$') plt.text(2,3.2,'$\overrightarrow{b}$') plt.text(1.0,4.5,'$\overrightarrow{c}$') plt.xlim(0,4) plt.ylim(0,7) plt.grid()
线性组合
2x - y = -1
0.1x - y = -6
# 计算向量张成 a = np.mat([[2,-1],[1/10,-1]]) b = np.mat([-1,-6]).T print (np.linalg.solve(a,b)) # 绘制线性组合图 x = np.arange(-10,10) y1 = 2*x+1 y2 = 0.1*x+6 plt.figure() plt.plot(x,y1) plt.plot(x,y2) plt.plot(2.63157895,6.26315789,'or')
矩阵相乘
v = np.array([[-1,2],[1,4]]) ij = np.array([[3, 1],[1, 2]]) v_t = np.matmul(ij,v) print (v_t)
矩阵求逆
A = np.mat([[1, -2], [1, -0.1]]) np.linalg.inv(A)
矩阵迹运算(对角线之和)
A = np.diag(np.arange(1,5)) print (A) np.trace(A)
转载请注明:思享SEO博客 » 线性代数:向量、矩阵数学计算