[R]Kronecker積を求める

R で Kronecker 積をもとめるには kronecker 関数を利用すればよい。
ふとやってみたくなったので、リファレンス片手にやってみた。

Essential Mathematics for Political and Social Research” by Jeff Gill という本の例から(P.122)

Properties Of Kronecker Products

まずは、例で用いる行列を定義

> X <- matrix(c(1,2,1,5), 2,2)
> Y <- matrix(-1, 1, -3, 1), 2,2)
> W <- matrix(c(2,3,-2,0), 2,2)
> X
[,1] [,2]
[1,]    1    1
[2,]    2    5
> Y
[,1] [,2]
[1,]   -1   -3
[2,]    1    1
> W
[,1] [,2]
[1,]    2   -2
[2,]    3    0
> kronecker(X, Y)
[,1] [,2] [,3] [,4]
[1,]   -1   -3   -1   -3
[2,]    1    1    1    1
[3,]   -2   -6   -5  -15
[4,]    2    2    5    5

Trace tr(XxY) = trX x trY

> sum(diag(kronecker(X, Y)))
[1] 0
> sum(diag(X))
[1] 6
> sum(diag(Y))
[1] 0
> kronecker(sum(diag(X)), sum(diag(Y)))
[1] 0

Transpose tr(X x Y) = tr(X) x tr(Y)

> t(kronecker(X, Y))
[,1] [,2] [,3] [,4]
[1,]   -1    1   -2    2
[2,]   -3    1   -6    2
[3,]   -1    1   -5    5
[4,]   -3    1  -15    5
> kronecker(t(X), t(Y))
[,1] [,2] [,3] [,4]
[1,]   -1    1   -2    2
[2,]   -3    1   -6    2
[3,]   -1    1   -5    5
[4,]   -3    1  -15    5

Inversion (X x Y) ^-1 = X ^ -1 x Y ^ -1

> solve(X)
[,1]       [,2]
[1,]  1.6666667 -0.3333333
[2,] -0.6666667  0.3333333
> solve(Y)
[,1] [,2]
[1,]  0.5  1.5
[2,] -0.5 -0.5
> X_inv <- solve(X)
> Y_inv <- solve(Y)
> X_inv
[,1]       [,2]
[1,]  1.6666667 -0.3333333
[2,] -0.6666667  0.3333333
> Y_inv
[,1] [,2]
[1,]  0.5  1.5
[2,] -0.5 -0.5
> solve(kronecker(X, Y))
[,1]       [,2]       [,3]       [,4]
[1,]  0.8333333  2.5000000 -0.1666667 -0.5000000
[2,] -0.8333333 -0.8333333  0.1666667  0.1666667
[3,] -0.3333333 -1.0000000  0.1666667  0.5000000
[4,]  0.3333333  0.3333333 -0.1666667 -0.1666667
> kronecker(solve(X), solve(Y))
[,1]       [,2]       [,3]       [,4]
[1,]  0.8333333  2.5000000 -0.1666667 -0.5000000
[2,] -0.8333333 -0.8333333  0.1666667  0.1666667
[3,] -0.3333333 -1.0000000  0.1666667  0.5000000
[4,]  0.3333333  0.3333333 -0.1666667 -0.1666667

Products (X x Y) (W x Z) = XW x YZ

> kronecker(X, Y) %*% kronecker(W, Z)
[,1] [,2] [,3] [,4]
[1,]  -55   10   22   -4
[2,]   25  -10  -10    4
[3,] -209   38   44   -8
[4,]   95  -38  -20    8
> kronecker(X %*% W, Y %*% Z)
[,1] [,2] [,3] [,4]
[1,]  -55   10   22   -4
[2,]   25  -10  -10    4
[3,] -209   38   44   -8
[4,]   95  -38  -20    8

Associative (X x Y) x W = X x (Y X W)

> kronecker(kronecker(X, Y), W)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]   -2    2   -6    6   -2    2   -6    6
[2,]   -3    0   -9    0   -3    0   -9    0
[3,]    2   -2    2   -2    2   -2    2   -2
[4,]    3    0    3    0    3    0    3    0
[5,]   -4    4  -12   12  -10   10  -30   30
[6,]   -6    0  -18    0  -15    0  -45    0
[7,]    4   -4    4   -4   10  -10   10  -10
[8,]    6    0    6    0   15    0   15    0

> kronecker(X, kronecker(Y, W))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]   -2    2   -6    6   -2    2   -6    6
[2,]   -3    0   -9    0   -3    0   -9    0
[3,]    2   -2    2   -2    2   -2    2   -2
[4,]    3    0    3    0    3    0    3    0
[5,]   -4    4  -12   12  -10   10  -30   30
[6,]   -6    0  -18    0  -15    0  -45    0
[7,]    4   -4    4   -4   10  -10   10  -10
[8,]    6    0    6    0   15    0   15    0

Distributive (X + Y) x W = (X x W) + (Y + W)

> X + Y
[,1] [,2]
[1,]    0   -2
[2,]    3    6
> kronecker(X+Y, W)
[,1] [,2] [,3] [,4]
[1,]    0    0   -4    4
[2,]    0    0   -6    0
[3,]    6   -6   12  -12
[4,]    9    0   18    0
> kronecker(X, W) + kronecker(Y, W)
[,1] [,2] [,3] [,4]
[1,]    0    0   -4    4
[2,]    0    0   -6    0
[3,]    6   -6   12  -12
[4,]    9    0   18    0

メモ

  • 置換関数は t
  • 逆行列は solve
  • 対角成分のベクトルは diag
  • R には行列のトレースを求めるための専用の関数が存在しない模様。仕方なく、定義通りに sum(diag(matrix)) とした。
Advertisements
Tagged with: , , , ,
Posted in algorithm, R

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Archives

Error: Twitter did not respond. Please wait a few minutes and refresh this page.

%d bloggers like this: