3

我正在使用 Choleski 分解来计算半正定矩阵的逆矩阵。但是,当我的矩阵变得非常大并且其中有零时,我的矩阵不再(从计算机的角度来看)是正定的。因此,为了解决这个问题,我使用pivot = TRUE了 .choleski 命令中的选项R。但是,(正如您将在下面看到的)两者返回相同的输出,但行和列或矩阵重新排列。我试图弄清楚是否有办法(或转换)使它们相同。这是我的代码:

X = matrix(rnorm(9),nrow=3)
A = X%*%t(X)

inv1 = function(A){
    Q = chol(A)
    L = t(Q)
    inverse = solve(Q)%*%solve(L)
    return(inverse)
}


inv2 = function(A){
    Q = chol(A,pivot=TRUE)
    L = t(Q)
    inverse = solve(Q)%*%solve(L)
    return(inverse)
}

运行时会导致:

 > inv1(A)
              [,1]      [,2]      [,3]
    [1,]  9.956119 -8.187262 -4.320911
    [2,] -8.187262  7.469862  3.756087
    [3,] -4.320911  3.756087  3.813175
    > 
    > inv2(A)
              [,1]      [,2]      [,3]
    [1,]  7.469862  3.756087 -8.187262
    [2,]  3.756087  3.813175 -4.320911
    [3,] -8.187262 -4.320911  9.956119

有没有办法让两个答案匹配?我想inv2()从返回答案inv1()

4

2 回答 2

5

这在中进行了解释?chol:列排列作为属性返回。

inv2 <- function(A){
  Q <- chol(A,pivot=TRUE)
  Q <- Q[, order(attr(Q,"pivot"))]
  Qi <- solve(Q)
  Qi %*% t(Qi)
}
inv2(A)
solve(A)  # Identical
于 2013-08-22T19:20:08.073 回答
1

Typically

M = matrix(rnorm(9),3)
M
           [,1]        [,2]       [,3]
[1,]  1.2109251 -0.58668426 -0.4311855
[2,] -0.8574944  0.07003322 -0.6112794
[3,]  0.4660271 -0.47364400 -1.6554356
library(Matrix)
pm1 <- as(as.integer(c(2,3,1)), "pMatrix")
M %*% pm1
           [,1]       [,2]        [,3]
[1,] -0.4311855  1.2109251 -0.58668426
[2,] -0.6112794 -0.8574944  0.07003322
[3,] -1.6554356  0.4660271 -0.47364400
于 2013-08-22T18:48:32.927 回答