热线电话:13121318867

登录
2018-11-01 阅读量: 950
用R进行矩阵运算(3)

9矩阵的特征值与特征向量

矩阵 A 的谱分解为 A=UΛU',其中 Λ 是由 A 的特征值组成的对角矩阵,U 的列为 A 的

特征值对应的特征向量,在 R 中可以用函数 eigen()函数得到 U 和 Λ,

> args(eigen)

function (x, symmetric, only.values = FALSE, EISPACK = FALSE)

其中:x 为矩阵,symmetric 项指定矩阵 x 是否为对称矩阵,若不指定,系统将自动

检测 x 是否为对称矩阵。例如:

> A=diag(4)+1

> A

[,1] [,2] [,3] [,4]

[1,] 2 1 1 1

[2,] 1 2 1 1

[3,] 1 1 2 1

[4,] 1 1 1 2

> A.eigen=eigen(A,symmetric=T)

> A.eigen

$values

[1] 5 1 1 1

$vectors

[,1] [,2] [,3] [,4]

[1,] 0.5 0.8660254 0.000000e+00 0.0000000

[2,] 0.5 -0.2886751 -6.408849e-17 0.8164966

[3,] 0.5 -0.2886751 -7.071068e-01 -0.4082483

[4,] 0.5 -0.2886751 7.071068e-01 -0.4082483

> A.eigen$vectors%*%diag(A.eigen$values)%*%t(A.eigen$vectors)

[,1] [,2] [,3] [,4]

[1,] 2 1 1 1

[2,] 1 2 1 1

[3,] 1 1 2 1

[4,] 1 1 1 2

> t(A.eigen$vectors)%*%A.eigen$vectors

[,1] [,2] [,3] [,4]

[1,] 1.000000e+00 4.377466e-17 1.626303e-17 -5.095750e-18

[2,] 4.377466e-17 1.000000e+00 -1.694066e-18 6.349359e-18

[3,] 1.626303e-17 -1.694066e-18 1.000000e+00 -1.088268e-16

[4,] -5.095750e-18 6.349359e-18 -1.088268e-16 1.000000e+00

10 矩阵的 Choleskey 分解

对于正定矩阵 A,可对其进行 Choleskey 分解,即:A=P'P,其中 P 为上三角矩阵,在

R 中可以用函数 chol()进行 Choleskey 分解,例如:

> A

[,1] [,2] [,3] [,4]

[1,] 2 1 1 1

[2,] 1 2 1 1

[3,] 1 1 2 1

[4,] 1 1 1 2

> chol(A)

[,1] [,2] [,3] [,4]

[1,] 1.414214 0.7071068 0.7071068 0.7071068

[2,] 0.000000 1.2247449 0.4082483 0.4082483

[3,] 0.000000 0.0000000 1.1547005 0.2886751

[4,] 0.000000 0.0000000 0.0000000 1.1180340

> t(chol(A))%*%chol(A)

[,1] [,2] [,3] [,4]

[1,] 2 1 1 1

[2,] 1 2 1 1

[3,] 1 1 2 1

[4,] 1 1 1 2

> crossprod(chol(A),chol(A))

[,1] [,2] [,3] [,4]

[1,] 2 1 1 1

[2,] 1 2 1 1

[3,] 1 1 2 1

[4,] 1 1 1 2

若矩阵为对称正定矩阵,可以利用 Choleskey 分解求行列式的值,如:

> prod(diag(chol(A))^2)

[1] 5

> det(A)

[1] 5

若矩阵为对称正定矩阵,可以利用Choleskey分解求矩阵的逆,这时用函数

chol2inv(),这种用法更有效。如:

> chol2inv(chol(A))

[,1] [,2] [,3] [,4]

[1,] 0.8 -0.2 -0.2 -0.2

[2,] -0.2 0.8 -0.2 -0.2

[3,] -0.2 -0.2 0.8 -0.2

[4,] -0.2 -0.2 -0.2 0.8

> solve(A)

[,1] [,2] [,3] [,4]

[1,] 0.8 -0.2 -0.2 -0.2

[2,] -0.2 0.8 -0.2 -0.2

[3,] -0.2 -0.2 0.8 -0.2

[4,] -0.2 -0.2 -0.2 0.8

11 矩阵奇异值分解

A为m×n矩阵,rank(A)= r, 可以分解为:A=UDV',其中U'U=V'V=I。在R中可以用函数

scd()进行奇异值分解,例如:

> A=matrix(1:18,3,6)

> A

[,1] [,2] [,3] [,4] [,5] [,6]

[1,] 1 4 7 10 13 16

[2,] 2 5 8 11 14 17

[3,] 3 6 9 12 15 18

> svd(A)

$d

[1] 4.589453e+01 1.640705e+00 3.627301e-16

$u

[,1] [,2] [,3]

[1,] -0.5290354 0.74394551 0.4082483

[2,] -0.5760715 0.03840487 -0.8164966

[3,] -0.6231077 -0.66713577 0.4082483

$v

[,1] [,2] [,3]

[1,] -0.07736219 -0.7196003 -0.18918124

[2,] -0.19033085 -0.5089325 0.42405898

[3,] -0.30329950 -0.2982646 -0.45330031

[4,] -0.41626816 -0.0875968 -0.01637004

[5,] -0.52923682 0.1230711 0.64231130

[6,] -0.64220548 0.3337389 -0.40751869

> A.svd=svd(A)

> A.svd$u%*%diag(A.svd$d)%*%t(A.svd$v)

[,1] [,2] [,3] [,4] [,5] [,6]

[1,] 1 4 7 10 13 16

[2,] 2 5 8 11 14 17

[3,] 3 6 9 12 15 18

> t(A.svd$u)%*%A.svd$u

[,1] [,2] [,3]

[1,] 1.000000e+00 -1.169312e-16 -3.016793e-17

[2,] -1.169312e-16 1.000000e+00 -3.678156e-17

[3,] -3.016793e-17 -3.678156e-17 1.000000e+00

> t(A.svd$v)%*%A.svd$v

[,1] [,2] [,3]

[1,] 1.000000e+00 8.248068e-17 -3.903128e-18

[2,] 8.248068e-17 1.000000e+00 -2.103352e-17

[3,] -3.903128e-18 -2.103352e-17 1.000000e+00

0.0000
5
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子