0

I'm using a C++ code in R, through the R package Rcpp to speed up my algorithm. I would like to use the hcubature function from the cubature C package to perform a 4dimensional integral in C++.

I'm having some difficulties in understanding how to use my function with the hcubature function. Below is my function:

#include <Rcpp.h>
#include <cubature.h>
#include <cmath>

// [[Rcpp::export]]
float 2dbrownian(const float& x,const float& x0, const float& sigma, const float& t) {
  const float& a1 = (1/sqrt(2.0 * M_PI * sigma * t));
  const float& b1 = exp(-((x - x0) * (x - x0))/(2.0 * sigma * t));
  const float& res = a1 * b1;
  return res;
}

// [[Rcpp::export]]
double integralFunction(float xAt_pos, float xBt_pos, float yAt_pos, float yBt_pos, const float& xA0, const float& xB0, const float& yA0,
   const float& yB0, const float& t1, const float& sigma){

  float temp_pbxA = 2dbrownian(xAt_pos, xA0, sigma,t1);
  float temp_pbxB = 2dbrownian(xBt_pos, xB0, sigma, t1);
  float temp_pbyA = 2dbrownian(yAt_pos, yA0, sigma,t1);
  float temp_pbyB = 2dbrownian(yBt_pos, yB0, sigma, t1);

  return (temp_pbxB * temp_pbyB) * (temp_pbxA * temp_pbyA);

};

And this is the hcubature function from the cubature C package, which can be used in C++ as:

int hcubature(unsigned fdim, integrand f, void *fdata,
      unsigned dim, const double *xmin, const double *xmax, 
      size_t maxEval, double reqAbsError, double reqRelError, 
      error_norm norm,
      double *val, double *err);

From this I have understood that:

fdim: is a fictitious parameter that has to be set equal to 1; f: is the integrand function ('integralFunction' in my case); void *fdata: should indicates the additional arguments of the integrand, where 'void *' means that there are no particular specification on which type of parameter are accepted; unsigned dim: the dimensionality of the integrand ('4' in my case); const double *xmin: is the vector specifying the lower limit of the integral for each variable; const double *xmax: is the vector specifying the upper limit of the integral for each variable;

I'm sure that I'm missing something but I do not know exactly what.

CafféSospeso
  • 1,101
  • 3
  • 11
  • 28
  • Is this really different from your previous question from four dayas ago? – Dirk Eddelbuettel Aug 06 '21 at 14:11
  • I'm sorry for having insisted on this issue. I'm trying to speed up mu algorithm as much as possible, since I will need to run it on several combinations of parameter values. The difference form my other questions is on the fact that here the issue is on how to use the hcubature C++ function with my custom C++ function. Perhaps my question is silly and trivial for those comfortable in C++, but I'm trying to understand. Perhaps others may have similar issues. – CafféSospeso Aug 06 '21 at 14:16
  • You are appearing to have a particular issue with a particular package. Did you try contacting its author and/or users? – Dirk Eddelbuettel Aug 06 '21 at 14:34
  • I did not because I thought that mine was a general question on how to use C++ function from a non-standard C++ library within my C++ code (to be sourced in R). But may be I'm wrong and it is just my ignorance. It appears that 7 yago someone has asked a similar question: https://stackoverflow.com/questions/20474303/using-c-function-from-other-package-in-rcpp – CafféSospeso Aug 06 '21 at 14:50

0 Answers0