机器学习之常用矩阵/向量运算

1. Matrix product (矩阵乘法)

定义: 给定 m × p m\times p m×p大小的矩阵 A A A p × n p\times n p×n大小的矩阵 B B B,则矩阵 A A A B B B的乘积为 m × n m\times n m×n大小的矩阵 C C C,计算过程如下,

C = A ⋅ B C=A\cdot B C=AB

其中, C i , j = ∑ k = 1 p A i , k ⋅ B k , j C_{i,j}=\sum_{k=1}^{p}{A_{i,k}\cdot B_{k,j}} Ci,j=k=1pAi,kBk,j 1 ≤ i ≤ m , 1 ≤ j ≤ n 1\leq i\leq m,1\leq j\leq n 1im,1jn

举例: 给定 2 × 3 2\times 3 2×3大小的矩阵 A = [ 0 1 2 3 4 5 ] A=\begin{bmatrix}0 & 1 & 2\\ 3 & 4 & 5\end{bmatrix} A=[031425] 3 × 2 3\times 2 3×2大小的矩阵 B = [ 0 3 1 4 2 5 ] B=\begin{bmatrix}0 & 3\\ 1 & 4\\ 2 & 5\end{bmatrix} B=012345,则矩阵 A A A B B B的乘积为 2 × 2 2×2 2×2大小的矩阵 C C C,计算过程如下,

C = A ⋅ B C=A\cdot B C=AB

= [ 0 1 2 3 4 5 ] ⋅ [ 0 3 1 4 2 5 ] =\begin{bmatrix} 0 & 1 & 2\\ 3 & 4 & 5\end{bmatrix}\cdot \begin{bmatrix}0 & 3\\ 1 & 4\\ 2 & 5\end{bmatrix} =[031425]012345

= [ 0 × 0 + 1 × 1 + 2 × 2 0 × 3 + 1 × 4 + 2 × 5 3 × 0 + 4 × 1 + 5 × 2 3 × 3 + 4 × 4 + 5 × 5 ] =\begin{bmatrix}0\times0+1\times1+2\times2 & 0\times3+1\times4+2\times5\\ 3\times0+4\times1+5\times2 & 3\times3+4\times4+5\times5\end{bmatrix} =[0×0+1×1+2×23×0+4×1+5×20×3+1×4+2×53×3+4×4+5×5]

= [ 5 14 14 50 ] =\begin{bmatrix}5 & 14\\ 14 & 50\end{bmatrix} =[5141450]



2. Hadamard product (哈达马积)

定义: 给定 m × n m\times n m×n大小的矩阵 A A A m × n m\times n m×n大小的矩阵 B B B,则矩阵 A A A B B B的Hadamard product为 m × n m\times n m×n大小的矩阵 C C C,计算过程如下,

C = A ⨀ B C=A\bigodot B C=AB

其中, C i , j = A i , j ⋅ B i , j C_{i,j}=A_{i,j}\cdot B_{i,j} Ci,j=Ai,jBi,j 1 ≤ i ≤ m , 1 ≤ j ≤ n 1\leq i\leq m,1\leq j\leq n 1im,1jn

举例: 给定 2 × 3 2\times 3 2×3大小的矩阵 A = [ 0 1 2 3 4 5 ] A=\begin{bmatrix}0 & 1 & 2\\ 3 & 4 & 5\end{bmatrix} A=[031425] 3 × 2 3\times 2 3×2大小的矩阵 B = [ 3 4 5 0 1 2 ] B=\begin{bmatrix}3 & 4 & 5\\ 0 & 1 & 2\end{bmatrix} B=[304152],则矩阵 A A A B B B的乘积为 3 × 2 3×2 3×2大小的矩阵 C C C,计算过程如下,

C = A ⨀ B C=A\bigodot B C=AB

= [ 0 1 2 3 4 5 ] ⨀ [ 3 4 5 0 1 2 ] =\begin{bmatrix}0 & 1 & 2\\ 3 & 4 & 5\end{bmatrix}\bigodot \begin{bmatrix}3 & 4 & 5\\ 0 & 1 & 2\end{bmatrix} =[031425][304152]

= [ 0 × 3 1 × 4 2 × 5 3 × 0 4 × 1 5 × 2 ] =\begin{bmatrix}0 \times 3 & 1\times 4 & 2 \times 5\\ 3\times 0 & 4\times 1 & 5 \times 2\end{bmatrix} =[0×33×01×44×12×55×2]

= [ 0 4 10 0 4 10 ] =\begin{bmatrix}0 & 4 & 10\\ 0 & 4 & 10\end{bmatrix} =[00441010]



3. Dot product (点积)

定义: 给定 1 × n 1\times n 1×n大小的向量 a ⃗ \vec{a} a 1 × n 1\times n 1×n大小的向量 b ⃗ \vec{b} b , 则向量 a ⃗ \vec{a} a b ⃗ \vec{b} b 的点积为 1 × 1 1\times 1 1×1大小的数值 c c c, 计算过程如下,

c = a ⃗ ∙ b ⃗ c=\vec{a}\bullet \vec{b} c=a b

