gpt4 book ai didi

r - 在 RcppArmadillo 中使用 SuperLU 稀疏求解器

转载 作者:行者123 更新时间:2023-12-03 14:15:37 27 4
gpt4 key购买 nike

我正在尝试通过 RcppArmadillo 使用来自 Armadillo ( http://arma.sourceforge.net/docs.html#spsolve )的 SparseLU 求解器:

#define ARMA_USE_SUPERLU
// [Rcpp::depends(RcppArmadillo)]
#include <RcppArmadillo.h>

// [[Rcpp::export]]
arma::vec sp_solver(arma::sp_mat K, arma::vec x) {
arma::superlu_opts opts;
opts.symmetric = true;
arma::vec res;
arma::spsolve(res, K, x, "superlu", opts);
return res;
}

/*** R
library(Matrix)
K <- sparseMatrix(i = c(1, 2, 1), j = c(1, 2, 2), x = c(1, 1, 0.5), symmetric = TRUE)
x <- runif(2)
sp_solver(K, x)
*/

我收到错误 undefined reference to 'superlu_free' .
我想我缺少一些库链接。
知道如何解决这个问题吗?

我在 Windows 10 上。

最佳答案

RcppArmadillo 非常方便,我自己一直在使用它。因为所有 Rcpp* 代码都将从 R 调用,所以我们可以假设存在一些功能。

这包括 LAPACK 和 BLAS,并解释了为什么即使 Armadillo 文档明确指出您需要 LAPACK 和 BLAS,我们也可以“不链接”地使用 RcppArmadillo。为什么?好吧,因为 R 已经有了 LAPACK 和 BLAS。

(顺便说一句,当且仅当 R 是用它自己的 LAPACK 子集构建的,因为某些复杂的值例程不可用时,这会导致相当多的早期问题。Baptiste 受到了很大的打击,因为他的包需要(编辑)这些。多年来,Brian Ripley 在将那些缺失的例程添加到 R 的 LAPACK 中最有帮助。当 R 使用外部 LAPACK 和 BLAS 构建时,从来没有出现过问题,这在我维护的 Debian/Ubuntu 包中很常见。)

在这里你想要 SuperLU。这是可选的,您的工作是确保将其链接起来。简而言之,它不仅仅是神奇地工作。并且很难自动化,因为它涉及链接,这使我们无法轻松控制平台相关性和安装要求。

但是这个问题并不新鲜,实际上还有an entire Rcpp Gallery post关于这个话题。

编辑:使用适合我系统的那篇文章的单行代码,您的代码也可以正常工作(一旦我更正了 Rcpp::depends 以获得它需要的双括号:

R> Sys.setenv("PKG_LIBS"="-lsuperlu")
R> sourceCpp("answer.cpp")

R> library(Matrix)

R> K <- sparseMatrix(i = c(1, 2, 1), j = c(1, 2, 2), x = c(1, 1, 0.5), symmetric = TRUE)

R> x <- runif(2)

R> sp_solver(K, x)
[,1]
[1,] 0.264929
[2,] 0.546050
R>

更正代码
#define ARMA_USE_SUPERLU
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>

// [[Rcpp::export]]
arma::vec sp_solver(arma::sp_mat K, arma::vec x) {
arma::superlu_opts opts;
opts.symmetric = true;
arma::vec res;
arma::spsolve(res, K, x, "superlu", opts);
return res;
}

/*** R
library(Matrix)
K <- sparseMatrix(i = c(1, 2, 1), j = c(1, 2, 2), x = c(1, 1, 0.5), symmetric = TRUE)
x <- runif(2)
sp_solver(K, x)
*/

关于r - 在 RcppArmadillo 中使用 SuperLU 稀疏求解器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60911356/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com