gpt4 book ai didi

RcppArmadillo 传递用户定义函数

转载 作者:行者123 更新时间:2023-12-03 01:25:47 26 4
gpt4 key购买 nike

考虑以下 R 代码,

## ----------- R version -----------

caller <- function(x=1:3, fun = "identity", ...){

## do some other stuff
## ...
## then call the function
eval(call(fun, x))

}

fun1 <- function(x, ...){
x + x
}

fun2 <- function(x, a = 10) a * x

caller(fun = "fun1")
caller(fun = "fun2")

用户可以传递一个函数名称“fun”,由调用者使用。我希望使用 RcppArmadillo 对象执行相同的任务(显然,作为更复杂任务的一部分)。该函数将在 C++ 中定义,用户通过引用其名称在 R 级别选择它:

caller_cpp(1:3, "fun1_cpp")

caller_cpp(1:3, "fun2_cpp")

等等

这是我对调用函数的天真的尝试,甚至无法编译:

## ----------- C++ version -----------

library(Rcpp)
require( RcppArmadillo )

sourceCpp( code = '

// [[Rcpp::depends("RcppArmadillo")]]

#include <RcppArmadillo.h>

using namespace arma ;
using namespace Rcpp ;


colvec fun1_cpp(const colvec x)
{
colvec y ;
y = x + x;
return (y);
}

colvec fun2_cpp(const colvec x)
{
colvec y ;
y = 10*x;
return (y);
}

// mysterious pointer business in an attempt
// to select a compiled function by its name

typedef double (*funcPtr)(SEXP);
SEXP putFunPtrInXPtr(SEXP funname) {
std::string fstr = Rcpp::as<std::string>(funname);
if (fstr == "fun1")
return(Rcpp::XPtr<funcPtr>(new funcPtr(&fun1_cpp)));
else if (fstr == "fun2")
return(Rcpp::XPtr<funcPtr>(new funcPtr(&fun2_cpp)));

}

// [[Rcpp::export]]
colvec caller_cpp(const colvec x, character funname)
{
Rcpp::XPtr fun = putFunPtrInXPtr(funname);
colvec y ;
y = fun(x);
return (y);
}

')
<小时/>

编辑:按照 Dirk 的建议查看 RcppDE 后修改示例。

最佳答案

(有时您需要对文件使用 svn log ... 来查看它们的日期...)

我认为更好的用例是我将基于 C 的 DEoptim“移植”到 Rcpp/RcppArmadillo:RcppDE。在其中,我允许优化例程使用 R 函数(如 DEoptim 所做的那样)或用户提供的编译函数——据我所知,这就是您想要的。

有一点点 C++ 脚手架,但遵循它应该没有问题。

于 2013 年 1 月 21 日编辑 下面是一个完整的解决方案,我也刚刚将其发布为 this new post at the Rcpp Gallery -- 包括一些评论和示例用法。

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

using namespace arma;
using namespace Rcpp;

vec fun1_cpp(const vec& x) { // a first function
vec y = x + x;
return (y);
}

vec fun2_cpp(const vec& x) { // and a second function
vec y = 10*x;
return (y);
}

typedef vec (*funcPtr)(const vec& x);

// [[Rcpp::export]]
XPtr<funcPtr> putFunPtrInXPtr(std::string fstr) {
if (fstr == "fun1")
return(XPtr<funcPtr>(new funcPtr(&fun1_cpp)));
else if (fstr == "fun2")
return(XPtr<funcPtr>(new funcPtr(&fun2_cpp)));
else
return XPtr<funcPtr>(R_NilValue); // runtime error as NULL no XPtr
}

// [[Rcpp::export]]
vec callViaString(const vec x, std::string funname) {
XPtr<funcPtr> xpfun = putFunPtrInXPtr(funname);
funcPtr fun = *xpfun;
vec y = fun(x);
return (y);
}

// [[Rcpp::export]]
vec callViaXPtr(const vec x, SEXP xpsexp) {
XPtr<funcPtr> xpfun(xpsexp);
funcPtr fun = *xpfun;
vec y = fun(x);
return (y);
}

关于RcppArmadillo 传递用户定义函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14428687/

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