行列#

行列の掛け算#

\[\begin{split} A = \begin{pmatrix} a & b \\ c & d \\ \end{pmatrix}\\ B = \begin{pmatrix} e\\ f\\ \end{pmatrix} \\ A \cdot B = \begin{pmatrix} a*e + b*f\\ c*e+d*f\\ \end{pmatrix} \end{split}\]
x = np.array([1,2])
w = np.array([[4,8],[6,3]])
x.dot(w)
# array([16,14])

Note

  • 計算できる条件:Aの列数=Bの行数

  • 結果.shape=(前の行数, 後ろの列数)

行列式#

\[\begin{split} A = \begin{pmatrix} a&b\\ c&d \end{pmatrix}の時、Aの行列式を\\ \det A、 |A|、 \begin{vmatrix} a&b\\ c&d \end{vmatrix}と表し、\\ |A|=a*d-b*c \end{split}\]

求め方#

2x2行列#

\[\begin{split} \begin{vmatrix} a&b\\ c&d \end{vmatrix} =a*d-b*c \end{split}\]

3x3行列(サラスの公式)#

\[\begin{split} \begin{vmatrix} a_{11} & a_{12} & a_{13}\\ a_{21} & a_{22} & a_{23}\\ a_{31} & a_{32} & a_{33} \end{vmatrix}\\ =a_{11} a_{22} a_{33}+a_{12} a_{23}a_{31}+a_{13}a_{32}a_{21}\\ -a_{13}a_{22} a_{31} -a_{12}a_{21}a_{33} -a_{11}a_{32} a_{23} \end{split}\]
np.linalg.det(A)

交換性#

行列の2列を入れ替えた場合、または2行を入れ替えた場合、行列式は元の行列式の-1倍になる。

\[\begin{split} \begin{vmatrix} a_{11} & a_{12} & a_{13}\\ a_{21} & a_{22} & a_{23}\\ a_{31} & a_{32} & a_{33} \end{vmatrix} = - \begin{vmatrix} a_{21} & a_{22} & a_{23}\\ a_{11} & a_{12} & a_{13}\\ a_{31} & a_{32} & a_{33} \end{vmatrix} \end{split}\]

逆行列#

元の行列とかけると単位行列になる行列、すなわち \(A^{-1} \cdot A = I\)を満たす\(A^{-1}\)\(A\)の逆行列という。

ただし、行列式\(|A|=0\)の時は\(A\)は逆行列を持たない。

求め方#

2x2行列の逆行列#

2x2の行列\(A=\begin{pmatrix}a&b\\c&d\end{pmatrix}\)の逆行列は $\( A^{-1} = \frac{1}{ad-bc} \begin{pmatrix} d&-b\\ -c&a \end{pmatrix} \)$

掃き出し法#

numpy#

import numpy as np
import numpy.linalg as LA

A = np.array([[3,5],[2,-2]])
LA.inv(A)
#=>array([[ 0.125 ,  0.3125],
#       [ 0.125 , -0.1875]])

対象行列#

転置しても元の行列とおなじになる行列のこと

\[\begin{split} A = A^T\\ \end{split}\]

対象行列の例

\[\begin{split} \begin{pmatrix} 1 & 2\\ 2 & 3 \end{pmatrix}\\ 単位行列 \begin{pmatrix} 1 & 0 & 0 & 0\\ 0 & 1 & 0 & 0\\ 0 & 0 & 1 & 0\\ 0 & 0 & 0 & 1\\ \end{pmatrix}\\ \end{split}\]

直行行列#

転置すると元の行列の逆行列になる行列を、直行行列と言う。

\[\begin{split} R^T = R^{-1}\\ RR^T = I \end{split}\]

対角行列#

対角成分以外が0な行列

\[\begin{split} \begin{pmatrix} 8 & 0 & 0 & 0\\ 0 & 0 & 0 & 0\\ 0 & 0 & 3 & 0\\ 0 & 0 & 0 & 9\\ \end{pmatrix} \end{split}\]

固有値・固有値ベクトル#

あるn次正方行列\(A\)に対し、

\[Ax = \lambda x\]

を満たすn次元ベクトル\(x (x \neq 0)\)が存在する時、

\[\begin{split} \lambda : Aの固有値\\ x : \lambdaに対する固有ベクトル \end{split}\]

と言う

求め方#

固有値#

\[\begin{split} Ax = \lambda xを満たすx,\lambdaを求める\\ 単位ベクトルEを使い変形すると\\ (A-\lambda E)=0\\ すなわち\\ |A-\lambda E|=0\\ を考える \end{split}\]