= ∑ i = 1 n a i b i =\sum_{i=1}^{n}{a_ib_i} =i=1naibi

举例: 给定 1 × 3 1\times 3 1×3大小的向量 a ⃗ = ( 0 1 2 ) \vec{a}=\begin{pmatrix}0 & 1 & 2\end{pmatrix} a =(012) 1 × 3 1\times 3 1×3大小的向量 b ⃗ = ( 3 4 5 ) \vec{b}=\begin{pmatrix}3 & 4 & 5\end{pmatrix} b =(345),则向量 a ⃗ \vec{a} a b ⃗ \vec{b} b 的点积为数值 c c c,计算过程如下,

c = a ⃗ ∙ b ⃗ c=\vec{a}\bullet \vec{b} c=a b

= 0 × 3 + 1 × 4 + 2 × 5 =0\times 3 + 1 \times 4 + 2 \times 5 =0×3+1×4+2×5

= 4 + 10 = 4 + 10 =4+10

= 14 =14 =14



4. Cross product (叉积)

定义: 叉积是依赖于欧几里德三维空间的一个度量单位。给定三维空间中 1 × 3 1\times 3 1×3大小的向量 a ⃗ \vec{a} a 1 × 3 1\times 3 1×3大小的向量 b ⃗ \vec{b} b ,则向量 a ⃗ \vec{a} a b ⃗ \vec{b} b 的叉积为 1 × 3 1\times 3 1×3大小的向量 c ⃗ \vec{c} c ,计算过程如下,

c ⃗ = a ⃗ × b ⃗ \vec{c}=\vec{a}\times \vec{b} c =a ×b

= ∥ a ∥ ∥ b ∥ s i n ( θ ) ζ = \begin{Vmatrix}a \end{Vmatrix}\begin{Vmatrix}b \end{Vmatrix}sin(\theta) \zeta =absin(θ)ζ

其中, θ \theta θ为向量 a a a和向量 b b b之间的夹角, ζ \zeta ζ为叉积 c ⃗ \vec{c} c 的方向,即右手定则下,向量 a ⃗ \vec{a} a 和向量 b ⃗ \vec{b} b 之间法线的方向。

衍生: 根据叉积的定义,给定三维坐标, y y y z z z的基坐标分别为 i ⃗ \vec{i} i j ⃗ \vec{j} j k ⃗ \vec{k} k ,根据右手定则有,

i ⃗ × j ⃗ = k ⃗ \vec{i}\times \vec{j}=\vec{k} i ×j =k

j ⃗ × k ⃗ = i ⃗ \vec{j}\times \vec{k}=\vec{i} j ×k =i

k ⃗ × i ⃗ = j ⃗ \vec{k}\times \vec{i}=\vec{j} k ×i =j

j ⃗ × i ⃗ = − k ⃗ \vec{j}\times \vec{i}=-\vec{k} j ×i =k

k ⃗ × j ⃗ = − i ⃗ \vec{k}\times \vec{j}=-\vec{i} k ×j =i

i ⃗ × k ⃗ = − j ⃗ \vec{i}\times \vec{k}=-\vec{j} i ×k =j

其中,当 θ = 0 \theta =0 θ=0时候,有,

i ⃗ × i ⃗ = 0 ⃗ \vec{i}\times \vec{i}=\vec{0} i ×i =0

j ⃗ × j ⃗ = 0 ⃗ \vec{j}\times \vec{j}= \vec{0} j ×j =0

k ⃗ × k ⃗ = 0 ⃗ \vec{k}\times \vec{k}= \vec{0} k ×k =0

举例一 (向量形式): 给定三维空间中的两个向量 u ⃗ \vec{u} u v ⃗ \vec{v} v ,令 u ⃗ = u 1 i ⃗ + u 2 j ⃗ + u 3 k ⃗ \vec{u} = u_1\vec{i} + u_2\vec{j} + u_3\vec{k} u =u1i +u2j +u3k v ⃗ = v 1 i ⃗ + v 2 j ⃗ + v 3 k ⃗ \vec{v} = v_1\vec{i} + v_2\vec{j} + v_3\vec{k} v =v1i +v2j +v3k ,根据定义,有,

u ⃗ × v ⃗ = ( u 1 i ⃗ + u 2 j ⃗ + u 3 k ⃗ ) × ( v 1 i ⃗ + v 2 j ⃗ + v 3 k ⃗ ) \vec{u} \times \vec{v} = \left ( u_1\vec{i} + u_2\vec{j} + u_3\vec{k} \right ) \times \left ( v_1\vec{i} + v_2\vec{j} + v_3\vec{k} \right ) u ×v =(u1i +u2j +u3k )×(v1i +v2j +v3k )

