Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

선형대수 (Linear Algebra)

1스칼라 (Scalar)

nZn \in \mathbb{Z}
n = 1
print(type(n))
<class 'int'>
xRx \in \mathbb{R}
x = 1.0
print(type(x))
<class 'float'>

2벡터 (Vector)

x=[x1,x2,,xn]\mathbf{x} = [x_1, x_2, \dots , x_n]
xRnifxkR  (k=1n)\mathbf{x} \in \mathbb{R}^n \quad\text{if}\quad x_k \in \mathbb{R}\;(k=1 \dots n)
x = [1.2, 2.3, 3.4]
print(type(x))
<class 'list'>

3행렬 (Matrix)

A=[A1,1A1,2A1,3A2,1A2,2A2,3]\mathbf{A} = \begin{bmatrix} A_{1,1} & A_{1,2} & A_{1,3} \\ A_{2,1} & A_{2,2} & A_{2,3} \end{bmatrix}
ARm×nifAi,jR\mathbf{A} \in \mathbb{R}^{m\times n} \quad\text{if}\quad A_{i,j} \in \mathbb{R}
A = [[1.1, 1.2, 1.3], [2.1, 2.2, 2.3]]
print(type(A))
# 행렬(matrix)로 출력 (round 함수로 반올림)
print_matrix = lambda x: print('\n'.join(['\t'.join([str(round(cell, 1)) for cell in row]) for row in x]))
print_matrix(A)
<class 'list'>
1.1	1.2	1.3
2.1	2.2	2.3
# 3차원 배열
A = [[[1.1, 1.2], [2.1, 2.2]], [[3.1, 3.2], [4.1, 4.2]]]
print(type(A))
# 각 행렬을 출력
for i, matrix in enumerate(A):
    print("행렬 {}".format(i))
    print_matrix(matrix)
<class 'list'>
행렬 0
1.1	1.2
2.1	2.2
행렬 1
3.1	3.2
4.1	4.2

3.1전치 (Transpose)

A = [[1.1, 1.2, 1.3], [2.1, 2.2, 2.3]]
# Transpose
AT = [[A[j][i] for j in range(len(A))] for i in range(len(A[0]))]

print_matrix(A)
print("Transpose")
print_matrix(AT)
1.1	1.2	1.3
2.1	2.2	2.3
Transpose
1.1	2.1
1.2	2.2
1.3	2.3

3.2사칙 연산

A = [[1.1, 1.2, 1.3], [2.1, 2.2, 2.3]]
print('A')
print_matrix(A)
print('A + A')
B = [[A[i][j] + A[i][j] for j in range(len(A[0]))] for i in range(len(A))]
print_matrix(B)
print('A - A')
B = [[A[i][j] - A[i][j] for j in range(len(A[0]))] for i in range(len(A))]
print_matrix(B)
print('A * A')
B = [[A[i][j] * A[i][j] for j in range(len(A[0]))] for i in range(len(A))]
print_matrix(B)
print('A / A')
B = [[A[i][j] / A[i][j] for j in range(len(A[0]))] for i in range(len(A))]
print_matrix(B)

print('\nBroadcasting')
b = 2
print(f'b={b}\n')
print('A + b')
B = [[A[i][j] + b for j in range(len(A[0]))] for i in range(len(A))]
print_matrix(B)
print('A - b')
B = [[A[i][j] - b for j in range(len(A[0]))] for i in range(len(A))]
print_matrix(B)
print('A * b')
B = [[A[i][j] * b for j in range(len(A[0]))] for i in range(len(A))]
print_matrix(B)
print('A / b')
B = [[A[i][j] / b for j in range(len(A[0]))] for i in range(len(A))]
print_matrix(B)
A
1.1	1.2	1.3
2.1	2.2	2.3
A + A
2.2	2.4	2.6
4.2	4.4	4.6
A - A
0.0	0.0	0.0
0.0	0.0	0.0
A * A
1.2	1.4	1.7
4.4	4.8	5.3
A / A
1.0	1.0	1.0
1.0	1.0	1.0

Broadcasting
b=2

