Transformations and matrix multiplication

Compositions of linear transformations 1

現在要來討論的是, linear transformation 的組合要怎麼表示?他還算是 linear transformation 嗎?

我們現在有 Rn, Rm, Rl 空間,以及 S 和 T 兩個 linear transformation function

S:XYXRn,YRmS(x)=AxA is m×nT:YZYRm,ZRlT(x)=BxB is l×m\begin{aligned} &S: X \to Y \mid X \subseteq \mathbb{R}^n, Y \subseteq \mathbb{R}^m \\ &S(\vec{x}) = \mathbf{A}\vec{x} \mid \mathbf{A} \text{ is } m \times n \\\\ &T: Y \to Z \mid Y \subseteq \mathbb{R}^m, Z \subseteq \mathbb{R}^l \\ &T(\vec{x}) = \mathbf{B}\vec{x} \mid \mathbf{B} \text{ is } l \times m \end{aligned}

那我們要怎麼一次定義 x 到 z ,也就是先對 vector 做 S 再做 T 的 transformation 這段變化呢

TS:XZ ( The composition of T with S )TS=T(S(x))T \circ S: X \to Z \text{ ( The composition of T with S )} \\ T \circ S = T(S(\vec{x}))

那這樣子定義還是不是 linear transformation 呢

  • 條件一 OK

TS(x+y)=T(S(x+y))=T(S(x)+S(y))=T(S(x))+T(S(y))=TS(x)+TS(y)\begin{aligned} T \circ S(\vec{x} + \vec{y}) &= T(S(\vec{x}+\vec{y}))\\ &= T(S(\vec{x})+S(\vec{y}))\\ &= T(S(\vec{x})) + T(S(\vec{y}))\\ &= T \circ S(\vec{x}) + T \circ S(\vec{y}) \end{aligned}
  • 條件二 OK

TS(cx)=T(S(cx))=T(cS(x))=cT(S(x))=cTS(x)\begin{aligned} T \circ S(c\vec{x} ) &= T(S(c\vec{x})) \\ &= T(cS(\vec{x})) \\ &= cT(S(\vec{x})) \\ &= c T \circ S(\vec{x}) \end{aligned}

所以我們的確可以把 S 和 T 兩個 transformation 合併,用一個 matrix 來表達

TS(x)=CxC is l×nT \circ S (\vec{x}) = \mathbf{C}\vec{x} \mid C \text{ is } l \times n

Compositions of linear transformations 2

現在將更進一步探討 S 和 T 兩個 transformation 如何轉為 matrix 表示

T(x)=BxB is l×mS(x)=AxA is m×nTS=B(A(x))=CxT(\vec{x})= \mathbf{B}\vec{x} \mid \mathbf{B} \text{ is } l \times m\\ S(\vec{x})= \mathbf{A}\vec{x} \mid \mathbf{A} \text{ is } m \times n\\ T \circ S = \mathbf{B}(\mathbf{A}(\vec{x})) = \mathbf{C}\vec{x}

我們要如何找到這個 C ,首先這個 C 將會是多大 size

因為 vector 從 Rn 開始被轉換,所以先定義 identity matrix 為 In

In=[10001000001]\mathbf{I_n} = \begin{bmatrix} 1&0&\cdots& 0\\ 0&1&\cdots&0 \\0&0&\ddots&\vdots\\0&0&\cdots&1\end{bmatrix}

而我們的 C 就會等於

C=[B(A([100]))B(A([010]))B(A([001]))]\mathbf{C} = \begin{bmatrix} \mathbf{B}(\mathbf{A}\left(\begin{bmatrix} 1\\0\\\vdots\\0\end{bmatrix}\right))& \mathbf{B}(\mathbf{A}\left(\begin{bmatrix} 0\\1\\\vdots\\0\end{bmatrix}\right))& \cdots& \mathbf{B}(\mathbf{A}\left(\begin{bmatrix} 0\\0\\\vdots\\1\end{bmatrix}\right)) \end{bmatrix}

雖然看起來很複雜,但我們將 A 拆開來看,A 的運算應該如下

A=[a1a2an][x1x2xn]=x1a1+x2a2++xnan\mathbf{A} = \begin{bmatrix} \vec{a_1} &\vec{a_2}&\cdots &\vec{a_n}\end{bmatrix} \begin{bmatrix} x_1\\x_2\\\vdots\\x_n\end{bmatrix} = x_1\vec{a_1}+x_2\vec{a_2}+\cdots+x_n\vec{a_n}

所以 C 的 A 只有乘到每一項對應的 vector 而已,而這個 matrix 也就是 BA 相乘 !

C=[B(a1)B(a2)B(an)]=BA\mathbf{C} = \begin{bmatrix} \mathbf{B}(\vec{a_1})& \mathbf{B}(\vec{a_2})& \cdots& \mathbf{B}(\vec{a_n}) \end{bmatrix} = \mathbf{BA}

Matrix product examples

我們現在知道 B 和 A 相乘的意義其實就是 S 和 T 這兩個 transformation 的 composition

我們實際舉個例子來運算看看

B2×3=[112021],A3×4=[101120113102]\underset{2\times3}{\mathbf{B}} = \begin{bmatrix}1&-1&2\\0&-2&1\end{bmatrix},\,\, \underset{3\times4}{\mathbf{A}} = \begin{bmatrix}1&0&1&1\\2&0&1&-1\\3&1&0&2\end{bmatrix}

