r ⋅ s = ∑ i = 1 s r = r + r + ⋯ + r ⏟ s times = ∑ j = 1 r s = s + s + ⋯ + s ⏟ r times r\cdot s=\sum _{i=1}^{s}r=\underbrace {r+r+\cdots +r} _{s{\text{ times}}}=\sum _{j=1}^{r}s=\underbrace {s+s+\cdots +s} _{r{\text{ times}}} r⋅s=i=1∑sr=s times r+r+⋯+r=j=1∑rs=r times s+s+⋯+s
4 * 3
>>> 12
.1 * .2
>>> 0.020000000000000004 #
[1, 2, 3] * 2
>>> [1, 2, 3, 1, 2, 3]
[1, 2, 3] * [1, 2, 3]
>>> TypeError: can't multiply sequence by non-int of type 'list' #
(1, 2, 3) * 4
>>> (1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3)
(1, 2, 3) * (1, 2, 3)
>>> TypeError: can't multiply sequence by non-int of type 'tuple'
np.array([1,2,3]) * 2
>>> array([2, 4, 6])
np.array([1,2,3]) * np.array([1,2,3])
>>> array([1, 4, 9])
np.multiply(np.array([1,2,3]), np.array([1,2,3]))
>>> array([1, 4, 9])
np.array([1,2,3]) * np.array([1,2,3,4])
>>> ValueError: operands could not be broadcast together with shapes (3,) (4,)
np.multiply(np.array([1,2,3]), np.array([1,2,3,4]))
>>> ValueError: operands could not be broadcast together with shapes (3,) (4,)
np.array([[1,2,3], [1,2,3]]) * np.array([2,3,4])
>>> array([[ 2, 6, 12],
[ 2, 6, 12]])
np.multiply(np.array([[1,2,3], [1,2,3]]), np.array([2,3,4]))
>>> array([[ 2, 6, 12],
[ 2, 6, 12]])
x = 3
y = 5
xNda = np.arange(x)
>>> array([0, 1, 2])
x2Nda = xNda*2+1
>>> array([1, 3, 5])
yNda = np.arange(1, y)
>>> array([1, 2, 3, 4])
xyNda = np.meshgrid(xNda, yNda)
>>> array([[[0, 1, 2],
[0, 1, 2],
[0, 1, 2],
[0, 1, 2]],
[[0, 0, 0],
[1, 1, 1],
[2, 2, 2],
[3, 3, 3]]])
np.dot(x, y)
>>> 15
np.dot(xNda, x)
>>> array([0, 3, 6])
np.dot(xNda+3, xNda+7) # 图示之
>>> 98
向量的点乘,也叫做向量的内积、数量积。对两个向量执行点乘运算,就是对着两个向量对应位置一一相乘之后求和的操作,点乘的结果是一个标量。
定义: 向量 a = [ a 1 , a 2 , . . . , a n ] a = [a_1, a_2, ..., a_n] a=[a1,a2,...,an] 和向量 b = [ b 1 , b 2 , . . . , b n ] b = [b_1, b_2, ..., b_n] b=[b1,b2,...,bn] 的内积为:
a ⋅ b = ∑ i = 1 n a i b i = a 1 b 1 + a 2 b 2 + . . . + a n b n a \cdot b = \sum_{i=1}^n a_i b_i = a_1 b_1 + a_2 b_2 + ... + a_n b_n a⋅b=∑i=1naibi=a1b1+a2b2+...+anbn
特别地, 0 ⋅ a = a ⋅ 0 = 0 0 \cdot a = a \cdot 0 = 0 0⋅a=a⋅0=0;若 a , b a, b a,b 是非零向量,则 a a a 与 b b b 正交的充要条件是 a ⋅ b = 0 a \cdot b = 0 a⋅b=0。
点积的几何意义: 可以用来表征或计算两个向量之间的夹角,以及在 b b b 向量在 a a a 向量方向上的投影,有公式:
a ⋅ b = ∣ a ∣ ∣ b ∣ cos θ a \cdot b = |a| |b| \cos \theta a⋅b=∣a∣∣b∣cosθ
其中, θ \theta θ 为 a a a 与 b b b 之间的夹角。
根据这个公式就可以计算向量 a a a 和向量 b b b 之间的夹角。从而就可以进一步判断这两个向量是否是同一方向,是否正交(也就是垂直)等方向关系,具体对应关系为:
向量点乘的运算特性:
…请等下集分解
以上内容来自维基百科, 由协助完成.