A + b
3.1	3.2	3.3
4.1	4.2	4.3
A - b
-0.9	-0.8	-0.7
0.1	0.2	0.3
A * b
2.2	2.4	2.6
4.2	4.4	4.6
A / b
0.6	0.6	0.7
1.1	1.1	1.1

4NumPy

import numpy as np

A = np.array([[1.1, 1.2, 1.3], [2.1, 2.2, 2.3]])

print(f'{A}\n-> Transpose ->\n {A.T}')
[[1.1 1.2 1.3]
 [2.1 2.2 2.3]]
-> Transpose ->
 [[1.1 2.1]
 [1.2 2.2]
 [1.3 2.3]]
A = np.array([[1.1, 1.2, 1.3], [2.1, 2.2, 2.3]])

assert np.all(A + A == A * 2)
assert np.all(A - A == 0)
assert np.all(A * A == A ** 2)
assert np.all(A / A == 1)

print('\nBroadcasting')
a, c = 2, 3
print(f'a={a}, b={c}\n')
print('aA + c')
B = a * A + c
print(B)

Broadcasting
a=2, b=3

aA + c
[[5.2 5.4 5.6]
 [7.2 7.4 7.6]]

4.1행렬곱

C=AB\mathbf{C}=\mathbf{A}\mathbf{B}
ARm×n,  BRn×p,  CRm×p\mathbf{A}\in\mathbb{R}^{m \times n},\;\mathbf{B}\in\mathbb{R}^{n \times p},\;\mathbf{C}\in\mathbb{R}^{m \times p}
Ci,j=kAi,kBk,jC_{i,j}=\sum_{k} A_{i,k}B_{k,j}

4.2스칼라곱

내적 (dot product)

c=aTb,cRifa,bRnc = \mathbf{a}^T\mathbf{b}, \quad c \in \mathbb{R} \quad \text{if} \quad \mathbf{a},\mathbf{b} \in \mathbb{R}^n

4.3원소별 곱 (element-wise product; Hadamard product)

$$

\mathbf{a} =

[a1a2an]\begin{bmatrix} a_1 \\ a_2 \\ \vdots \\ a_n \end{bmatrix}

\ \mathbf{a} \circ \mathbf{b} = \begin{bmatrix} a_1 \cdot b_1 \ a_2 \cdot b_2 \ \vdots \ a_n \cdot b_n \end{bmatrix}

$$

a = np.array([1, 2, 3], dtype=np.float32)
b = np.array([4, 5, 6], dtype=np.float32)

print('내적 (dot product)')
c = np.dot(a, b)
assert c == np.dot(b, a)
print(c)
print('원소별 곱셈 (element-wise product; Hadamard product)')
d = a * b
print(d)

A = np.array([[1.1, 1.2, 1.3], [2.1, 2.2, 2.3]])
B = np.array([[1.1, 1.2], [2.1, 2.2], [3.1, 3.2]])

print('행렬곱 (matrix multiplication)')
C = np.matmul(A, B)
print(C)
assert np.allclose(C, A @ B)
assert np.allclose(C, np.dot(A, B))

CN = np.matmul(B, A)
print(CN)
print(C.shape, CN.shape)

print('원소별 곱셈 (element-wise product; Hadamard product)')
D = A * A
print(D)
내적 (dot product)
32.0
원소별 곱셈 (element-wise product; Hadamard product)
[ 4. 10. 18.]
행렬곱 (matrix multiplication)
[[ 7.76  8.12]
 [14.06 14.72]]
[[ 3.73  3.96  4.19]
 [ 6.93  7.36  7.79]
 [10.13 10.76 11.39]]
(2, 2) (3, 3)
원소별 곱셈 (element-wise product; Hadamard product)
[[1.21 1.44 1.69]
 [4.41 4.84 5.29]]

4.4행렬-벡터 곱하기 (Matrix-Vector Multiplication)

A = np.arange(1, 7).reshape(3, 2)
x = np.array([7, 8])

4.4.1행별 내적 (Dot Product)

$$ (\mathbf{A}\mathbf{x}){j}=\sum{k=1}^{n}\mathbf{A}{j,k}x{k} \

\mathbf{A}\mathbf{x} =

