模拟预先存在的 R 包中的 C 用法



我正在尝试基于包QUIC在 R 中使用 C 代码。不幸的是,我无法让我自己的本地版本的软件包工作。我从 CRAN 下载了软件包源代码进行检查。在src文件夹中,我找到了所需的 QUIC.cpp代码(github 页面包含它以供参考(和一个没有扩展名的 Makevars 文件,其中包含以下两行:

PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)
PKG_CXXFLAGS = -DLANG_R

接下来,我在终端中运行R CMD SHLIB QUIC.cpp(没有产生警告或错误(,然后使用dyn.load(QUIC.so)加载到 R 中。当我尝试使用我创建的.so文件在 R 中运行相应的QUIC函数时,我收到一条错误消息:

错误:BLAS/LAPACK 例程"DPOTRF"给出错误代码 -4

dpotrf调用执行胆汁分解,错误不是因为缺乏正确定性或任何"理论"......我不确定如何解决这个问题,在网上找不到任何有用的东西。

编辑:需要明确的是,所有这些都是在下载的QUIC包中完成的,我没有更改任何代码。当我执行library(QUIC)并从那里使用它时,所需的功能可以完美运行。我想最终更改他们的 C 代码,因为我认为QUIC方法可以扩展,但首先我需要QUIC以这种方式工作!

编辑2:以下是一些系统详细信息。

> Sys.info()
sysname 
"Darwin" 
release 
"16.7.0" 
version 
"Darwin Kernel Version 16.7.0; root:xnu-3789.73.8~1/RELEASE_X86_64" 
nodename 
"MacBook-Pro.local" 
machine 
"x86_64" 
> R.Version()
$platform
[1] "x86_64-apple-darwin15.6.0"
$arch
[1] "x86_64"
$os
[1] "darwin15.6.0"
$system
[1] "x86_64, darwin15.6.0"

$`svn rev`
[1] "74626"
$version.string
[1] "R version 3.5.0 (2018-04-23)"
$nickname
[1] "Joy in Playing"

编辑3:这是我用来测试的代码。

n <- 15
rho <- 0.2
S <- diag(n)
# library(QUIC); QUIC(S,rho) works fine. Now copying the setup for QUIC and trying with .so file:

path = NULL; tol = 1e-04; msg = 1; maxIter = 1000; X.init = NULL; W.init = NULL
if (is.null(path)) {
npath <- 1
} else {npath <- length(path)}
if (!is.matrix(rho) && length(rho) != 1 && length(rho) != 
n) {
stop("Wrong number of elements in rho")
}
if (is.vector(rho)) {
rho <- matrix(sqrt(rho)) %*% sqrt(rho)
}
if (length(rho) == 1) {
rho <- matrix(rho, ncol = n, nrow = n)
}
if (is.null(path)) {
if (is.null(X.init)) {
X <- diag(n)
W <- diag(n)
} else {
X <- X.init
W <- W.init }
} else {
if (is.null(X.init)) {
X <- array(diag(n), c(n, n, npath))
W <- array(diag(n), c(n, n, npath))
} else {
X <- array(0, c(n, n, npath))
W <- array(0, c(n, n, npath))
X[, , 1] <- X.init
W[, , 1] <- W.init
}
}
opt <- matrix(0, ncol = npath, nrow = 1)
cputime <- matrix(0, ncol = npath, nrow = 1)
iter <- matrix(0, ncol = npath, nrow = 1)
dGap <- matrix(0, ncol = npath, nrow = 1)
if (is.null(path)) {
job <- "d"
} else {job <- "p"}
storage.mode(job) <- "character"
storage.mode(S) <- "double"
storage.mode(rho) <- "double"
storage.mode(npath) <- "integer"
storage.mode(path) <- "double"
storage.mode(tol) <- "double"
storage.mode(msg) <- "integer"
storage.mode(maxIter) <- "integer"
storage.mode(X) <- "double"
storage.mode(W) <- "double"
storage.mode(opt) <- "double"
storage.mode(cputime) <- "double"
storage.mode(iter) <- "integer"
storage.mode(dGap) <- "double"
dyn.load(".../QUIC.so")    
tmp <- .C("QUICR", job, n, S, rho, npath, path, tol, msg, 
maxIter, X = X, W = W, opt = opt, cputime = cputime, 
iter = iter, dGap = dGap)
#Error: BLAS/LAPACK routine 'DPOTRF' gave error code -4

问题是,在原始QUIC.r函数中,行:

n <- nrow(S)

n一个integerstorage.mode. 但是,在代码中,您可以分配:

n <- 15

这给了n一个doublestorage.mode.

如果添加:

storage.mode(n) <- "integer"

对于存储模式设置的大列表,您的程序应该可以正常工作。

最新更新