Content

2組の正方行列(a、b)から様々な行列演算を確認してみましょう。

\[ a = \left[ \begin{array}{rrr} 1 & 2\\ 3 & 4 \end{array} \right] \,,\, b = \left[ \begin{array}{rrr} 4 & 3\\ 2 & 1 \end{array} \right] \]

# 行列の作成
a <- rbind(c(1, 2), c(3, 4))
b <- rbind(c(4, 3), c(2, 1))
a
cat("-------------------------\n")
b
     [,1] [,2]
[1,]    1    2
[2,]    3    4
-------------------------
     [,1] [,2]
[1,]    4    3
[2,]    2    1

# 行列の和
a + b
     [,1] [,2]
[1,]    5    5
[2,]    5    5

# 行列の差
a - b
     [,1] [,2]
[1,]   -3   -1
[2,]    1    3

# 行列の積
a %*% b
     [,1] [,2]
[1,]    8    5
[2,]   20   13

# a * b としない(要素毎の積となる)
a * b
     [,1] [,2]
[1,]    4    6
[2,]    6    4

# クロネッカー積
a %x% b
cat("-------------------------\n")
b %x% a
     [,1] [,2] [,3] [,4]
[1,]    4    3    8    6
[2,]    2    1    4    2
[3,]   12    9   16   12
[4,]    6    3    8    4
-------------------------
     [,1] [,2] [,3] [,4]
[1,]    4    8    3    6
[2,]   12   16    9   12
[3,]    2    4    1    2
[4,]    6    8    3    4

# 転置行列
t(x = a)
cat("-------------------------\n")
t(x = b)
     [,1] [,2]
[1,]    1    3
[2,]    2    4
-------------------------
     [,1] [,2]
[1,]    4    2
[2,]    3    1

# 逆行列
solve(a = a)
cat("-------------------------\n")
solve(a = a) %*% a
cat("-------------------------\n")
solve(a = b)
cat("-------------------------\n")
solve(a = b) %*% b
     [,1] [,2]
[1,] -2.0  1.0
[2,]  1.5 -0.5
-------------------------
     [,1]         [,2]
[1,]    1 4.440892e-16
[2,]    0 1.000000e+00
-------------------------
     [,1] [,2]
[1,] -0.5  1.5
[2,]  1.0 -2.0
-------------------------
     [,1] [,2]
[1,]    1    0
[2,]    0    1

# 逆行列 引数aに正方行列以外を入力した場合のエラーメッセージ
c <- cbind(a, c(10, 10))
c
solve(a = c)
Error in solve.default(a = c): 'a' (2 x 3) must be square
     [,1] [,2] [,3]
[1,]    1    2   10
[2,]    3    4   10

# 単位行列 対角成分:1
diag(x = 4)
     [,1] [,2] [,3] [,4]
[1,]    1    0    0    0
[2,]    0    1    0    0
[3,]    0    0    1    0
[4,]    0    0    0    1

# 単位行列 対角成分:1,2,3,4
diag(x = 1:4)
     [,1] [,2] [,3] [,4]
[1,]    1    0    0    0
[2,]    0    2    0    0
[3,]    0    0    3    0
[4,]    0    0    0    4

# 固有値と固有ベクトル
result <- eigen(x = a)
result$values
cat("-------------------------\n")
result$vectors
cat("-------------------------\n")
# 確認
a %*% result$vectors[, 1, drop = F] - result$values[1] * result$vectors[, 1, drop = F]
cat("-------------------------\n")
a %*% result$vectors[, 2, drop = F] - result$values[2] * result$vectors[, 2, drop = F]
[1]  5.3722813 -0.3722813
-------------------------
           [,1]       [,2]
[1,] -0.4159736 -0.8245648
[2,] -0.9093767  0.5657675
-------------------------
              [,1]
[1,] -4.440892e-16
[2,]  0.000000e+00
-------------------------
             [,1]
[1,] 0.000000e+00
[2,] 5.551115e-17

# QR分解
result <- qr(x = a)
result
cat("-------------------------\n")
# 確認
qr.Q(result)
cat("-------------------------\n")
qr.R(result)
cat("-------------------------\n")
qr.Q(result) %*% qr.R(result)
$qr
           [,1]       [,2]
[1,] -3.1622777 -4.4271887
[2,]  0.9486833 -0.6324555

$rank
[1] 2

$qraux
[1] 1.3162278 0.6324555

$pivot
[1] 1 2

attr(,"class")
[1] "qr"
-------------------------
           [,1]       [,2]
[1,] -0.3162278 -0.9486833
[2,] -0.9486833  0.3162278
-------------------------
          [,1]       [,2]
[1,] -3.162278 -4.4271887
[2,]  0.000000 -0.6324555
-------------------------
     [,1] [,2]
[1,]    1    2
[2,]    3    4

Asset Management Consulting Corporation