[A1kxkA2kxkAmkxk]\begin{bmatrix} \sum \mathbf{A}_{1k}x_k \\ \sum \mathbf{A}_{2k}x_k \\ \vdots \\ \sum \mathbf{A}_{mk}x_k \end{bmatrix}

$$

np.dot(A, x)
array([23, 53, 83])

4.4.2열별 조합 (Linear Combination of Columns)

Ax=ixiA:,i \mathbf{A}\mathbf{x} = \sum_{i}x_i\mathbf{A}_{:,i}

A[:, 0] * x[0] + A[:, 1] * x[1]
array([23, 53, 83])
A(B+C)=AB+ACA(BC)=(AB)C\begin{align*} A(B+C) &= AB+AC \\ A(BC) &= (AB)C \end{align*}
ATB=BTA(AB)T=BTAT\begin{align*} A^TB &= B^TA \\ (AB)^T &= B^TA^T \end{align*}
A = np.random.randn(2, 2)
B = np.random.randn(2, 2)
C = np.random.randn(2, 2)

print('A(B+C) = AB + AC')
assert np.allclose(np.dot(A, B + C), np.dot(A, B) + np.dot(A, C))
print('A(BC) = (AB)C')
assert np.allclose(np.dot(A, np.dot(B, C)), np.dot(np.dot(A, B), C))
print('(AB))^T = B^TA^T')
assert np.allclose(np.dot(A, B).T, np.dot(B.T, A.T))
A(B+C) = AB + AC
A(BC) = (AB)C
(AB))^T = B^TA^T

5선형 방정식 (Linear System)

xRnwhereARm×n,bRm\mathbf{x}\in\mathbb{R}^n\quad\text{where}\quad A\in\mathbb{R}^{m \times n},\,\mathbf{b}\in\mathbb{R}^m

Ax=bA\mathbf{x}=\mathbf{b} \\

5.1예시

System of Linear Equations

  1. 2x1+3x2x3=52x_1 + 3x_2 - x_3 = 5

  2. x1+4x2+2x3=6-x_1 + 4x_2 + 2x_3 = 6

  3. x12x2+3x3=3x_1 - 2x_2 + 3x_3 = -3

Ax=bA\mathbf{x} = \mathbf{b}:

[231142123][x1x2x3]=[563]\begin{bmatrix} 2 & 3 & -1 \\ -1 & 4 & 2 \\ 1 & -2 & 3 \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \\ x_3 \end{bmatrix} = \begin{bmatrix} 5 \\ 6 \\ -3 \end{bmatrix}

Where:

A=[231142123]x=[x1x2x3]b=[563]A = \begin{bmatrix} 2 & 3 & -1 \\ -1 & 4 & 2 \\ 1 & -2 & 3 \end{bmatrix} \quad \mathbf{x} = \begin{bmatrix} x_1 \\ x_2 \\ x_3 \end{bmatrix} \quad \mathbf{b} = \begin{bmatrix} 5 \\ 6 \\ -3 \end{bmatrix}
A = np.array([[2, 3, -1], 
              [-1, 4, 2], 
              [1, -2, 3]])
b = np.array([5, 6, -3])

6항등 행렬(Identity Matrix)과 역행렬(Inverse Matrix)

I = np.dot(A, np.linalg.inv(A))
np.around(I)
array([[ 1., 0., -0.], [-0., 1., 0.], [ 0., 0., 1.]])

7행렬식 (determinant)

np.around(np.linalg.det(A))
49.0
Ax=bA1Ax=A1bIx=A1bx=A1b\begin{align*} A\mathbf{x} &= \mathbf{b} \\ A^{-1}A\mathbf{x} &= A^{-1}\mathbf{b} \\ I\mathbf{x}&=A^{-1}\mathbf{b} \\ \mathbf{x}&=A^{-1}\mathbf{b} \end{align*}
x = np.dot(np.linalg.inv(A), b)
np.around(x, 2)
array([ 0.16, 1.55, -0.02])

8특이 행렬 (Singular Matrix)

행렬식(determinant)이 0인 경우, 역행렬이 존재하지 않습니다.

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

print(f'det(A)={np.linalg.det(A)}')

try:
    np.linalg.inv(A)
except Exception as e:
    print(e)
det(A)=0.0
Singular matrix