仮想計算機構

IT業界と無縁な派遣社員のブログ

SLIB(Scheme)で行列の計算をする

準備

A = \begin{bmatrix}1 & 2\\ 3 & 4\end{bmatrix},\ B = \begin{bmatrix}5 & 6\\ 7 & 8\end{bmatrix}

gosh> (use slib)
gosh> (require 'determinant)
#t
gosh> (define A '((1 2) (3 4))) 
A
gosh> (define B '((5 6) (7 8))) 
B

行列式

|A| = 1\times 4 - 2\times 3 = -2
|B| = 5\times 8 - 6\times 7 = -2

gosh> (determinant A)
-2
gosh> (determinant B)                    
-2

転置行列

A^{T} = \begin{bmatrix}1 & 2\\ 3 & 4\end{bmatrix}^{T} = \begin{bmatrix}1 & 3\\ 2 & 4\end{bmatrix}

gosh> (transpose A)
((1 3) (2 4))

転置行列の行列式はもともとの行列の行列式に等しい*1ので、それを以下で確かめる。

gosh> (determinant (transpose A))
-2

A+B を計算する。

gosh> (matrix:sum A B)
((6 8) (10 12))

A-B を計算する。

gosh> (matrix:difference A B) 
((-4 -4) (-4 -4))

 AB = \begin{bmatrix}1 & 2\\ 3 & 4\end{bmatrix}\begin{bmatrix}5 & 6\\ 7 & 8\end{bmatrix}
=\begin{bmatrix}1\times 5 + 2\times 7 & 3\times 5 + 4\times 7\\ 1\times 6 + 2\times 8 & 3\times 6 + 4\times 8\end{bmatrix}

gosh> (matrix:product A B)    
((19 22) (43 50))

また、以下も成り立つ。*2

|AB|=|A|\cdot |B|

gosh> (determinant (matrix:product A B))
4
gosh> (* (determinant A) (determinant B)) 
4

逆行列

最後に逆行列
AA^{-1} = I を確かめる。

gosh> (matrix:inverse A)
((-2 1) (3/2 -1/2))
gosh> (matrix:product A (matrix:inverse A))
((1 0) (0 1))

また、|A|\cdot |A^{-1}|=|AA^{-1}|=|I|=1 より、|A^{-1}| = 1/|A| が成り立つ。

gosh> (determinant (matrix:inverse A))
-1/2
gosh> (/ 1 (determinant A)) 
-1/2

*1:斎藤正彦『線型代数入門』東京大学出版会(p.79)定理2.1

*2:同書(p.82)定理2.7