带你一次搞懂点积(内积)、叉积(外积)

数量积与向量积

  • 1. 基本概念
    • (1)数量积
    • (2)向量积
    • (3)普通乘积
  • 2. 举例
  • 3. Numpy实现

1. 基本概念

设两向量 a ⃗ = ( x 1 , y 1 , z 1 )   b ⃗ = ( x 2 , y 2 , z 2 ) \vec{a}=\left( {{x}_{1}},{{y}_{1}}, {{z}_{1}} \right)~\vec{b}=\left( {{x}_{2}},{{y}_{2}} ,{{z}_{2}}\right) a =(x1,y1,z1) b =(x2,y2,z2),夹角为 θ \theta θ

(1)数量积

numpy中 使用np.dot

数量积又称内积(Kronecker product)、点积(Dot product),对应元素相乘相加,结果是一个标量(即一个数)。

a ⃗ ⋅ b ⃗ = x 1 x 2 + y 1 y 2 + z 1 z 2 \vec{a}\cdot \vec{b}= {{x}_{1}}{{x}_{2}}+{{y}_{1}}{{y}_{2}}+{{z}_{1}}{{z}_{2}} a b =x1x2+y1y2+z1z2有时也写成 a ⊗ b a\otimes b ab

几何意义是 a ⃗ \vec{a} a b ⃗ \vec{b} b 上的投影: a ⃗ ⋅ b ⃗ = ∣ a ⃗ ∣ ∗ ∣ b ⃗ ∣ ∗ c o s θ \vec{a}\cdot \vec{b}=\left| {\vec{a}} \right|*\left| {\vec{b}} \right|*cos\theta a b =a b cosθ

对于矩阵,假设 A A A m × s m \times s m×s阶矩阵, B B B s × n s \times n s×n阶矩阵(A的列数与B的行数相等),那么 C = A B C=AB C=AB(有时写成)是 m × n m\times n m×n阶矩阵,且矩阵 C C C中的元素满足
c i j = ∑ k = 1 s a i k b k j {{c}_{ij}}=\sum_{k=1}^{s}{{a}_{ik}}{{b}_{kj}} cij=k=1saikbkj

A = [ a 11 a 12 ⋯ a 1 s a 21 a 22 ⋯ a 2 s ⋯ ⋯ ⋱ ⋮ a m 1 a m 2 ⋯ a m s ]     B = [ b 11 b 12 ⋯ b 1 n b 21 b 22 ⋯ b 2 n ⋯ ⋯ ⋱ ⋮ b s 1 b s 2 ⋯ b s n ]     C = A B = [ c 11 c 12 ⋯ c 1 n c 21 c 22 ⋯ c 2 n ⋯ ⋯ ⋱ ⋮ c m 1 c m 2 ⋯ c m n ] A=\left[ \begin{matrix} {{a}_{11}} & {{a}_{12}} & \cdots & {{a}_{1s}} \\ {{a}_{21}} & {{a}_{22}} & \cdots & {{a}_{2s}} \\ \cdots & \cdots & \ddots & \vdots \\ {{a}_{m1}} & {{a}_{m2}} & \cdots & {{a}_{ms}} \\ \end{matrix} \right]~~~B=\left[ \begin{matrix} {{b}_{11}} & {{b}_{12}} & \cdots & {{b}_{1n}} \\ {{b}_{21}} & {{b}_{22}} & \cdots & {{b}_{2n}} \\ \cdots & \cdots & \ddots & \vdots \\ {{b}_{s1}} & {{b}_{s2}} & \cdots & {{b}_{sn}} \\ \end{matrix} \right]~~~C=AB=\left[ \begin{matrix} {{c}_{11}} & {{c}_{12}} & \cdots & {{c}_{1n}} \\ {{c}_{21}} & {{c}_{22}} & \cdots & {{c}_{2n}} \\ \cdots & \cdots & \ddots & \vdots \\ {{c}_{m1}} & {{c}_{m2}} & \cdots & {{c}_{mn}} \\ \end{matrix} \right] A=a11a21am1a12a22am2a1sa2sams   B=b11b21bs1b12b22bs2b1nb2nbsn   C=AB=c11c21cm1c12c22cm2c1nc2ncmn

(2)向量积

numpy中 使用np.cross

向量积又称外积、叉积(Cross product),结果 a ⃗ \vec{a} a b ⃗ \vec{b} b 的法向量,该向量垂直于 a ⃗ \vec{a} a b ⃗ \vec{b} b 构成的平面。