所以 BA 會等於

BA=[B(a1)B(a2)B(an)]=[B[123]B[001]B[110]B[112]]=[52061124]\begin{aligned} \mathbf{BA} &= \begin{bmatrix} \mathbf{B}(\vec{a_1})& \mathbf{B}(\vec{a_2})& \cdots& \mathbf{B}(\vec{a_n}) \end{bmatrix} \\ &= \begin{bmatrix} \mathbf{B}\begin{bmatrix}1\\2\\3\end{bmatrix} & \mathbf{B}\begin{bmatrix}0\\0\\1\end{bmatrix} & \mathbf{B}\begin{bmatrix}1\\1\\0\end{bmatrix} & \mathbf{B}\begin{bmatrix}1\\-1\\2\end{bmatrix} \end{bmatrix}\\ &= \begin{bmatrix} 5&2&0&6\\-1&1&-2&4 \end{bmatrix} \end{aligned}

更詳細的運算過程可以去看影片

我們可以發現,能夠運算如此順利,是因為 B 和 A 已經 well defined

因為 2 x 3 和 3 x 4 的矩陣相乘,我們可以得到一個 2 x 4 的矩陣

今天若是 B 和 A 交換,要求 AB 是求不出來的

所以這邊又點出一個觀念,就是 矩陣相乘是沒有交換律的 (NO commutative)

ABBA\mathbf{AB \neq BA}

Matrix product associativity

那們矩陣相乘有沒有結合律呢?我們從更多的 transformation 來看

H(x)=AxG(x)=BxF(x)=CxH(\vec{x}) = \mathbf{A}\vec{x}\\ G(\vec{x}) = \mathbf{B}\vec{x}\\ F(\vec{x}) = \mathbf{C}\vec{x}\\

我們將轉移到 F 後,再轉移到 G ,最終轉移到 H,可以這樣寫

((HG)F)(x)=(HG)(F(x))=H(G(F(x)))=H(GF(x))=(H(GF))(x)\begin{aligned} ((H\circ G) \circ F)(\vec{x}) &= (H\circ G)(F(\vec{x})) \\&= H(G(F(\vec{x})))\\&= H(G\circ F(\vec{x})) \\&= (H\circ (G \circ F))(\vec{x}) \end{aligned}

結果我們發現

((HG)F)(x)=(H(GF))(x)=(HGF)(x)((H\circ G) \circ F)(\vec{x}) = (H\circ (G \circ F))(\vec{x}) = (H\circ G \circ F)(\vec{x})

也就是說 矩陣相乘是有結合律的 (HAS Associative)

(AB)C=A(BC)=ABC(\mathbf{AB})\mathbf{C} = \mathbf{A}(\mathbf{BC}) = \mathbf{ABC}

簡單來說就是可以忽略掉括號啦

Distributive property of matrix products

最後我們來看矩陣相乘有沒有分配律 !

A=k×mB=m×nC=m×n\mathbf{A} = k \times m\\ \mathbf{B} = m \times n\\ \mathbf{C} = m \times n\\

試著乘乘看

A(B+C)=A[b1+c1b2+c2bn+cn]=[A(b1+c1)A(b2+c2)A(bn+cn)]=[Ab1+Ac1Ab2+Ac2Abn+Acn]=[Ab1Ab2Abn]+[Ac1Ac2Acn]=AB+AC\begin{aligned} \mathbf{A}(\mathbf{B+C}) &= \mathbf{A} \begin{bmatrix}\vec{b_1}+\vec{c_1}& \vec{b_2}+\vec{c_2} & \cdots & \vec{b_n}+\vec{c_n}\end{bmatrix}\\ &= \begin{bmatrix}\mathbf{A}(\vec{b_1}+\vec{c_1})& \mathbf{A}(\vec{b_2}+\vec{c_2}) & \cdots & \mathbf{A}(\vec{b_n}+\vec{c_n})\end{bmatrix} \\ &= \begin{bmatrix}\mathbf{A}\vec{b_1}+\mathbf{A}\vec{c_1}& \mathbf{A}\vec{b_2}+\mathbf{A}\vec{c_2} & \cdots & \mathbf{A}\vec{b_n}+\mathbf{A}\vec{c_n}\end{bmatrix} \\ &= \begin{bmatrix}\mathbf{A}\vec{b_1}& \mathbf{A}\vec{b_2} & \cdots & \mathbf{A}\vec{b_n}\end{bmatrix} + \begin{bmatrix}\mathbf{A}\vec{c_1}& \mathbf{A}\vec{c_2} & \cdots & \mathbf{A}\vec{c_n}\end{bmatrix} \\ &= \mathbf{AB} + \mathbf{AC} \end{aligned}

所以矩陣相乘是有分配律的 (HAS Distributive)

統整一下:

  • matrix multiplication has no commutative

    ABBA\mathbf{AB} \neq \mathbf{BA}
  • matrix multiplication has associative

A(BC)=(AB)C=ABC\mathbf{A}(\mathbf{BC}) = (\mathbf{AB})\mathbf{C} = \mathbf{ABC}
  • matrix multiplication has distributive

A(B+C)=AB+AC\mathbf{A}(\mathbf{B+C}) = \mathbf{AB}+\mathbf{AC}

Last updated