我有以下矩阵:
j <- matrix(c(1,1,.5,1,1,.5,.5,.5,1), nrow=3, ncol=3)
这是正半正定的,因为所有特征值都是> = 0。
> eigen(j, symmetric = TRUE)
$values
[1] 2.3660254 0.6339746 0.0000000
$vectors
[,1] [,2] [,3]
[1,] -0.6279630 -0.3250576 7.071068e-01
[2,] -0.6279630 -0.3250576 -7.071068e-01
[3,] -0.4597008 0.8880738 -1.942890e-15
然而,cholesky分解失败......
> chol(j)
Error in chol.default(j) :
the leading minor of order 2 is not positive definite
我还改编了一些来自互联网的代码......
cholesky_matrix <- function(A){
# http://rosettacode.org/wiki/Cholesky_decomposition#C
L <- matrix(0,nrow=nrow(A),ncol=ncol(A))
colnames(L) <- colnames(A)
rownames(L) <- rownames(A)
m <- ncol(L)
for(i in 1:m){
for(j in 1:i){
s <- 0
if(j > 1){
for(k in 1:(j-1)){
s <- s + L[i,k]*L[j,k]
}
}
if(i == j){
L[i,j] <- sqrt(A[i,i] - s)
} else {
L[i,j] <- (1 / L[j,j])*(A[i,j] - s)
}
}
}
return(L)
}
NaNs也“失败”。
> cholesky_matrix(j)
[,1] [,2] [,3]
[1,] 1.0 0 0
[2,] 1.0 0 0
[3,] 0.5 NaN NaN
有谁知道发生了什么事?为什么我的分解失败了?
答案 0 :(得分:2)
矩阵的特征值是
> eigen(j)
$values
[1] 2.366025e+00 6.339746e-01 4.440892e-16
在数值精度的限制范围内,最后一个实际上为零。每?chol
:
计算实对称正定方阵的Choleski分解。
(强调我的)
那就是说,你仍然可以通过设置pivot=TRUE
来获得分解,> chol(j, pivot=TRUE)
[,1] [,2] [,3]
[1,] 1 0.5000000 1
[2,] 0 0.8660254 0
[3,] 0 0.0000000 0
attr(,"pivot")
[1] 1 3 2
attr(,"rank")
[1] 2
Warning message:
In chol.default(j, pivot = TRUE) :
the matrix is either rank-deficient or indefinite
能够处理半明确性:
add_filter( 'option_blogdescription', 'mytheme_bloginfo', 10, 1 );
function mytheme_bloginfo( $text )
{
$country_code = ...;
if ($country_code=="UK") {
$text = "UK Description";
} else if ($country_code=="AU") {
$text = "AU Description";
}
return $text;
}
add_filter( 'option_blogname', 'mytheme_bloginfo_name', 10, 2);
function mytheme_bloginfo_name( $text )
{
$country_code = ...;
if ($country_code=="UK") {
$text = "United Kingdom";
} else if ($country_code=="AU") {
$text = "Australia";
}
return $text;
}