固有ベクトル#

\[\begin{split} \lambda = \lambda_1, \lambda_2, \dots を代入して、\\ (A-\lambda_i E)x=0\\ を満たすx_iを求める。 \end{split}\]

例:

\[\begin{split} \begin{pmatrix}3&2&0\\0&2&0\\0&0&1\end{pmatrix}の固有値、固有ベクトルは? \end{split}\]
\[\begin{split} \begin{pmatrix}3-\lambda & 2 & 0\\ 0 & 2 - \lambda & 0\\ 0 & 0 & 1-\lambda \end{pmatrix}=0 \end{split}\]
\[\begin{split} (3-\lambda)(2-\lambda)(1-\lambda)=0\\ \therefore \lambda=3,2,1 \end{split}\]

固有ベクトルは、

  1. \(\lambda=1\)の時

\[ \begin{align}\begin{aligned}\begin{split} (A-\lambda_1 E)x=0\\ \begin{pmatrix} 3-1 & 2 & 0\\ 0 & 2-1 & 0\\ 0 & 0 & 1-1 \end{pmatrix} \begin{pmatrix} x_1\\ x_2\\ x_3 \end{pmatrix} =0\\\end{split}\\\begin{split}\begin{cases} 2x_1+2x_2=0 \therefore x_1=-x_2\\ x_2=0 \therefore x_2=0\\ 0=0\\ \end{cases}\\ \therefore \lambda=1の時、固有値は \begin{pmatrix} 0\\0\\1 \end{pmatrix} の定数倍 \end{split}\end{aligned}\end{align} \]
  1. \(\lambda=2\)の時

\[ \begin{align}\begin{aligned}\begin{split} (A- 2 E)x=0\\ \begin{pmatrix} 3-2 & 2 & 0\\ 0 & 2-2 & 0\\ 0 & 0 & 1-2 \end{pmatrix} \begin{pmatrix} x_1\\ x_2\\ x_3 \end{pmatrix} =0\\\end{split}\\\begin{split}\begin{cases} x_1+2x_2=0 \therefore x_1=-2x_2\\ 0=0\\ -x_3=0 \therefore x_3=0\\ \end{cases}\\ 任意の定数Sを用いると\\\end{split}\\\begin{split}\begin{pmatrix} x_1\\ x_2\\ x_3\\ \end{pmatrix} = \begin{pmatrix} -2S\\ S\\ 0\\ \end{pmatrix} =S \begin{pmatrix} -2\\ 1\\ 0\\ \end{pmatrix} \\\end{split}\\\begin{split}\therefore \lambda=2の時、固有値は \begin{pmatrix} -2\\1\\0 \end{pmatrix} の定数倍 \end{split}\end{aligned}\end{align} \]
  1. \(\lambda=3\)の時

\[ \begin{align}\begin{aligned}\begin{split} (A- 3E)x=0\\ \begin{pmatrix} 3-3 & 2 & 0\\ 0 & 2-3 & 0\\ 0 & 0 & 1-3 \end{pmatrix} \begin{pmatrix} x_1\\ x_2\\ x_3 \end{pmatrix} =0\\\end{split}\\\begin{split}\begin{cases} 2x_2=0 \therefore x_2=0\\ -x_2=0\\ -2x_3=0 \therefore x_3=0\\ \end{cases}\\\end{split}\\\begin{split}\therefore \lambda=3の時、固有値は \begin{pmatrix} 1\\0\\0 \end{pmatrix} の定数倍 \end{split}\end{aligned}\end{align} \]

numpy#

import numpy as np
import numpy.linalg as LA

A = np.array([
	[3, 2, 0],
	[0, 2, 0],
	[0, 0, 1]
])
w, v = LA.eig(A)
# 固有値
print(w)
#array([3., 2., 1.])
# 固有ベクトル; 縦に見る
print(v)
#array([[ 1.        , -0.89442719,  0.        ],
#       [ 0.        ,  0.4472136 ,  0.        ],
#       [ 0.        ,  0.        ,  1.        ]])

固有値分解#

正方行列\(A\)を、

  • 固有値を対角線上に並べた固有値行列\(\Lambda\)

  • 固有ベクトルを並べた行列\(V\) を使ってこのように表すこと

\[A=V \Lambda V^{-1}\]

求め方#

numpy#

import numpy as np
import numpy.linalg as LA

