Skip to content

Commit

Permalink
Create svd_wrapper and copy ginv to avoid LAPACK errors
Browse files Browse the repository at this point in the history
  • Loading branch information
GFabien committed May 5, 2024
1 parent eefad3b commit af5e5aa
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 19 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ Imports:
ggrepel,
graphics,
gridExtra,
MASS,
matrixStats,
methods,
parallel,
Expand All @@ -45,6 +44,7 @@ Suggests:
devtools,
FactoMineR,
knitr,
MASS,
pander,
rmarkdown,
rticles,
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export(rgcca_predict)
export(rgcca_stability)
export(rgcca_transform)
importFrom(Deriv,Deriv)
importFrom(MASS,ginv)
importFrom(caret,confusionMatrix)
importFrom(caret,multiClassSummary)
importFrom(caret,postResample)
Expand Down
2 changes: 0 additions & 2 deletions R/block_init.R
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")
}
Expand Down
20 changes: 20 additions & 0 deletions R/ginv.R
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])
)
}
6 changes: 3 additions & 3 deletions R/initsvd.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ initsvd <- function(X, dual = TRUE) {

if (dual) {
ifelse(n >= p,
return(svd(X, nu = 0, nv = 1)$v),
return(svd(X, nu = 1, nv = 0)$u)
return(svd_wrapper(X, nu = 0, nv = 1)$v),
return(svd_wrapper(X, nu = 1, nv = 0)$u)
)
} else {
return(svd(X, nu = 0, nv = 1)$v)
return(svd_wrapper(X, nu = 0, nv = 1)$v)
}
}
25 changes: 25 additions & 0 deletions R/svd_wrapper.R
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)
}
17 changes: 5 additions & 12 deletions codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@
},
"sameAs": "https://CRAN.R-project.org/package=knitr"
},
{
"@type": "SoftwareApplication",
"identifier": "MASS",
"name": "MASS"
},
{
"@type": "SoftwareApplication",
"identifier": "pander",
Expand Down Expand Up @@ -247,18 +252,6 @@
"sameAs": "https://CRAN.R-project.org/package=gridExtra"
},
"8": {
"@type": "SoftwareApplication",
"identifier": "MASS",
"name": "MASS",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
"name": "Comprehensive R Archive Network (CRAN)",
"url": "https://cran.r-project.org"
},
"sameAs": "https://CRAN.R-project.org/package=MASS"
},
"9": {
"@type": "SoftwareApplication",
"identifier": "matrixStats",
"name": "matrixStats",
Expand Down

0 comments on commit af5e5aa

Please sign in to comment.