向量的基本操作
向量的基本操作包括:向量相加,向量乘以标量。
- 标量
标量(scalar):一个标量就是一个单独的数,我们在使用标量时,一般都要明确给出它是那种类型的数【1】。
* a,其中 a 。
从图像上说,向量乘以标量,是使得向量拉伸或者收缩了。例如,如果a>1
,就是将向量拉伸了a
倍;如果0<a<1
,就是使得向量收缩到了原来的1/a
。这一拉伸或者收缩的过程用英语描述就是scale
,因此称这个实数a
称为scalar
。
实数域公理
- 元素0
在实数域 中,它是由元素0组成的向量:= - 元素1
在实数域 中,它是由元素1组成的向量:=
上述运算遵守实数域公理:
- 结合律
- 交换律
- 分配律
- 单元性(定义加上零和乘以一)
- 逆元(定义加减逆元和乘除逆元)
python代码演示
绘制二维向量
# Import NumPy and Matplotlib
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
# Define vector v
v = np.array([1,1])
# Plots vector v as blue arrow with red dot at origin (0,0) using Matplotlib
# Creates axes of plot referenced 'ax'
ax = plt.axes()
# Plots red dot at origin (0,0)
ax.plot(0,0,'or')
# Plots vector v as blue arrow starting at origin 0,0
ax.arrow(0, 0, *v, color='b', linewidth=2.0, head_width=0.20, head_length=0.25)
# Sets limit for plot for x-axis
plt.xlim(-2,2)
# Set major ticks for x-axis
major_xticks = np.arange(-2, 3)
ax.set_xticks(major_xticks)
# Sets limit for plot for y-axis
plt.ylim(-1, 2)
# Set major ticks for y-axis
major_yticks = np.arange(-1, 3)
ax.set_yticks(major_yticks)
# Creates gridlines for only major tick marks
plt.grid(b=True, which='major')
# Displays final plot
plt.show()
将二维向量与标量相乘并绘制结果
# Define vector v
v = np.array([1,1])
# Define scalar a
a = 3
# TODO 1.: Define vector av - as vector v multiplied by scalar a
av = v * a
# Plots vector v as blue arrow with red dot at origin (0,0) using Matplotlib
# Creates axes of plot referenced 'ax'
ax = plt.axes()
# Plots red dot at origin (0,0)
ax.plot(0,0,'or')
# Plots vector v as blue arrow starting at origin 0,0
ax.arrow(0, 0, *v, color='b', linewidth=2.5, head_width=0.30, head_length=0.35)
# TODO 2.: Plot vector av as dotted (linestyle='dotted') vector of cyan color (color='c')
# using ax.arrow() statement above as template for the plot
ax.arrow(0, 0, *av, color='c', linestyle='dotted', linewidth=2.5, head_width=0.30, head_length=0.35)
# Sets limit for plot for x-axis
plt.xlim(-2, 4)
# Set major ticks for x-axis
major_xticks = np.arange(-2, 4)
ax.set_xticks(major_xticks)
# Sets limit for plot for y-axis
plt.ylim(-1, 4)
# Set major ticks for y-axis
major_yticks = np.arange(-1, 4)
ax.set_yticks(major_yticks)
# Creates gridlines for only major tick marks
plt.grid(b=True, which='major')
# Displays final plot
plt.show()
将两个向量相加并绘制结果
# Define vector v
v = np.array([1,1])
# Define vector w
w = np.array([-2,2])
# TODO 1.: Define vector vw by adding vectors v and w
vw = None
# Plot that graphically shows vector vw (color='b') - which is the result of
# adding vector w(dotted cyan arrow) to vector v(blue arrow) using Matplotlib
# Creates axes of plot referenced 'ax'
ax = plt.axes()
# Plots red dot at origin (0,0)
ax.plot(0,0,'or')
# Plots vector v as blue arrow starting at origin 0,0
ax.arrow(0, 0, *v, color='b', linewidth=2.5, head_width=0.30, head_length=0.35)
# Plots vector w as cyan arrow with origin defined by vector v
ax.arrow(v[0], v[1], *w, linestyle='dotted', color='c', linewidth=2.5,
head_width=0.30, head_length=0.35)
# TODO 2.: Plot vector vw as black arrow (color='k') with 3.5 linewidth (linewidth=3.5)
# starting vector v's origin (0,0)
vw = v + w
ax.arrow(0, 0, *vw, linestyle='dotted', color='k', linewidth=3.5,
head_width=0.30, head_length=0.35)
# Sets limit for plot for x-axis
plt.xlim(-3, 2)
# Set major ticks for x-axis
major_xticks = np.arange(-3, 2)
ax.set_xticks(major_xticks)
# Sets limit for plot for y-axis
plt.ylim(-1, 4)
# Set major ticks for y-axis
major_yticks = np.arange(-1, 4)
ax.set_yticks(major_yticks)
# Creates gridlines for only major tick marks
plt.grid(b=True, which='major')
# Displays final plot
plt.show()
网友评论