Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
集成C++;R中的函数给出了可疑的结果 当我试图通过RCPP将C++中包含的函数归一化(使其集成到Unity)时,我正在处理一个问题。 C++函数如下所示: // C++ - Function: Standard Gamma Kernel // [[Rcpp::export]] NumericVector g_hat(double b, NumericVector u, NumericVector x){ int n_arg = x.size(); int size = u.size(); NumericVector g(n_arg); double integ = 0; for(int j = 0; j < n_arg; j++){ for(int i = 0; i < size; i++){ g[j] = g[j] + (1/(b*double(size)))*pow(u[i]/b,x[j]/b)*exp(-u[i]/b)/(tgamma(x[j]/b + 1)); } integ += g[j]; } integ = integ -0.5*g[n_arg - 1] -0.5*g[0]; integ = integ*(x[n_arg-1] - x[0]); integ = integ/(double(n_arg)); g = g/double(integ); return(g); }_C++_R_Rcpp - Fatal编程技术网

集成C++;R中的函数给出了可疑的结果 当我试图通过RCPP将C++中包含的函数归一化(使其集成到Unity)时,我正在处理一个问题。 C++函数如下所示: // C++ - Function: Standard Gamma Kernel // [[Rcpp::export]] NumericVector g_hat(double b, NumericVector u, NumericVector x){ int n_arg = x.size(); int size = u.size(); NumericVector g(n_arg); double integ = 0; for(int j = 0; j < n_arg; j++){ for(int i = 0; i < size; i++){ g[j] = g[j] + (1/(b*double(size)))*pow(u[i]/b,x[j]/b)*exp(-u[i]/b)/(tgamma(x[j]/b + 1)); } integ += g[j]; } integ = integ -0.5*g[n_arg - 1] -0.5*g[0]; integ = integ*(x[n_arg-1] - x[0]); integ = integ/(double(n_arg)); g = g/double(integ); return(g); }

集成C++;R中的函数给出了可疑的结果 当我试图通过RCPP将C++中包含的函数归一化(使其集成到Unity)时,我正在处理一个问题。 C++函数如下所示: // C++ - Function: Standard Gamma Kernel // [[Rcpp::export]] NumericVector g_hat(double b, NumericVector u, NumericVector x){ int n_arg = x.size(); int size = u.size(); NumericVector g(n_arg); double integ = 0; for(int j = 0; j < n_arg; j++){ for(int i = 0; i < size; i++){ g[j] = g[j] + (1/(b*double(size)))*pow(u[i]/b,x[j]/b)*exp(-u[i]/b)/(tgamma(x[j]/b + 1)); } integ += g[j]; } integ = integ -0.5*g[n_arg - 1] -0.5*g[0]; integ = integ*(x[n_arg-1] - x[0]); integ = integ/(double(n_arg)); g = g/double(integ); return(g); },c++,r,rcpp,C++,R,Rcpp,对于我的“重标函数”和函数的R包版本,分别产生17.49247和1,表明我的重整化努力失败得很惨 > integrate(f = g_hat, u = data, b = 0.5, lower = 0, upper = 50)$value [1] 17.49247 > integrate(f = dbckden, kerncentres = data, lambda = 0.5, bcmethod = "gamma1", lower = 0, upper = 50)$value [1

对于我的“重标函数”和函数的R包版本,分别产生17.492471,表明我的重整化努力失败得很惨

> integrate(f = g_hat, u = data, b = 0.5, lower = 0, upper = 50)$value
[1] 17.49247
> integrate(f = dbckden, kerncentres = data, lambda = 0.5, bcmethod = "gamma1", lower = 0, upper = 50)$value
[1] 1
对出错原因的分析并没有取得成果。更改
g_hat()
-函数的输出以返回一个
double
,并显示
integ
的值

> g_hat(x = grid, u = data, b = 0.5)
[1] 0.7254907
当我将此值(0.72…)插入
g_hat
算法的最后几行时,如下所示:

g = g/double(0.7254907);
  return(g);
再次在R中执行积分,我得到:

> integrate(f = g_hat, u = data, b = 0.5, lower = 0, upper = 50)$value
[1] 1.004675
> integrate(f = dbckden, kerncentres = data, lambda = 0.5, bcmethod = "gamma1", lower = 0, upper = 50)$value
[1] 1
这意味着近似重整化非常有效

但是为什么在使用上面代码中所示的
integ
-变量时它不起作用呢?为什么我需要以明确的方式将数字插入函数中

此外,为什么这两个函数的图形如此接近,但却集成到完全不同的值?是R中的
integrate()
函数吗?还是Rcpp特有的数值向量

我非常感谢任何一个adivse,因为到目前为止,在这个adivse中花费了大量的调试时间

详细信息:更改积分或细分的
上限
下限
未显示任何重大变化。参数
b
的变化也是如此)

谢谢

编辑:正如克里斯托夫善意暗示的那样,我应该添加一个可复制的示例。 这是C++代码。在RStudio中运行此命令(请确保您有
library(Rcpp)
somwhere。)随着数据更改(10个数据点),绘图看起来有点不同。但悖论仍然完全相同:图形看起来一样,但积分完全不正确(系数>33)。同样,操纵
细分
(从22到10e7)或
abs.tol
(0.01到25)也不能奏效

