了解通过RInside将犰狳矩阵传递给R函数
Understanding passing armadillo matrices to R functions via RInside
我正在尝试通过RInside在C++中使用R。我在将犰狳矩阵传递给 R 并返回结果时遇到问题。下面我能够从 R 库函数返回结果,但是我得到了错误的结果。我使用时刻包中的偏度函数作为示例,该函数在 R 中应按要求工作。我检查了RInside的示例,但我仍然不确定如何使用RcppArmadillo。如何将 c++ 中的犰狳矩阵正确传递给 R?
#include <RInside.h>
#include <RcppArmadillo.h>
using namespace std;
using namespace arma;
int main(int argc, char *argv[]) {
RInside R(argc, argv);
string R_libs = "suppressMessages(library(moments));";
R.parseEvalQ(R_libs);
mat A = randu<mat>(5,5);
R["A"] = A;
string R_skewness = "B <- skewness(A);";
//this fails
mat B = Rcpp::as<mat>(R.parseEval(R_skewness)); //terminate called after throwing an instance of 'Rcpp::not_a_matrix'
//this works but wrong
mat B = Rcpp::as<vec>(R.parseEval(R_skewness)); // returns only 1 number, should be 5 ( 1 for each columnn), same result if i change mat B to vec B
exit(0);
}
我们实现as<mat>
的方式要求你传递的 R 对象是一个矩阵。在您的示例中,B
是一个向量:
> A <- matrix( runif(25), ncol = 5)
> A
[,1] [,2] [,3] [,4] [,5]
[1,] 0.19215339 0.5857249 0.14345222 0.32154176 0.6162155
[2,] 0.95753898 0.9618379 0.06239842 0.06200197 0.7044018
[3,] 0.33575790 0.1372804 0.03027635 0.62662467 0.9778451
[4,] 0.16504957 0.1919765 0.49176372 0.94841456 0.2914772
[5,] 0.01570709 0.8055231 0.51218581 0.79562809 0.6939380
> B <- skewness( A )
> B
[1] 1.15196587 -0.04547576 0.32186257 -0.30788111 -0.29251009
对于转换为arma::vec
,我不重现您看到的行为。arma::vec
有 3 个元素:
require( RcppArmadillo ) ## and make sure you have Rcpp 0.10.0 or later
sourceCpp( code = '
// [[Rcpp::depends("RcppArmadillo")]]
#include <RcppArmadillo.h>
using namespace arma ;
using namespace Rcpp ;
// [[Rcpp::export]]
List foo( NumericVector x){
vec B = Rcpp::as<vec>(x);
return List::create(
_["nrows"] = B.n_rows,
_["ncols"] = B.n_cols
) ;
}
')
foo( c(1, 2, 3 ) )
# $nrows
# [1] 3
#
# $ncols
# [1] 1
您正在尝试涉及多个模板化程度很高的库的复合表达式。 这可能会出错。 我建议分批进行:
-
确保将期望
A
矩阵传递到嵌入式 R -
确保函数调用正常工作,检查结果。
-
重要提示:检查结果类型。矩阵应该可以正常工作。
-
将结果返回到C++。
-
把它送到RCPP。
-
使用Rcpp犰狳编组到达犰狳。
原则上,这应该有效。魔鬼一如既往地在细节中。
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 函数调用中参数的顺序重要吗
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用不带参数的函数访问结构元素
- 代码在main()中运行,但在函数中出现错误
- 内置函数可查看CPP中的成员变量
- 如何获取std::result_of函数的返回类型
- 了解通过RInside将犰狳矩阵传递给R函数
- C 具有数学库,例如GSL,Lapack或CBLAS性能与带有R函数的Rinside的C .态
- 一