热线电话:13121318867

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

12 矩阵QR分解

A为m×n矩阵可以进行QR分解,A=QR,其中:Q'Q=I,在R中可以用函数qr()进行

QR分解,例如:

> A=matrix(1:16,4,4)

> qr(A)

$qr

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

[1,] -5.4772256 -12.7801930 -2.008316e+01 -2.738613e+01

[2,] 0.3651484 -3.2659863 -6.531973e+00 -9.797959e+00

[3,] 0.5477226 -0.3781696 2.641083e-15 2.056562e-15

[4,] 0.7302967 -0.9124744 8.583032e-01 -2.111449e-16

$rank

[1] 2

$qraux

[1] 1.182574e+00 1.156135e+00 1.513143e+00 2.111449e-16

$pivot

[1] 1 2 3 4

attr(,"class")

[1] "qr"

rank项返回矩阵的秩,qr项包含了矩阵Q和R的信息,要得到矩阵Q和R,可以用函数

qr.Q()和qr.R()作用qr()的返回结果,例如:

> qr.R(qr(A))

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

[1,] -5.477226 -12.780193 -2.008316e+01 -2.738613e+01

[2,] 0.000000 -3.265986 -6.531973e+00 -9.797959e+00

[3,] 0.000000 0.000000 2.641083e-15 2.056562e-15

[4,] 0.000000 0.000000 0.000000e+00 -2.111449e-16

> qr.Q(qr(A))

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

[1,] -0.1825742 -8.164966e-01 -0.4000874 -0.37407225

[2,] -0.3651484 -4.082483e-01 0.2546329 0.79697056

[3,] -0.5477226 -8.131516e-19 0.6909965 -0.47172438

[4,] -0.7302967 4.082483e-01 -0.5455419 0.04882607

> qr.Q(qr(A))%*%qr.R(qr(A))

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

[1,] 1 5 9 13

[2,] 2 6 10 14

[3,] 3 7 11 15

[4,] 4 8 12 16

> t(qr.Q(qr(A)))%*%qr.Q(qr(A))

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

[1,] 1.000000e+00 -1.457168e-16 -6.760001e-17 -7.659550e-17

[2,] -1.457168e-16 1.000000e+00 -4.269046e-17 7.011739e-17

[3,] -6.760001e-17 -4.269046e-17 1.000000e+00 -1.596437e-16

[4,] -7.659550e-17 7.011739e-17 -1.596437e-16 1.000000e+00

> qr.X(qr(A))

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

[1,] 1 5 9 13

[2,] 2 6 10 14

[3,] 3 7 11 15

[4,] 4 8 12 16

13 矩阵广义逆(Moore-Penrose)

n×m矩阵A+称为m×n矩阵A的Moore-Penrose逆,如果它满足下列条件:

① A A+A=A;②A+A A+= A+;③(A A+)H=A A+;④(A+A)H= A+A

在R的MASS包中的函数ginv()可计算矩阵A的Moore-Penrose逆,例如:

library(“MASS”)

> A

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

[1,] 1 5 9 13

[2,] 2 6 10 14

[3,] 3 7 11 15

[4,] 4 8 12 16

> ginv(A)

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

[1,] -0.285 -0.1075 0.07 0.2475

[2,] -0.145 -0.0525 0.04 0.1325

[3,] -0.005 0.0025 0.01 0.0175

[4,] 0.135 0.0575 -0.02 -0.0975

验证性质1:

> A%*%ginv(A)%*%A

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

[1,] 1 5 9 13

[2,] 2 6 10 14

[3,] 3 7 11 15

[4,] 4 8 12 16

验证性质2:

> ginv(A)%*%A%*%ginv(A)

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

[1,] -0.285 -0.1075 0.07 0.2475

[2,] -0.145 -0.0525 0.04 0.1325

[3,] -0.005 0.0025 0.01 0.0175

[4,] 0.135 0.0575 -0.02 -0.0975

验证性质3:

> t(A%*%ginv(A))

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

[1,] 0.7 0.4 0.1 -0.2

[2,] 0.4 0.3 0.2 0.1

[3,] 0.1 0.2 0.3 0.4

[4,] -0.2 0.1 0.4 0.7

> A%*%ginv(A)

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

[1,] 0.7 0.4 0.1 -0.2

[2,] 0.4 0.3 0.2 0.1

[3,] 0.1 0.2 0.3 0.4

[4,] -0.2 0.1 0.4 0.7

验证性质4:

> t(ginv(A)%*%A)

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

[1,] 0.7 0.4 0.1 -0.2

[2,] 0.4 0.3 0.2 0.1

[3,] 0.1 0.2 0.3 0.4

[4,] -0.2 0.1 0.4 0.7

> ginv(A)%*%A

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

[1,] 0.7 0.4 0.1 -0.2

[2,] 0.4 0.3 0.2 0.1

[3,] 0.1 0.2 0.3 0.4

[4,] -0.2 0.1 0.4 0.7

14 矩阵Kronecker积

n×m矩阵A与h×k矩阵B的kronecker积为一个nh×mk维矩阵,公式为:

11 1

1

n

m n h k

m mn mh nk

a a

a a

´ ´

´

æ ö

ç ÷

Ä = ç ÷

ç ÷

è ø

B B

A B

B B

  

在R中kronecker积可以用函数kronecker()来计算,例如:

> A=matrix(1:4,2,2)

> B=matrix(rep(1,4),2,2)

> A

[,1] [,2]

[1,] 1 3

[2,] 2 4

> B

[,1] [,2]

[1,] 1 1

[2,] 1 1

> kronecker(A,B)

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

[1,] 1 1 3 3

[2,] 1 1 3 3

[3,] 2 2 4 4

[4,] 2 2 4 4

15 矩阵的维数

在R中很容易得到一个矩阵的维数,函数dim()将返回一个矩阵的维数,nrow()返回

行数,ncol()返回列数,例如:

> A=matrix(1:12,3,4)

> A

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

[1,] 1 4 7 10

[2,] 2 5 8 11

[3,] 3 6 9 12

> nrow(A)

[1] 3

> ncol(A)

[1] 4

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

发表评论

暂无数据
推荐帖子