Basic Linear Algebra : QR Decomposition

QR Decomposition หรืออาจเรียกว่า QR Factorization คือการแยกเมตริกซ์ M ออกสองส่วนตามสมการ

(1)M=QR
เมื่อ
M เป็นเมตริกซ์ขนาด n×d เมื่อ n>d
Q เป็น orthogonal matrix
R เป็น upper triangular matrix

มีหลาย algorithm ที่ใช้หา QR แต่ในข้อเขียนนี้จะใช้ algorithm เรียกว่า Gram-Shmidt process

Algorithm

การทำงานของ Gram-Shmidt จะต้องทั้งหมด n ขั้นตอน เมื่อ n คือจำนวน column vectors ของเมตริกซ์ M
M=[m1|m2|m3|...|mn]
เมื่อ mn คือ column vector ของ M ขั้นตอนการทำงานคือ

1. ให้ e1=m1 และคำนวณ e^1=e1e1

2. ให้ e2=m2(m2e^1)e^1 และคำนวณ e^2=e2e2

3. ให้ e3=m3(m3e^1)e^1(m3e^2)e^2 และคำนวณ e^3=e3e3


n. ให้ en=mn(mne^1)e^1(mne^2)e^2...(mne^n1)e^n1 และคำนวณ e^n=enen

สุดท้ายแล้วจะได้

M=[m1|m2|m3|...|mn]=[e1^|e2^|e3^|...|en^][m1e1^m2e1^m3e1^mne1^0m2e2^m3e2^mne2^00m3e3^mne3^000mnen^]=QR

ตัวอย่าง

M=[110101011]
จะได้
m1=[110],m2=[101],m3=[011]
คำนวณหา QR e1=m1e1^=e1e1=12[110]e2=m2(m2e^1)e^1=[101]([101][12120])[12120]=[12121]e2^=e2e2=[161626]e3=m3(m3e1^)e1^(m3e2^)e2^=[011]12[12120]16[161626]=[131313]e3^=e3e3=[131313]
ดังนั้น Q=[e1^|e2^|e3^]=[12161312161302613]R=[m1e1^m2e1^m3e1^0m2e2^m3e2^00m3e3^]=[221212036160023]

Python implementation

การหา QR ด้วยภาษา Python มีหลายทางได้แก่ numpy.linalg.qr, scipy.linalg.qr หรือ sympy.Matrix

Numpy /Scipy :
      
      import numpy as np
      
      M = np.array([[1,1,0],[1,0,1],[0,1,1]])
      Q,R = np.linalg.qr(M)
      
    


Sympy :
      
      from sympy import Matrix
      from sympy.interactive.printing import init_printing
      init_printing(use_unicode=False, wrap_line=False)
      
      # create sympy matrix from numpy matrix
      X = Matrix(M)
      
      # print Q and R matrix
      X.QRdecomposition()
      
    


เอกสารอ้างอิง
[1] https://en.wikipedia.org/wiki/QR_decomposition
[2] https://smarter-machine.blogspot.com/2021/03/basic-linear-algebra-orthogonal-matrix.html
[3] https://smarter-machine.blogspot.com/2021/03/basic-linear-algebra-lu-matrix.html
[4] https://en.wikipedia.org/wiki/Gram%E2%80%93Schmidt_process#Numerical_stability

ความคิดเห็น