- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我一直在编写一个 R 函数来计算关于某些分布的积分,请参见下面的代码。
EVofPsi = function(psi, probabilityMeasure, eps=0.01, ...){
distFun = function(u){
probabilityMeasure(u, ...)
}
xx = yy = seq(0,1,length=1/eps+1)
summand=0
for(i in 1:(length(xx)-1)){
for(j in 1:(length(yy)-1)){
signPlus = distFun(c(xx[i+1],yy[j+1]))+distFun(c(xx[i],yy[j]))
signMinus = distFun(c(xx[i+1],yy[j]))+distFun(c(xx[i],yy[j+1]))
summand = c(summand, psi(c(xx[i],yy[j]))*(signPlus-signMinus))
}
}
sum(summand)
}
它工作正常,但速度很慢。经常听说用 C++ 等编译语言重新编写函数会加快速度,尤其是因为上面的 R 代码涉及双循环。我也是,使用 Rcpp:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
double EVofPsiCPP(Function distFun, Function psi, int n, double eps) {
NumericVector xx(n+1);
NumericVector yy(n+1);
xx[0] = 0;
yy[0] = 0;
// discretize [0,1]^2
for(int i = 1; i < n+1; i++) {
xx[i] = xx[i-1] + eps;
yy[i] = yy[i-1] + eps;
}
Function psiCPP(psi);
Function distFunCPP(distFun);
double signPlus;
double signMinus;
double summand = 0;
NumericVector topRight(2);
NumericVector bottomLeft(2);
NumericVector bottomRight(2);
NumericVector topLeft(2);
// compute the integral
for(int i=0; i<n; i++){
//printf("i:%d \n",i);
for(int j=0; j<n; j++){
//printf("j:%d \n",j);
topRight[0] = xx[i+1];
topRight[1] = yy[j+1];
bottomLeft[0] = xx[i];
bottomLeft[1] = yy[j];
bottomRight[0] = xx[i+1];
bottomRight[1] = yy[j];
topLeft[0] = xx[i];
topLeft[1] = yy[j+1];
signPlus = NumericVector(distFunCPP(topRight))[0] + NumericVector(distFunCPP(bottomLeft))[0];
signMinus = NumericVector(distFunCPP(bottomRight))[0] + NumericVector(distFunCPP(topLeft))[0];
summand = summand + NumericVector(psiCPP(bottomLeft))[0]*(signPlus-signMinus);
//printf("summand:%f \n",summand);
}
}
return summand;
}
我很高兴,因为这个 C++ 函数运行良好。但是,当我测试这两个函数时,C++ 函数运行速度较慢:
sourceCpp("EVofPsiCPP.cpp")
pFGM = function(u,theta){
u[1]*u[2] + theta*u[1]*u[2]*(1-u[1])*(1-u[2])
}
psi = function(u){
u[1]*u[2]
}
print(system.time(
for(i in 1:10){
test = EVofPsi(psi, pFGM, 1/100, 0.2)
}
))
test
print(system.time(
for(i in 1:10){
test = EVofPsiCPP(psi, function(u){pFGM(u,0.2)}, 100, 1/100)
}
))
那么,周围有好心的专家愿意为我解释一下吗?我是不是像猴子一样编码,有没有办法加快该功能?此外,我还有第二个问题。实际上,我可以用 SEXP 替换输出类型 double,也可以用 SEXP 替换参数类型 Function,它似乎没有改变任何东西。那有什么区别呢?
非常感谢您,吉尔达斯
最佳答案
其他人已经在评论中回答了。所以我只强调一点:回调 R 函数是昂贵的,因为我们需要对错误处理格外小心。仅仅在 C++ 中使用循环并调用 R 函数并不是在 C++ 中重写代码。尝试将 psi
和 pFGM
重写为 C++ 函数,并在此处报告发生的情况。
您可能会争辩说您失去了一些灵 active 并且您不能再使用任何 R 函数。对于这种情况,我建议使用某种混合解决方案,您已经在 C++ 中实现了最常见的情况,否则回退到 R 解决方案。
至于另一个问题,SEXP
是一个 R 对象。这是 R API 的一部分。它可以是任何东西。当您从它创建一个 Function
时(就像在创建一个带有 Function
参数的函数时为您隐式完成的那样)时,您可以保证这确实是一个 R 函数。开销非常小,但是在代码表达能力方面的 yield 是巨大的。
关于c++ - Rcpp 函数比相同的 R 函数慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17958168/
我想将函数参数中的默认值设置为 Rcpp::Function 参数。 只是简单的赋值,Rcpp::Function func = mean , 不可能。它返回错误:no viable conversi
我正在处理需要逐元素矩阵乘法的代码。我试图在 Rcpp 中实现这一点,因为代码需要一些昂贵的循环。我对 Rcpp 还很陌生,可能会遗漏一些东西,但我无法使逐元素矩阵乘法工作。 // [[Rcpp::e
在 C++ 中,我们可以声明一个变量作为引用。 int a = 10; int& b = a; 如果我们设置 b=15 , a 也会改变。 我想在 Rcpp 中做类似的事情。 List X = obj
我正在阅读很棒的 Rcpp vignette关于使用 Rcpp 模块公开 C++ 类和函数。在这种情况下,是否可以创建一个 Rcpp 函数,该函数具有一个类型为 Uniform 的类作为参数之一,并且
我在 R 中有一个命名列表: l = list(a=1, b=2) 我想在 Rcpp 中使用这个列表,并迭代值和名称。理想情况下,它可能类似于(为简洁起见使用 C++11 格式): void prin
这个问题在这里已经有了答案: Rcpp - sourceCpp - undefined symbol (2 个答案) 关闭 4 年前。 我现有的 C 代码由三个文件组成:头文件(“.h”文件)、库文
我目前正在为类作业编写模拟退火算法(“解决”背包问题),并想在 Rcpp 中完成(我必须使用 R,而 Rcpp 更快)。 Rcpp 一直给我以下错误 invalid static_cast from
根据我的理解,在 Rcpp 和 C++ 之间转换 vector 会创建新 vector ,如下所示。我的理解对吗? 将 Rcpp vector 转换为 C++ vector 时,我们使用 Rcpp::
我想将参数的默认值设置为 NULL在Rcpp如果参数不是NULL,则函数并根据参数进行一些计算.这种代码的一个例子是 #include using namespace Rcpp; // [[Rcpp
任何人都可以解释以下行为吗? 当声明一个新的NumericMatrix时,y,作为原始矩阵,x,乘以一个标量,c,标量/矩阵乘法的顺序很重要。如果我将左侧的标量与右侧的矩阵相乘(例如 NumericM
有一种方法可以使用 NA 值初始化数值向量,例如。 NumericVector x(10,NumericVector::get_na()) 有没有类似的方法可以将矩阵初始化为 NA 值? 最佳答案 这
这可能是一个非常简单的问题,但我不知道哪里出了问题。 我有一个传递给 Rcpp 函数的列表,该列表的第一个元素是一个 data.frame。 我如何获取该 data.frame? bar = list
我正在尝试开发一个使用 Sundials 的 R 包用于求解微分方程的 C 库。为了不让用户安装库,我将库的源代码放在我的包中。 我已将库中的所有头文件放入 /inst/include/sundial
我正在研究一个同时使用 Rcpp::IntegerVector (行/列指针)和模板化 std::vector 的 Rcpp 稀疏矩阵类。基本原理是,在极大的稀疏矩阵中深度复制整数指针 vector
我想将一个R函数翻译成Rcpp,一个简单的测试代码如下,但我不知道如何处理默认设置为NULL的参数。 test t=R_NilValue, Rcpp
我想将一个R函数翻译成Rcpp,一个简单的测试代码如下,但我不知道如何处理默认设置为NULL的参数。 test t=R_NilValue, Rcpp
我想公开一个 C++ 类和一个将该类的对象作为 R 参数的函数。我必须遵循简化的示例。我使用创建了一个包 Rscript -e 'Rcpp::Rcpp.package.skeleton("soq")'
我想用 Rcpp 编写一个 C++ 函数,它使用 hypred 包中的 C 函数,它在 CRAN here 上. 我读了using C function from other package in R
[我在别处将其草拟为评论,但决定创建一个适当的问题...] 在 Rcpp 中使用数据帧时,就代码结构而言,目前被认为是“最佳实践”的是什么?从 R 到 C++ 代码的输入数据帧“传输”非常容易,但是如
我正在尝试使用 Rcpp::CharacterMatrix 并将每一行转换为 Rcpp::List 中它自己的元素。 但是,我为此编写的函数有一个奇怪的行为,即列表的每个条目都对应于矩阵的最后一行。为
我是一名优秀的程序员,十分优秀!