A = np.array([
	[3, 2, 0],
	[0, 2, 0],
	[0, 0, 1]
])
w, v = LA.eig(A)

# 固有値
print(w)
#array([3., 2., 1.])

# 固有値ベクトル
Q = np.diag(w)
print(Q)
#array([[3., 0., 0.],
#       [0., 2., 0.],
#       [0., 0., 1.]])

# 固有ベクトル
print(v)
#array([[ 1.        , -0.89442719,  0.        ],
#       [ 0.        ,  0.4472136 ,  0.        ],
#       [ 0.        ,  0.        ,  1.        ]])

# 固有ベクトル(右)
R = LA.inv(v) # 逆行列を得る
print(R)
#array([[1.        , 2.        , 0.        ],
#       [0.        , 2.23606798, 0.        ],
#       [0.        , 0.        , 1.        ]])

numpyで得らた結果を使ってAを固有値分解すると、

\[\begin{split} A=vQR= \begin{pmatrix} 1 & -2 & 0\\ 0 & 1 & 0\\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} 3 & 0 & 0\\ 0 & 2 & 0\\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} 1 & 2 & 0\\ 0 & \sqrt{5} & 0\\ 0 & 0 & 1 \end{pmatrix} \end{split}\]

対角化#

正方行列\(A\)を、\(A\)の固有ベクトルを列に並べた行列\(P\)とその逆行列\(P^{-1}\)で挟み、固有値がならんだ対角行列\(D\)に線形変換する事。

対角化のメリット#

  • 計算が楽になる

対角化可能#

次数と同じ数の固有ベクトルが互いに一次独立

#

\( A = \begin{pmatrix} 1 & 2 & 0\\ 0 & 3 & 0\\ 2 & -4 & 2 \end{pmatrix} \) を対角化せよ。

import numpy as np
import numpy.linalg as LA
A = np.array([
	[1,2,0],
	[0,3,0],
	[2,-4,2]
	])
w,P =LA.eig(A)

# Aを対角化した行列D
D = np.diag(w)
D
array([[2., 0., 0.],
       [0., 1., 0.],
       [0., 0., 3.]])
# 固有ベクトルを列ベクトルとして並べた行列P
P
array([[ 0.        ,  0.4472136 ,  0.40824829],
       [ 0.        ,  0.        ,  0.40824829],
       [ 1.        , -0.89442719, -0.81649658]])
# 列毎に整数化
P = np.array([
    [0, 1, 1],
    [0, 0, 1],
    [1, -2, -2]
])
# Pの逆行列
P1 = LA.inv(P)
P1
array([[ 2.,  0.,  1.],
       [ 1., -1.,  0.],
       [ 0.,  1.,  0.]])

よって、Aを対角化すると

\[ \begin{align}\begin{aligned}\begin{split} P^{-1}AP = D\\\end{split}\\\begin{split}\begin{pmatrix} 2 & 0 & 1\\ 1 & 1 & 0\\ 0 & -1 & 0 \end{pmatrix}\end{split}\\\begin{split}\begin{pmatrix} 1 & 2 & 0\\ 0 & 3 & 0\\ 2 & -4 & 2 \end{pmatrix}\end{split}\\\begin{split}\begin{pmatrix} 0 & 1 & 1\\ 0 & 0 & 1\\ 1 & -2 & -2 \end{pmatrix}\end{split}\\=\\\begin{split}\begin{pmatrix} 2 & 0 & 0\\ 0 & 1 & 0\\ 0 & 0 & 3 \end{pmatrix}\end{split}\end{aligned}\end{align} \]

\(X=\begin{pmatrix}3&1\\2&2\end{pmatrix}\)のn乗は?

import numpy as np
import numpy.linalg as LA
A = np.array([
	[3,1],
	[2,2]
	])
w,v=LA.eig(A)
w
array([4., 1.])

Aを対角化した物がD

D=np.diag(w)
D
array([[4., 0.],
       [0., 1.]])
v
array([[ 0.70710678, -0.4472136 ],
       [ 0.70710678,  0.89442719]])

vの整数倍であれば問題ない。計算しやすいように、縦の要素毎にきりの良い数字に変換する

\[\begin{split} V=\begin{pmatrix} 1 & 1\\ 1 & -2 \end{pmatrix} \end{split}\]
# 整数化したv
V = np.array([[
	[1, 1],
	[1, -2]
]])

# 逆行列を求めるコマンド
V1 = LA.inv(V)
V1
array([[[ 0.66666667,  0.33333333],
        [ 0.33333333, -0.33333333]]])