// This is the C++-code

#include <Rcpp.h>
#include <boost/math/special_functions/gamma.hpp>
using namespace Rcpp;

// C++ - Function: Standard Gamma Kernel
// [[Rcpp::export]]
NumericVector g_hat(double b, NumericVector u, NumericVector x){
  int n_arg = x.size();
  int size = u.size();
  NumericVector g(n_arg);

  // variable which approximates the area beneath the graph of the function g
  double integ = 0;

  // loop over domain of the function
  for(int j = 0; j < n_arg; j++){

    // loop over data
    for(int i = 0; i < size; i++){
      g[j] = g[j] + (1/(b*double(size)))*pow(u[i]/b,x[j]/b)*exp(-u[i]/b)/(tgamma(x[j]/b + 1));
    }
    integ += g[j];
  }

  // correction of the approximate integral (trapezoidal rule)
  integ = integ -0.5*g[n_arg - 1] -0.5*g[0];
  integ = integ*(x[n_arg-1] - x[0]);
  integ = integ/(double(n_arg));

  // normalization of g
  g = g/double(integ);
  return(g);
}

/*** R
# This is the R code
data <- c(1.98559739, 0.86798303, 0.48703074, 1.11475725, 1.69790403, 0.09901693, 0.21825991, 1.08029421, 0.60396438, 0.83915639)
grid <- seq(from = 0, to = 20, by = 0.01)

library(evmix)

# plot of the 2 function graphs
plot(grid,g_hat(x = grid, u = data, b = 0.5),type="l", main = "Comparison: g_hat (C++) and dbckden (R)",ylab="")
lines(grid,dbckden(x = grid, kerncentres = data, lambda = 0.5, bcmethod = "gamma1"),col="red")
legend("topright", legend=c("dbckden", "g_hat"), col = c("red","black"), lty = c(1,1))

# definite integrals
integrate(f = g_hat, u = data, b = 0.5, lower = 0, upper = 55)$value                                          # 33.58002
integrate(f = dbckden, kerncentres = data, lambda = 0.5, bcmethod = "gamma1", lower = 0, upper = 55)$value    # 1

*/
//这是C++代码
#包括
#包括
使用名称空间Rcpp;
//C++函数:标准伽玛核
//[[Rcpp::导出]]
数字向量g_帽(双b,数字向量u,数字向量x){
int n_arg=x.size();
int size=u.size();
数字向量g(n_arg);
//近似函数g图形下方区域的变量
双整数=0;
//函数域上的循环
对于(int j=0;j数据你确定x刻度计数正确吗?顺便说一句:如果没有一个可复制的例子,这可能很难帮助…谢谢你的输入,克里斯托夫!添加了可复制的示例。。。x标度是一个从0到20的序列(步长很小)。在每个x(即x[j])处,我需要计算函数值g[j],它也是数据点(u[0],…,u[size-1])上的和。我通过进一步评论代码澄清了这一点。integ变量只是函数值的总和,所以它只在外部循环的每次迭代中更新!你希望我们仔细检查你的三到四屏代码,我怀疑这会发生。我们都很忙,请尽量把时间缩短到10分钟。
// This is the C++-code

#include <Rcpp.h>
#include <boost/math/special_functions/gamma.hpp>
using namespace Rcpp;

// C++ - Function: Standard Gamma Kernel
// [[Rcpp::export]]
NumericVector g_hat(double b, NumericVector u, NumericVector x){
  int n_arg = x.size();
  int size = u.size();
  NumericVector g(n_arg);

  // variable which approximates the area beneath the graph of the function g
  double integ = 0;

  // loop over domain of the function
  for(int j = 0; j < n_arg; j++){

    // loop over data
    for(int i = 0; i < size; i++){
      g[j] = g[j] + (1/(b*double(size)))*pow(u[i]/b,x[j]/b)*exp(-u[i]/b)/(tgamma(x[j]/b + 1));
    }
    integ += g[j];
  }

  // correction of the approximate integral (trapezoidal rule)
  integ = integ -0.5*g[n_arg - 1] -0.5*g[0];
  integ = integ*(x[n_arg-1] - x[0]);
  integ = integ/(double(n_arg));

  // normalization of g
  g = g/double(integ);
  return(g);
}

/*** R
# This is the R code
data <- c(1.98559739, 0.86798303, 0.48703074, 1.11475725, 1.69790403, 0.09901693, 0.21825991, 1.08029421, 0.60396438, 0.83915639)
grid <- seq(from = 0, to = 20, by = 0.01)

library(evmix)

# plot of the 2 function graphs
plot(grid,g_hat(x = grid, u = data, b = 0.5),type="l", main = "Comparison: g_hat (C++) and dbckden (R)",ylab="")
lines(grid,dbckden(x = grid, kerncentres = data, lambda = 0.5, bcmethod = "gamma1"),col="red")
legend("topright", legend=c("dbckden", "g_hat"), col = c("red","black"), lty = c(1,1))

# definite integrals
integrate(f = g_hat, u = data, b = 0.5, lower = 0, upper = 55)$value                                          # 33.58002
integrate(f = dbckden, kerncentres = data, lambda = 0.5, bcmethod = "gamma1", lower = 0, upper = 55)$value    # 1

*/