본문 바로가기

Python/AI 수학 with Python

[Python] 벡터의 선형변환, 표준기저 구현

[In]

# 벡터 그리기
%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt

# 화살표를 그리는 함수
def arrow(start, size, color):
    plt.quiver(start[0], start[1], size[0], size[1], 
    angles = 'xy', scale_units = 'xy', scale = 1, color = color)
    
# 화살표의 시작점
s = np.array([0,0])

# 벡터
a = np.array([2,3]) # 세로 벡터

arrow(s, a, color = 'black')

# 그래프 표시
plt.xlim([-3, 3])
plt.ylim([-3, 3])

plt.xlabel('x', size = 14)
plt.ylabel('y', size = 14)
plt.grid()
plt.gca().set_aspect('equal') # 가로 세로 비율 같게
plt.show()

[Out]

 

 

[In]

# 선형변환
%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt

a = np.array([2,3]) # 변환 전 벡터

A = np.array([[2,-1],
              [2,-2]]) # 2x2 행렬

b = np.dot(A, a) # 선형변환

print("변환 전의 벡터 a :", a)
print("변환 후의 벡터 b :", b)

def arrow(start, size, color):
    plt.quiver(start[0], start[1], size[0], size[1], 
    angles = "xy", scale_units = "xy", scale = 1, color = color)

s = np.array([0,0])

arrow(s, a, color = "black")
arrow(s, b, color = "blue")

# 그래프 표시
plt.xlim([-3, 3])
plt.ylim([-3, 3])
plt.xlabel('x', size = 14)
plt.ylabel('y', size = 14)
plt.grid()
plt.gca().set_aspect('equal') # 가로세로비 같게
plt.show()

[Out]

변환 전의 벡터 a : [2 3]
변환 후의 벡터 b : [ 1 -2]

 

 

 

[In]

# 표준기저
%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt

a = np.array([2,3])
e_x = np.array([1,0]) # 표준기져
e_y = np.array([0,1]) # 표준기저

print("a :", a)
print("e_x :", e_x)
print("e_y :", e_y)

def arrow(start, size, color):
    plt.quiver(start[0], start[1], size[0], size[1], 
               angles='xy', scale_units='xy', scale = 1, color = color)
    
s = np.array([0, 0]) # 원점

arrow(s, a, color = 'blue')
arrow(s, e_x, color = 'black')
arrow(s, e_y, color = 'black')

# 그래프 표시
plt.xlim([-3, 3])
plt.ylim([-3, 3])
plt.xlabel('x', size = 14)
plt.ylabel('y', size = 14)
plt.grid()
plt.gca().set_aspect('equal') # 가로세로비를 같게
plt.show()

[Out]

a : [2 3]
e_x : [1 0]
e_y : [0 1]