\(V^{-1}\)の変換は行列全体に対して係数を掛ける方法っぽい

\[\begin{split} V^{-1}= \dfrac{1}{3} \begin{pmatrix} 2 & 1\\ 1 & -1 \end{pmatrix} \end{split}\]
# 検算
V1 = np.array([
	[2, 1],
	[1, -1]
])

1/3 * V @ D @ V1
array([[[3., 1.],
        [2., 2.]]])
\[\begin{split} A=V \Lambda V^{-1}\\ = \frac{1}{3} \begin{pmatrix} 1&1\\ 1&-2 \end{pmatrix} \begin{pmatrix} 4&0\\ 0&1 \end{pmatrix} \begin{pmatrix} 2&1\\ 1&-1 \end{pmatrix} \\ A^n =V \Lambda^n V^{-1}\\ = \frac{1}{3} \begin{pmatrix} 1&1\\ 1&-2 \end{pmatrix} \begin{pmatrix} 4&0\\ 0&1 \end{pmatrix}^n \begin{pmatrix} 2&1\\ 1&-1 \end{pmatrix}\\ = \frac{1}{3} \begin{pmatrix} 1&1\\ 1&-2 \end{pmatrix} \begin{pmatrix} 4^n&0\\ 0&1^n \end{pmatrix} \begin{pmatrix} 2&1\\ 1&-1 \end{pmatrix}\\ =\frac{1}{3} \begin{pmatrix} 4^n & 1\\ 4^n & -2 \end{pmatrix} \begin{pmatrix} 2&1\\ 1&-1 \end{pmatrix}\\ =\frac{1}{3} \begin{pmatrix} 2*4^n+1 & 4^n-1\\ 2*4^n-2 & 4^n+2 \end{pmatrix}\end{split}\]

特異値分解#

Singular Value Decomposition SVD

次元削減のために用いられる手法 = 長方形行列の固有値分解

手順#

任意の行列\(A\)\(A=USV^T\)に特異値分解してみる

\[\begin{split} A = \begin{pmatrix} 1&2&3\\ 3&2&1 \end{pmatrix} \end{split}\]
# linalgモジュールはLAとしてimportする
import numpy as np
import numpy.linalg as LA

# 行列Aを定義する
a = np.array([
              [1,2,3],
              [3,2,1]
])

# AA^Tを求める
AAT = np.dot(a, a.T)
AAT
array([[14, 10],
       [10, 14]])
# 特異値分解のコマンド
U, S, Vt = LA.svd(a, full_matrices=True)
# 左特異値行列
U
array([[-0.70710678, -0.70710678],
       [-0.70710678,  0.70710678]])
# 特異値
S
array([4.89897949, 2.        ])
# 対角行列にする
S = np.diag(S)
S
array([[4.89897949, 0.        ],
       [0.        , 2.        ]])
# 左特異値行列の転地
Vt
array([[-5.77350269e-01, -5.77350269e-01, -5.77350269e-01],
       [ 7.07106781e-01,  1.65486686e-16, -7.07106781e-01],
       [ 4.08248290e-01, -8.16496581e-01,  4.08248290e-01]])
# shapeをそろえる
S2 = np.pad(S, ((0, 0), (0, 1)))
S2
array([[4.89897949, 0.        , 0.        ],
       [0.        , 2.        , 0.        ]])
# 検算
U @ S2 @ Vt
array([[1., 2., 3.],
       [3., 2., 1.]])

よって、

\[\begin{split} U = \begin{pmatrix} - \dfrac{1}{\sqrt{2}} & - \dfrac{1}{\sqrt{2}}\\ - \dfrac{1}{\sqrt{2}} & \dfrac{1}{\sqrt{2}} \end{pmatrix} \end{split}\]
\[\begin{split} S=\begin{pmatrix} 2\sqrt{6} & 0 &0\\ 0 & 2 & 0 \end{pmatrix} \end{split}\]
\[\begin{split} V^T = \begin{pmatrix} \dfrac{1}{\sqrt{3}} & \dfrac{1}{\sqrt{3}} & \dfrac{1}{\sqrt{3}} \\ - \dfrac{1}{\sqrt{2}} & 0 & \dfrac{1}{\sqrt{2}}\\ \dfrac{1}{\sqrt{6}} & -\dfrac{2}{\sqrt{6}} & \dfrac{1}{\sqrt{6}} & \end{pmatrix} \end{split}\]