a ⃗ × b ⃗ = ∣ i j k x 1 y 1 z 1 x 2 y 2 z 2 ∣ = ( y 1 z 2 − y 2 z 1 ) i ⃗ − ( x 1 z 2 − x 2 z 1 ) j ⃗ + ( x 1 y 2 − x 2 y 1 ) k ⃗ \vec{a} \times \vec{b}= \begin{vmatrix}i & j & k \\ x_1 & y_1 & z_1 \\ x_2 & y_2 & z_2 \\ \end{vmatrix} = (y_1z_2-y_2z_1)\vec{i} - (x_1z_2-x_2z_1)\vec{j}+(x_1y_2-x_2y_1)\vec{k} a ×b =ix1x2jy1y2kz1z2=(y1z2y2z1)i (x1z2x2z1)j +(x1y2x2y1)k ∣ a ⃗ × b ⃗ ∣ = ∣ a ⃗ ∣ ∗ ∣ b ⃗ ∣ ∗ s i n θ |\vec{a} \times \vec{b}| =\left| {\vec{a}} \right|*\left| {\vec{b}} \right|*sin\theta a ×b =a b sinθ

(3)普通乘积

numpy中 使用np.multiply*

普通乘积:对应元素相乘,结果还是向量。 a ⃗ ∗ b ⃗ = { x 1 x 2 , y 1 y 2 , z 1 z 2 } \vec{a} * \vec{b}= \{{{x}_{1}{x}_{2}},{{y}_{1}}{{y}_{2}},{{z}_{1}}{{z}_{2}} \} a b ={x1x2,y1y2,z1z2}
有时写成 A ⊙ B A\odot B AB

2. 举例

X 1 = [ 1 2 3 4 ]          Y 1 = [ 5 6 7 8 ] ,          X 2 = [ 1 2 3 1 2 3 ]          Y 2 = [ 1 2 3 ]          X_1 = \begin{bmatrix}1 & 2 \\3 & 4\end{bmatrix}\;\;\;\; Y_1 = \begin{bmatrix}5 & 6 \\7 & 8\end{bmatrix}, \;\;\;\; X_2 = \begin{bmatrix}1 & 2 & 3\\1 & 2 & 3\end{bmatrix}\;\;\;\; Y_2 = \begin{bmatrix}1\\2\\3 \end{bmatrix} \;\;\;\; X1=[1324]Y1=[5768]X2=[112233]Y2=123

(1)数量积
X 1 Y 1 = [ 19 22 43 50 ] ,          X 2 Y 2 = [ 14 14 ] X_1Y_1 = \begin{bmatrix}19 & 22 \\43 & 50\end{bmatrix}, \;\;\;\; X_2Y_2 = \begin{bmatrix}14&14\end{bmatrix} X1Y1=[19432250]X2Y2=[1414]
(2)向量积
X 1 × Y 1 = [ − 4 − 4 ] ,          X 2 × Y 2 = [ 0 0 0 0 0 0 ] X_1 \times Y_1 = \begin{bmatrix}-4 & -4 \end{bmatrix}, \;\;\;\; X_2 \times Y_2 = \begin{bmatrix}0&0&0 \\0&0&0 \end{bmatrix} X1×Y1=[44]X2×Y2=[000000]
(3)普通乘积
X 1 ∗ Y 1 = [ 5 12 21 32 ] ,          X 2 ∗ Y 2 = [ 1 4 9 1 4 9 ] X_1*Y_1 = \begin{bmatrix}5 & 12 \\21 & 32\end{bmatrix}, \;\;\;\; X_2*Y_2 = \begin{bmatrix}1&4&9 \\1&4&9 \end{bmatrix} X1Y1=[5211232]X2Y2=[114499]

3. Numpy实现

import numpy as np 
X1 = np.array([[1,2],[3,4]])
Y1 = np.array([[5,6],[7,8]])
X2 = np.array([[1,2,3],[1,2,3]])
Y2 = np.array([1,2,3])
## 数量积  使用np.dot
print(np.dot(X1,Y1))
print(np.dot(X2,Y2))
## 向量积  使用np.cross
print(np.cross(X1,Y1))
print(np.cross(X2,Y2))
## 普通乘积  使用np.multiply
print(np.multiply(X1,Y1)) #或者直接使用X1*Y1
print(np.multiply(X2,Y2)) #或者直接使用X2*Y2

你可能感兴趣的:(Python数据分析)