Skip to content

Commit

Permalink
Check first for negative eigenvalues
Browse files Browse the repository at this point in the history
  • Loading branch information
haziqj committed May 30, 2024
1 parent e978b33 commit 8826cda
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions R/21-rgeneric_cached.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ inla_sem_cached <- function(
# fixed parameter values we still need the indices...

force_pd <- function(x) {
ed <- eigen(x, symmetric = TRUE)
eval <- ed$values
evec <- ed$vectors
eval[eval < 0] <- .Machine$double.eps
evec %*% diag(eval) %*% t(evec)
ed <- eigen(x, symmetric = TRUE, only.values = TRUE)
if (any (ed$values < 0)) {
ed <- eigen(x, symmetric = TRUE)
eval <- ed$values
evec <- ed$vectors
eval[eval < 0] <- .Machine$double.eps
out <- evec %*% diag(eval) %*% t(evec)
} else {
out <- x
}
out
}
assign("force_pd", force_pd, envir = envir)

Expand Down

0 comments on commit 8826cda

Please sign in to comment.