线性代数|机器学习-P4正交矩阵中的标准正交向量
文章目录
1. 正交矩阵Q
1.1 矩阵Q的推导
方阵A正交的充要条件是A的行(列)向量组是单位正交向量组. 我们定义正交矩阵Q表示如下:
Q = [ q 1 q 2 ⋯ q n ] ; q i T q j = { 1 , i = j 0 , i ≠ j \begin{equation} Q=\begin{bmatrix}q_1&q_2&\cdots&q_n\end{bmatrix};q_i^Tq_j=\left\{ \begin{aligned} 1&,i=j \\ 0&,i\neq j \\ \end{aligned} \right. \end{equation} Q=[q1q2⋯qn];qiTqj={10,i=j,i=j
- 对矩阵Q展开可得:
Q T Q = [ q 1 T q 2 T ⋮ q n T ] [ q 1 q 2 ⋯ q n ] = [ q 1 T q 1 q 1 T q 2 ⋯ q 1 T q n q 2 T q 1 q 2 T q 2 ⋯ q 2 T q n ⋮ ⋮ ⋮ ⋮ q n T q 1 q n T q 2 ⋯ q n T q n ] = [ 1 0 ⋯ 0 0 1 ⋯ 0 ⋮ ⋮ ⋮ ⋮ 0 0 ⋯ 1 ] \begin{equation} Q^TQ=\begin{bmatrix}q_1^T\\\\q_2^T\\\\\vdots \\\\q_n^T\end{bmatrix}\begin{bmatrix}q_1&q_2&\cdots &q_n\end{bmatrix}=\begin{bmatrix} q_1^Tq_1&q_1^Tq_2&\cdots&q_1^Tq_n\\\\ q_2^Tq_1&q_2^Tq_2&\cdots&q_2^Tq_n\\\\ \vdots&\vdots&\vdots&\vdots\\\\ q_n^Tq_1&q_n^Tq_2&\cdots&q_n^Tq_n \end{bmatrix}=\begin{bmatrix} 1&0&\cdots&0\\\\ 0&1&\cdots&0\\\\ \vdots&\vdots&\vdots&\vdots\\\\ 0&0&\cdots&1 \end{bmatrix} \end{equation} QTQ= q1Tq2T⋮qnT [q1q2⋯qn]= q1Tq1q2Tq1⋮qnTq1q1Tq2q2Tq2⋮qnTq2⋯⋯⋮⋯q1Tqnq2Tqn⋮qnTqn = 10⋮001⋮0⋯⋯⋮⋯00⋮1
1.2 |Qx|=|x|
首先我们知道对于一个正交矩阵Q来说,满足: Q T Q = I Q^TQ=I QTQ=I
- 两边分别乘以 x T , x x^T,x xT,x
x T Q T Q x = x T x → ( Q x ) T ( Q x ) = x T x → ∣ Q x ∣ 2 = ∣ x ∣ 2 → ∣ Q x ∣ = ∣ x ∣ \begin{equation} x^TQ^TQx=x^Tx\rightarrow (Qx)^T(Qx)=x^Tx\rightarrow |Qx|^2=|x|^2\rightarrow |Qx|=|x| \end{equation} xTQTQx=xTx→(Qx)T(Qx)=xTx→∣Qx∣2=∣x∣2→∣Qx∣=∣x∣ - 小结:也就是说,对于任意向量x来说,我们设计一个正交矩阵Q,在不断地左乘矩阵Q的时候,因为 ∣ Q x ∣ = ∣ x ∣ |Qx|=|x| ∣Qx∣=∣x∣的原因,所以矩阵大小值不会变,这点在计算编程中至关重要,这样的话,我们就可以保证矩阵相乘的时候值不会溢出。
- Python 代码:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName :L2Norm.py
# @Time :2024/10/18 4:44
# @Author :Jason Zhang
import numpy as np
np.set_printoptions(suppress=True, precision=3)
class L2Norm(object):
def __init__(self, matrix, vector):
self.matrix = matrix
self.L2VectorNorm = 0
self.L2MatrixNorm = 0
self.vector = vector
self.matrixQ = np.zeros_like(self.matrix)
self.result = np.zeros_like(self.vector)
self.init_method()
def init_method(self):
self.get_MatrixQ()
self.get_L2VectorNorm()
self.get_L2MatrixNorm()
self.get_result()
def get_MatrixQ(self):
my_matrix = np.zeros_like(self.matrix)
my_matrix, _ = np.linalg.qr(self.matrix)
self.matrixQ = my_matrix
return my_matrix
def get_L2VectorNorm(self):
my_L2VectorNorm = np.linalg.norm(self.vector, ord=2)
self.L2VectorNorm = my_L2VectorNorm
return my_L2VectorNorm
def get_result(self):
my_result = np.zeros_like(self.vector)
my_result = self.matrixQ @ self.vector
my_result = np.linalg.norm(my_result, ord=2)
self.result = my_result
return my_result
def get_L2MatrixNorm(self):
my_L2MatrixNorm = np.linalg.norm(self.matrixQ, ord=2)
self.L2MatrixNorm = my_L2MatrixNorm
return my_L2MatrixNorm
def __repr__(self):
return f"************************\n" \
f"Matrix = \n{self.matrix}\n" \
f"MatrixQ = \n{self.matrixQ}\n" \
f"vector = \n{self.vector}\n" \
f"vectorL2 = \n{self.get_L2VectorNorm()}\n" \
f"result_|QX| = \n{self.get_result()}\n" \
f"************************\n"
if __name__ == "__main__":
run_code = 0
Matrix = np.arange(9).reshape((3, 3))
vector = np.array([1, 2, 3])
MyQX = L2Norm(Matrix, vector)
print(MyQX)
- 结果:
************************
Matrix =
[[0 1 2]
[3 4 5]
[6 7 8]]
MatrixQ =
[[ 0. 0.913 0.408]
[-0.447 0.365 -0.816]
[-0.894 -0.183 0.408]]
vector =
[1 2 3]
vectorL2 =
3.7416573867739413
result_|QX| =
3.7416573867739413
************************
2. 常见正交矩阵
对称矩阵和正交矩阵的特征向量组为正交单位向量,所以我们为了方便后续的快速计算,我们希望直接找正交矩阵和对称矩阵。
2.1 旋转矩阵
- 我们将向量在保持大小不变的情况下,逆时针旋转 θ \theta θ, 我们分别看看向量 x 1 = [ 0 , 1 ] T , x 2 = [ 1 , 0 ] T x_1=[0,1]^T,x_2=[1,0]^T x1=[0,1]T,x2=[1,0]T变化结果,如图所述:
A = [ 1 0 ] → A 1 = [ cos θ sin θ ] ; B = [ 0 1 ] → B 1 = [ − sin θ cos θ ] \begin{equation} A=\begin{bmatrix} 1\\\\0 \end{bmatrix}\rightarrow A_1=\begin{bmatrix} \cos{\theta}\\\\\sin{\theta} \end{bmatrix};B=\begin{bmatrix} 0\\\\1 \end{bmatrix}\rightarrow B_1=\begin{bmatrix} -\sin{\theta}\\\\\cos{\theta} \end{bmatrix} \end{equation} A= 10 →A1= cosθsinθ ;B= 01 →B1= −sinθcosθ - 可得旋转矩阵Q表示如下:
Q = [ cos θ − sin θ sin θ cos θ ] \begin{equation} Q=\begin{bmatrix} \cos{\theta}&-\sin{\theta}\\\\ \sin{\theta}&\cos{\theta} \end{bmatrix} \end{equation} Q= cosθsinθ−sinθcosθ 
2.2 镜像矩阵
- 我们将向量在保持大小不变的情况下,沿着 1 2 θ \frac{1}{2}\theta 21θ镜像, 我们分别看看向量 x 1 = [ 0 , 1 ] T , x 2 = [ 1 , 0 ] T x_1=[0,1]^T,x_2=[1,0]^T x1=[0,1]T,x2=[1,0]T变化结果,如图所述:
A = [ 1 0 ] → A 1 = [ cos θ sin θ ] ; B = [ 0 1 ] → B 1 = [ sin θ − cos θ ] \begin{equation} A=\begin{bmatrix} 1\\\\0 \end{bmatrix}\rightarrow A_1=\begin{bmatrix} \cos{\theta}\\\\\sin{\theta} \end{bmatrix};B=\begin{bmatrix} 0\\\\1 \end{bmatrix}\rightarrow B_1=\begin{bmatrix} \sin{\theta}\\\\-\cos{\theta} \end{bmatrix} \end{equation} A= 10 →A1= cosθsinθ ;B= 01 →B1= sinθ−cosθ - 可得旋转矩阵Q表示如下:
R e f l e c t i o n = [ cos θ sin θ sin θ − cos θ ] \begin{equation} Reflection=\begin{bmatrix} \cos{\theta}&\sin{\theta}\\\\ \sin{\theta}&-\cos{\theta} \end{bmatrix} \end{equation} Reflection= cosθsinθsinθ−cosθ 
- python 代码,旋转矩阵和镜像矩阵
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName :Orthogonal_Matrix.py
# @Time :2024/10/18 5:17
# @Author :Jason Zhang
import numpy as np
np.set_printoptions(suppress=True, precision=3)
class RotationMatrix(object):
def __init__(self, theta):
self.theta = theta * 1.0 / 360.0 * 2 * np.pi
self.RotationMatrix = np.zeros((2, 2))
def get_RotationMatrix(self):
self.RotationMatrix = np.array([[np.cos(self.theta), -np.sin(self.theta)],
[np.sin(self.theta), np.cos(self.theta)]])
return self.RotationMatrix
class ReflectionMatrix(object):
def __init__(self, theta):
self.theta = theta * 1.0 / 360.0 * 2 * np.pi
self.ReflectionMatrix = np.zeros((2, 2))
def get_ReflectionMatrix(self):
self.ReflectionMatrix = np.array([[np.cos(self.theta), np.sin(self.theta)],
[np.sin(self.theta), -np.cos(self.theta)]])
return self.ReflectionMatrix
if __name__ == "__main__":
run_code = 0
my_theta = 45
MyRotationMatrix = RotationMatrix(my_theta)
MyNewRoationMatrix = MyRotationMatrix.get_RotationMatrix()
vector = np.array([1, 0])
Rotationresult = MyNewRoationMatrix @ vector
print(f"theta={my_theta}\nMyNewRoationMatrix=\n{MyNewRoationMatrix}")
print(f"vector={vector}")
print(f"Rotationresult={Rotationresult}")
MyReflectionMatrix = ReflectionMatrix(my_theta)
MyNewReflectionMatrix = MyReflectionMatrix.get_ReflectionMatrix()
ReflectionResult = MyNewReflectionMatrix @ vector
print(f"theta={my_theta}\nMyNewReflectionMatrix=\n{MyNewReflectionMatrix}")
print(f"vector={vector}")
print(f"ReflectionResult={ReflectionResult}")
- 结果:
theta=45
MyNewRoationMatrix=
[[ 0.707 -0.707]
[ 0.707 0.707]]
vector=[1 0]
Rotationresult=[0.707 0.707]
theta=45
MyNewReflectionMatrix=
[[ 0.707 0.707]
[ 0.707 -0.707]]
vector=[1 0]
ReflectionResult=[0.707 0.707]
2.3 Householder矩阵
-
主要链接如下:householder进行矩阵QR分解
householder变换的作用是将向量x的第一项值为|x|,其他值不变,相当于将x通过镜面反射得到y -
x:为我们输入的向量x
-
y:为经过householder变换后向量y
-
w: x-y=2w,等腰三角形底边相等
-
u:定义跟w平行的单位向量u,|u|=1;

-
根据 u T x u^Tx uTx公式可得,|u|=1
u T x = ∣ x ∣ ∣ u T ∣ cos α = ∣ x ∣ cos α = ∣ w ∣ \begin{equation} u^Tx=|x||u^T|\cos{\alpha}=|x|\cos{\alpha}=|w| \end{equation} uTx=∣x∣∣uT∣cosα=∣x∣cosα=∣w∣ -
向量w,x,y之间的关系如下:
y + 2 w = x , 2 w = 2 ∣ w ∣ u = 2 u T x u \begin{equation} y+2w=x,2w=2|w|u=2u^Txu \end{equation} y+2w=x,2w=2∣w∣u=2uTxu -
因为 u T x u^Tx uTx为一个数,可以任意放位置
y + 2 u u T x = x → y = ( I − 2 u u T ) x \begin{equation} y+2uu^Tx=x\rightarrow y=(I-2uu^T)x \end{equation} y+2uuTx=x→y=(I−2uuT)x -
这里我们可以将H定义为如下:
y = H x ; H = I − 2 u u T ; y = ( I − 2 u u T ) x \begin{equation} y=Hx;H=I-2uu^T;y=(I-2uu^T)x \end{equation} y=Hx;H=I−2uuT;y=(I−2uuT)x -
Householder矩阵,对称性,正交性。
H = I − 2 u u T → H T = I − 2 u u T = H , H T H = I − 2 u u T − 2 u u T + 4 u u T u u T = I \begin{equation} H=I-2uu^T\rightarrow H^T=I-2uu^T=H,H^TH=I-2uu^T-2uu^T+4uu^Tuu^T=I \end{equation} H=I−2uuT→HT=I−2uuT=H,HTH=I−2uuT−2uuT+4uuTuuT=I
2.4 Hadamard矩阵
哈达玛(Hadamard)矩阵;
- H 2 , H 4 H_2,H_4 H2,H4
H 2 = [ 1 1 1 − 1 ] ; H 4 = [ H 2 H 2 H 2 − H 2 ] = [ 1 1 1 1 1 − 1 1 − 1 1 1 − 1 − 1 1 − 1 − 1 1 ] \begin{equation} H_2=\begin{bmatrix} 1&1\\\\ 1&-1 \end{bmatrix};H_4=\begin{bmatrix} H_2&H_2\\\\ H_2&-H_2 \end{bmatrix}= \begin{bmatrix} 1&1&1&1\\\\ 1&-1&1&-1\\\\ 1&1&-1&-1\\\\ 1&-1&-1&1\\\\ \end{bmatrix} \end{equation} H2= 111−1 ;H4= H2H2H2−H2 = 11111−11−111−1−11−1−11
2.4 小波矩阵
我们知道小波矩阵也是一个正交矩阵
2.5 傅里叶级数矩阵
我们这里要引入复数i,这里先定义,后面再详细展开
F 4 = [ 1 1 1 1 1 i i 2 i 3 1 i 2 i 4 i 6 1 i 3 i 6 i 9 ] \begin{equation} F_4=\begin{bmatrix} 1&1&1&1\\\\ 1&i&i^2&i^3\\\\ 1&i^2&i^4&i^6\\\\ 1&i^3&i^6&i^9 \end{bmatrix} \end{equation} F4=
11111ii2i31i2i4i61i3i6i9
- 注意,这里第二列和第四列之间正交需要取共轭复数,具体如下:
f 1 = [ 1 i i 2 i 3 ] = [ 1 i − 1 − i ] → f 1 H = [ 1 − i − 1 i ] \begin{equation} f_1=\begin{bmatrix} 1\\\\i\\\\i^2\\\\i^3 \end{bmatrix}=\begin{bmatrix} 1\\\\i\\\\-1\\\\-i \end{bmatrix}\rightarrow f_1^H=\begin{bmatrix}1&-i&-1&i\end{bmatrix} \end{equation} f1= 1ii2i3 = 1i−1−i →f1H=[1−i−1i]
f 4 = [ 1 i 3 i 6 i 9 ] = [ 1 − i 1 − i ] → f 1 H f 4 = [ 1 − i − 1 i ] [ 1 − i 1 − i ] = 1 + i 2 − 1 − i 2 = 0 \begin{equation} f_4=\begin{bmatrix} 1\\\\i^3\\\\i^6\\\\i^9 \end{bmatrix}=\begin{bmatrix} 1\\\\-i\\\\1\\\\-i \end{bmatrix}\rightarrow f_1^Hf_4=\begin{bmatrix}1&-i&-1&i\end{bmatrix}\begin{bmatrix} 1\\\\-i\\\\1\\\\-i \end{bmatrix}=1+i^2-1-i^2=0 \end{equation} f4= 1i3i6i9 = 1−i1−i →f1Hf4=[1−i−1i] 1−i1−i =1+i2−1−i2=0 - 同理,可以逐个计算得到 F 4 F_4 F4的列向量相互正交。
3. python 代码
import numpy as np
from scipy.linalg import hadamard
np.set_printoptions(suppress=True, precision=3)
class FourierMatrix(object):
def __init__(self, N):
self.N = N
self.result = np.zeros((self.N, self.N))
self.get_result()
def get_result(self):
self.result = np.fft.fft(np.eye(self.N))
return self.result
if __name__ == "__main__":
my_code = 0
H2 = hadamard(2)
H4 = hadamard(4)
print(f"H2=\n{H2}")
print(f"H4=\n{H4}")
N = 4
F4 = FourierMatrix(N)
print(f"F4=\n{F4.result}")
- 结果:
H2=
[[ 1 1]
[ 1 -1]]
H4=
[[ 1 1 1 1]
[ 1 -1 1 -1]
[ 1 1 -1 -1]
[ 1 -1 -1 1]]
F4=
[[ 1.+0.j 1.+0.j 1.+0.j 1.+0.j]
[ 1.+0.j 0.-1.j -1.+0.j 0.+1.j]
[ 1.+0.j -1.+0.j 1.+0.j -1.+0.j]
[ 1.+0.j 0.+1.j -1.+0.j 0.-1.j]]
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐



所有评论(0)