c - 在预先存在的 R 包中模仿 C 的用法

标签 c r lapack

我正在尝试基于包 QUIC 在 R 中使用 C 代码.不幸的是,我无法让我自己的本地版本的包工作。我从 CRAN 下载了包源考试。在 src文件夹我找到了所需的 QUIC.cpp 代码( A github page contains it here for reference )和一个没有扩展名的 Makevars 文件,其中包含以下两行:

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

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

Error: BLAS/LAPACK routine 'DPOTRF' gave error code -4


dpotrf call 执行 choleksy 分解,错误不是因为缺乏正定性或任何“理论”。 . .我不知道如何解决这个问题,也找不到任何有用的在线信息。

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

EDIT2:这里有一些系统细节。
> 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"

EDIT3:这是我用来测试的代码。
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 storage.modeinteger .但是,在您的代码中,您分配:
n <- 15

这给 n storage.modedouble .

如果添加:
storage.mode(n) <- "integer"

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

关于c - 在预先存在的 R 包中模仿 C 的用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51850295/

相关文章:

r - ggExtra绘图格式:不同绘图尺寸的相似边际绘图

c - 使用命名管道的进程间通信

c - 如何在C程序中使用环境变量

r - 使用 grid_draw 方法而不是 gridExtra 保存绘图

iphone - 是否可以使用 Accelerate/LAPACK 求解非方欠/过约束矩阵?

matrix - 在 OpenCL 中并行执行许多小矩阵运算

c++ - 泊松方程的 Eigen 共轭梯度与 SimplicialLLT

c++ - 当我们使用斐波那契数列的递归方法调用 fib(6) 时,fib(3) 被调用多少次?

c - Id 返回 1 退出状态错误

r - R 中的上标图例文本