= u 1 v 1 i ⃗ × i ⃗ + u 1 v 2 i ⃗ × j ⃗ + u 1 v 3 i ⃗ × k ⃗ + u 2 v 1 j ⃗ × i ⃗ + u 2 v 2 j ⃗ × j ⃗ + u 2 v 3 j ⃗ × k ⃗ + u 3 v 1 k ⃗ × i ⃗ + u 3 v 2 k ⃗ × j ⃗ + u 3 v 3 k ⃗ × k ⃗ = u_1v_1\vec{i}\times \vec{i} + u_1v_2\vec{i}\times \vec{j} + u_1v_3\vec{i}\times \vec{k} + u_2v_1\vec{j}\times \vec{i} + u_2v_2\vec{j}\times \vec{j} + u_2v_3\vec{j}\times \vec{k} + u_3v_1\vec{k}\times \vec{i} + u_3v_2\vec{k}\times \vec{j} + u_3v_3\vec{k}\times \vec{k} =u1v1i ×i +u1v2i ×j +u1v3i ×k +u2v1j ×i +u2v2j ×j +u2v3j ×k +u3v1k ×i +u3v2k ×j +u3v3k ×k

= 0 ⃗ + u 1 v 2 k ⃗ − u 1 v 3 j ⃗ − u 2 v 1 k ⃗ + 0 ⃗ + u 2 v 3 i ⃗ + u 3 v 1 j ⃗ − u 3 v 2 j ⃗ − u 3 v 2 i ⃗ + 0 ⃗ = \vec{0} + u_1v_2\vec{k} - u_1v_3\vec{j} - u_2v_1\vec{k} + \vec{0} + u_2v_3\vec{i} + u_3v_1\vec{j} - u_3v_2\vec{j} - u_3v_2\vec{i} + \vec{0} =0 +u1v2k u1v3j u2v1k +0 +u2v3i +u3v1j u3v2j u3v2i +0

= u 1 v 2 k ⃗ − u 1 v 3 j ⃗ − u 2 v 1 k ⃗ + u 2 v 3 i ⃗ + u 3 v 1 j ⃗ − u 3 v 2 i ⃗ = u_1v_2\vec{k} - u_1v_3\vec{j} - u_2v_1\vec{k} + u_2v_3\vec{i} + u_3v_1\vec{j} - u_3v_2\vec{i} =u1v2k u1v3j u2v1k +u2v3i +u3v1j u3v2i

= ( u 2 v 3 − u 3 v 2 ) i ⃗ + ( u 3 v 1 − u 1 v 3 ) j ⃗ + ( u 1 v 2 − u 2 v 1 ) k ⃗ = \left ( u_2v_3 - u_3v_2 \right )\vec{i} + \left ( u_3v_1 - u_1v_3 \right )\vec{j} + \left ( u_1v_2 - u_2v_1 \right )\vec{k} =(u2v3u3v2)i +(u3v1u1v3)j +(u1v2u2v1)k

举例二 (行列式形式): 给定三维空间中的两个向量 u ⃗ \vec{u} u v ⃗ \vec{v} v ,令 u ⃗ = u 1 i ⃗ + u 2 j ⃗ + u 3 k ⃗ \vec{u} = u_1\vec{i} + u_2\vec{j} + u_3\vec{k} u =u1i +u2j +u3k v ⃗ = v 1 i ⃗ + v 2 j ⃗ + v 3 k ⃗ \vec{v} = v_1\vec{i} + v_2\vec{j} + v_3\vec{k} v =v1i +v2j +v3k ,差积也可以通过构造矩阵,计算行列式而得,如下,

u ⃗ × v ⃗ = ∣ i ⃗ j ⃗ k ⃗ u 1 u 2 u 3 v 1 v 2 v 3 ∣ \vec{u} \times \vec{v} = \begin{vmatrix} \vec{i} & \vec{j} & \vec{k}\\ u_1 & u_2 & u_3\\ v_1 & v_2 & v_3 \end{vmatrix} u ×v =i u1v1j u2v2k u3v3

高阶方阵的行列式可以通过根据拉普拉斯定理降维计算而得,如下,

u ⃗ × v ⃗ = ∣ u 2 u 3 v 2 v 3 ∣ i ⃗ − ∣ u 1 u 3 v 1 v 3 ∣ j ⃗ + ∣ u 1 u 2 v 1 v 2 ∣ k ⃗ \vec{u} \times \vec{v} = \begin{vmatrix}u_2 & u_3\\ v_2 & v_3\end{vmatrix}\vec{i} - \begin{vmatrix}u_1 & u_3\\ v_1 & v_3\end{vmatrix}\vec{j} + \begin{vmatrix}u_1 & u_2\\ v_1 & v_2\end{vmatrix}\vec{k} u ×v =u2v2u3v3i u1v1u3v3j +u1v1u2v2k

= ( u 2 v 3 − u 3 v 2 ) i ⃗ + ( u 3 v 1 − u 1 v 3 ) j ⃗ + ( u 1 v 2 − u 2 v 1 ) k ⃗ = \left ( u_2v_3 - u_3v_2 \right )\vec{i} + \left ( u_3v_1 - u_1v_3 \right )\vec{j} + \left ( u_1v_2 - u_2v_1 \right )\vec{k} =(u2v3u3v2)i +(u3v1u1v3)j +(u1v2u2v1)k

你可能感兴趣的:(机器学习)