[선형대수학] 행렬(2) - Product 및 선형변환

김기정·2024년 5월 2일
0

선형대수학

목록 보기
3/6

지난 포스팅 행렬(1) - 행렬의 정의 및 기본연산에 이어서 행렬의 곱셈(matrix multiplication), 선형변환(Linear Transformation)에 대해 알아보겠습니다.

matrix multiplication

행렬의 곱셈 즉 행렬의 내적을 계산할 때는 위와 같이 "ㄱ"모양으로 진행하면 됩니다. 수식으로 설명하면 다음과 같습니다.

For m×nm \times n matrix AA and n×pn \times p matrix BB,

A=[a11a12a13a1na21a22a23a2na31a32a33a3nam1am2am3amn]&B=[b11b12b13b1pb21b22b23b2pb31b32b33b3pbn1bn2bn3bnp]A = \begin{bmatrix} a_{11} & a_{12} & a_{13} & \cdots & a_{1n} \\ a_{21} & a_{22} & a_{23} & \cdots & a_{2n} \\ a_{31} & a_{32} & a_{33} & \cdots & a_{3n} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & a_{m3} & \cdots & a_{mn} \end{bmatrix} \quad \& \quad B = \begin{bmatrix} b_{11} & b_{12} & b_{13} & \cdots & b_{1p} \\ b_{21} & b_{22} & b_{23} & \cdots & b_{2p} \\ b_{31} & b_{32} & b_{33} & \cdots & b_{3p} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ b_{n1} & b_{n2} & b_{n3} & \cdots & b_{np} \end{bmatrix}

위와 같이 행렬 A, B가 정의되어 있습니다. 그럼 C=ABC = A\cdot B는 다음과 같이 m×pm \times p인 행렬이 됩니다.

C=[c11c12c13c1pc21c22c23c2pc31c32c33c3pcm1cm2cm3cmp], cij=ai1b1j+ai2b2j++ainbnj=<ai,bj>=k=1naikbkjC = \begin{bmatrix} c_{11} & c_{12} & c_{13} & \cdots & c_{1p} \\ c_{21} & c_{22} & c_{23} & \cdots & c_{2p} \\ c_{31} & c_{32} & c_{33} & \cdots & c_{3p} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ c_{m1} & c_{m2} & c_{m3} & \cdots & c_{mp} \end{bmatrix} ,\quad \begin{matrix} \ c_{ij} &=& a_{i1}b_{1j} +a_{i2}b_{2j} + \cdots +a_{in}b_{nj} \\\\ &=& <a_i, b_j> \quad = \quad \sum_{k=1}^n{a_{ik}b_{kj}} \end{matrix}

다시 말해 cijc_{ij}AAii번째 row vector(=aia_i)와 BBjj번째 column vector(=bjb_j)의 내적으로 구할 수 있습니다.

example

A=[102131],B=[312110]AB=?A = \begin{bmatrix} 1 & 0 & 2 \\ -1 & 3 & 1 \end{bmatrix} , B = \begin{bmatrix} 3 & 1 \\ 2 & 1 \\ 1 & 0 \end{bmatrix} \quad \Rightarrow \quad A \cdot B = ?

sol>

AB=[102131][312110]=[13+02+2111+01+2013+32+1111+31+10]=[5142]\begin{matrix} A \cdot B &&=&& \begin{bmatrix} 1 & 0 & 2 \\ -1 & 3 & 1 \end{bmatrix} \cdot \begin{bmatrix} 3 & 1 \\ 2 & 1 \\ 1 & 0 \end{bmatrix} \\\\ &&=&& \begin{bmatrix} 1\cdot3 + 0\cdot 2 + 2\cdot 1 && 1\cdot 1 +0\cdot 1 + 2\cdot0 \\ -1\cdot3 + 3\cdot 2 + 1\cdot1 && -1\cdot1 + 3\cdot 1 + 1\cdot 0\\ \end{bmatrix} \\\\ &&=&& \begin{bmatrix} 5 & 1 \\ 4 & 2 \\ \end{bmatrix} \end{matrix}
import numpy as np

A = np.array([[1,0,2],
              [-1,3,1]])

B = np.array([[3,1],
              [2,1],
              [1,0]])


print(f"A\n{A}\n")

print(f"B \n {B}\n")

print(f"A x B \n {np.dot(A,B)}")

result

또한 numpy의 기능으로 행렬 각각의 성분 곱을 지원합니다. 이는 아래와 같습니다.

import numpy as np

A = np.array([[3,1],
              [2,1],
              [1,0]])


B = np.array([[1,2],
              [3,4],
              [5,6]])


print(f"A\n{A}\n")

print(f"B \n {B}\n")

print(f"성분 곱 \n {A * B}")

result

근데 만약 크기가 다른 행렬을 성분 곱을 취했을 때는 다음과 같은 Error가 발생합니다.

행렬과 벡터를 연산하는 데 있어서 size를 중요하게 고려해야합니다.

지금까지 행렬의 곱셈에 대해 설명하고 예제를 손으로 직접 구해도 보고 numpy를 이용해 구해봤습니다. 이제 부터는 행렬의 곱셈의 성질에 대해 알아보겠습니다.

