-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create svd_wrapper and copy ginv to avoid LAPACK errors
- Loading branch information
Showing
7 changed files
with
54 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
#' @importFrom MASS ginv | ||
|
||
block_init <- function(x, init = "svd") { | ||
UseMethod("block_init") | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Copy of the ginv function from the MASS package. | ||
# We replace calls to svd by our wrapper to avoid LAPACK errors. | ||
ginv <- function (X, tol = sqrt(.Machine$double.eps)) | ||
{ | ||
if (length(dim(X)) > 2L || !(is.numeric(X) || is.complex(X))) | ||
stop("'X' must be a numeric or complex matrix") | ||
if (!is.matrix(X)) | ||
X <- as.matrix(X) | ||
Xsvd <- svd_wrapper(X) | ||
if (is.complex(X)) | ||
Xsvd$u <- Conj(Xsvd$u) | ||
Positive <- Xsvd$d > max(tol * Xsvd$d[1L], 0) | ||
if (all(Positive)) | ||
Xsvd$v %*% (1/Xsvd$d * t(Xsvd$u)) | ||
else if (!any(Positive)) | ||
array(0, dim(X)[2L:1L]) | ||
else Xsvd$v[, Positive, drop = FALSE] %*% ( | ||
(1/Xsvd$d[Positive]) * t(Xsvd$u[, Positive, drop = FALSE]) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# workaround by Art Owen to avoid LAPACK errors | ||
# See https://stat.ethz.ch/pipermail/r-help/2007-October/143508.html | ||
svd_wrapper <- function(x, nu = min(n, p), nv = min(n, p), ...) { | ||
success <- FALSE | ||
n <- NROW(x) | ||
p <- NCOL(x) | ||
try({ | ||
svd_x <- base::svd(x, nu, nv, ...) | ||
success <- TRUE | ||
}, silent = TRUE) | ||
if( success ) { | ||
return(svd_x) | ||
} | ||
try( { | ||
svd_tx <- base::svd(t(x), nv, nu, ...) | ||
success <- TRUE | ||
}, silent = TRUE ) | ||
if( !success ) { | ||
stop("Error: svd(x) and svd(t(x)) both failed to converge.") | ||
} | ||
temp <- svd_tx$u | ||
svd_tx$u <- svd_tx$v | ||
svd_tx$v <- temp | ||
return(svd_tx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters