R

时间:2017-04-23 19:04:46

标签: r matrix-inverse

我试图在R中找到A的Moore-Penrose矩阵逆,因为A是矩形,然后是i代码。我发现的一个逆是AI,当我计算A AI 时,A与A不同。

> A = matrix(
+ c(1,1,1,1,1,1,1,1,0,1,1,1,0,0,1,1,0,0,0,1),
+ nrow=5, ncol=4, byrow = TRUE)

> A
     [,1] [,2] [,3] [,4]
[1,]    1    1    1    1
[2,]    1    1    1    1
[3,]    0    1    1    1
[4,]    0    0    1    1
[5,]    0    0    0    1

> (AA <- t(A) %*% A)
     [,1] [,2] [,3] [,4]
[1,]    2    2    2    2
[2,]    2    3    3    3
[3,]    2    3    4    4
[4,]    2    3    4    5

> (AAI <- ginv(AA))
              [,1]          [,2]          [,3]          [,4]
[1,]  1.500000e+00 -1.000000e+00  5.551115e-16  2.220446e-16
[2,] -1.000000e+00  2.000000e+00 -1.000000e+00 -1.110223e-16
[3,]  1.332268e-15 -1.000000e+00  2.000000e+00 -1.000000e+00
[4,] -2.220446e-16 -1.110223e-16 -1.000000e+00  1.000000e+00

> AI <- AAI  %*%  t(A)

> AI
              [,1]          [,2]          [,3]          [,4]          [,5]
[1,]  5.000000e-01  5.000000e-01 -1.000000e+00  7.771561e-16  2.220446e-16
[2,]  3.330669e-16  3.330669e-16  1.000000e+00 -1.000000e+00 -1.110223e-16
[3,]  2.220446e-16  2.220446e-16 -1.110223e-15  1.000000e+00 -1.000000e+00
[4,] -1.110223e-16 -1.110223e-16  1.110223e-16  2.220446e-16  1.000000e+00`

> A %*% AI %*% A
              [,1]          [,2]         [,3] [,4]
[1,]  1.000000e+00  1.000000e+00 1.000000e+00    1
[2,]  1.000000e+00  1.000000e+00 1.000000e+00    1
[3,]  8.881784e-16  1.000000e+00 1.000000e+00    1
[4,]  2.220446e-16 -7.771561e-16 1.000000e+00    1
[5,] -2.220446e-16 -1.110223e-16 1.110223e-16    1
`

1 个答案:

答案 0 :(得分:2)

实值矩阵A的伪逆A +满足4个条件:
a)A + A = A
b)A + A A + = A +
c)t(A A +)= A A +
d)t(A + A)= A + A

以下是代码:

A = matrix(
  c(1,1,1,1,1,1,1,1,0,1,1,1,0,0,1,1,0,0,0,1),
  nrow=5, ncol=4, byrow = TRUE)
A
     [,1] [,2] [,3] [,4]
[1,]    1    1    1    1
[2,]    1    1    1    1
[3,]    0    1    1    1
[4,]    0    0    1    1
[5,]    0    0    0    1

library(MASS)
pinvA <- round(ginv(A),14)
pinvA
     [,1] [,2] [,3] [,4] [,5]
[1,]  0.5  0.5   -1    0    0
[2,]  0.0  0.0    1   -1    0
[3,]  0.0  0.0    0    1   -1
[4,]  0.0  0.0    0    0    1

A %*% pinvA %*% A == A
     [,1] [,2] [,3] [,4]
[1,] TRUE TRUE TRUE TRUE
[2,] TRUE TRUE TRUE TRUE
[3,] TRUE TRUE TRUE TRUE
[4,] TRUE TRUE TRUE TRUE
[5,] TRUE TRUE TRUE TRUE

(pinvA %*% A %*% pinvA) == pinvA
     [,1] [,2] [,3] [,4] [,5]
[1,] TRUE TRUE TRUE TRUE TRUE
[2,] TRUE TRUE TRUE TRUE TRUE
[3,] TRUE TRUE TRUE TRUE TRUE
[4,] TRUE TRUE TRUE TRUE TRUE

t(A %*% pinvA) == A %*% pinvA
     [,1] [,2] [,3] [,4] [,5]
[1,] TRUE TRUE TRUE TRUE TRUE
[2,] TRUE TRUE TRUE TRUE TRUE
[3,] TRUE TRUE TRUE TRUE TRUE
[4,] TRUE TRUE TRUE TRUE TRUE
[5,] TRUE TRUE TRUE TRUE TRUE

t(pinvA %*% A) == pinvA %*% A
     [,1] [,2] [,3] [,4]
[1,] TRUE TRUE TRUE TRUE
[2,] TRUE TRUE TRUE TRUE
[3,] TRUE TRUE TRUE TRUE
[4,] TRUE TRUE TRUE TRUE