Property1. Associativity

Let AA be a m×nm \times n matrix & BB be a n×pn \times p matrix & CC be a p×qp \times q matrix.
we have that

A(BC)=(AB)CA(BC) = (AB)C

example> numpy를 이용해 무작위로 행렬을 만들고 값을 비교해보겠습니다.

Let AA be a 4×34 \times 3 matrix & BB be a 3×53 \times 5 matrix & CC be a 5×75 \times 7 matrix.

import numpy as np

A = np.random.randint(6, size=(4,3))

B = np.random.randint(6, size=(3,5))

C = np.random.randint(6, size=(5,7))

print(f"A \n {A}\n")

print(f"B \n {B}\n")

print(f"C \n {C}\n")

m1 = np.dot(np.dot(A,B),C)

m2 = np.dot(A, np.dot(B,C))

print(f"(AB)C \n {m1}\n")

print(f"A(BC) \n {m2}\n")

result>

Property 2. distributivity

Let AA and BB be m×nm \times n matrices & CC be a n×pn \times p matrix.
we have that

(A+B)C=AC+BC(A+B)C = AC + BC

example> numpy를 이용해 무작위로 행렬을 만들고 값을 비교해보겠습니다.

Let AA and BB be 4×34 \times 3 matrices & CC be a 3×53 \times 5 matrix

import numpy as np

A = np.random.randint(6, size=(4,3))

B = np.random.randint(6, size=(4,3))

C = np.random.randint(6, size=(3,5))

print(f"A \n {A}\n")

print(f"B \n {B}\n")

print(f"C \n {C}\n")

r1 = np.dot((A + B),C)

r2 = np.dot(A,C) + np.dot(B,C)

print(f"(A+B)C \n {r1}\n")


print(f"AC + BC  \n {r2}\n")

result>

당연하게도 다음도 성립합니다.

Let CC be a m×nm \times n matrix & AA and BB be n×pn \times p matrices.
we have that

C(A+B)=CA+CBC(A+B) = CA + CB

그리고 matrix multiplication에서는 교환법칙(commutative)이 성립되지 않습니다.

commutative ? \Rightarrow 성립 x

ABBAAB \ne BA

당연하게 AAa×ba \times b matrix이고 BBb×cb \times c matrix 이면 애초에 행렬간의 곱셈을 취할 수 없습니다.
그리고 A,BA, Bn×nn \times n인 정방 행렬인 경우에 대해서 살펴보겠습니다.

Let AB=CAB = C and BA=C BA = C^{\ \prime}. and we have that

cij = ai1b1j+ai2b2j++ainbnj = k=1naikbkj,c_{ij} \ = \ a_{i1}b_{1j} +a_{i2}b_{2j} + \cdots +a_{in}b_{nj} \ = \ \sum_{k=1}^n{a_{ik}b_{kj}} \quad ,
cij  = bi1a1j+bi2a2j++binanj = k=1nbikakjc_{ij}^{\ \prime} \ = \ b_{i1}a_{1j} +b_{i2}a_{2j} + \cdots +b_{in}a_{nj} \ = \ \sum_{k=1}^n{b_{ik}a_{kj}} \quad

Socijcij CC .\quad c_{ij} \ne c_{ij}^{\ \prime} \quad \Rightarrow \quad C \ne C^{\ \prime}.

일반적으로 행렬의 곱셈에서 교환법칙은 성립하지 않습니다.

Row Operation(기본 행 연산)과 Linear equations(방정식)에 대해서는 다음에 다루도록 하겠습니다.

이제는 Linear Transformations(선형변환)에 대해 알아보겠습니다.

Linear Transformations

간단하게 선형 변환은 차원을 늘리거나 축소시키는 것을 의미합니다.
먼저 수학적으로 정의부터 살펴보겠습니다.

Def> linear transformation

Let V,WV, W be vector spaces over the same field FF.
A function T:VWT : V \rightarrow W is said to be a linear transformation if for any two vectors u,vVu, v \in V and any scalar kFk \in F the following two conditions ar satisfied:

  • additivity : T(u+v)=T(u)+T(v)T(u + v) = T(u) + T(v)
  • homogeneity : T(ku)=kT(u)T(ku) = k T(u)
  • T(ku+v)=kT(u)+T(v)T(ku+v) = kT(u) + T(v)

위키 백과를 통한 수학적 정의는 위와 같습니다. 예제를 들어 설명해보겠습니다.

example>
Let AA be a m×nm \times n matrix

Au=v,with uRn and vRm,Au = v, \quad \text{with} \ u \in \R^n \ and \ v \in \R^m,

즉 위에서는 nn차원인 uu 벡터가 mm 차원인 vv 벡터로 선형변환 된것입니다.

선형변환은 이후 차원 축소, 차원 확대, 고유값 고유벡터를 설명할 때 쓰이는 아주 중요한 개념 입니다.

추가적으로 더 궁금한 내용이나 포스팅에 오류가 있을 경우 댓글 부탁드립니다. 이번 포스팅은 여기서 마치겠습니다.

Reference

profile
끊임 없이 도전하는 개발자입니다.

0개의 댓글