using C function from other package in Rcpp

前端 未结 3 636
北恋
北恋 2020-12-05 14:54

I\'m trying to call a C routine from the cubature package in a c++ function to perform multidimensional integration.

The basic R example I\'m trying to reproduce is

3条回答
  •  旧时难觅i
    2020-12-05 15:41

    Unfortunately cubature does not ship the headers in inst/include, so you have to borrow that from them and do something like this in your code:

    typedef void (*integrand) (unsigned ndim, const double *x, void *,
               unsigned fdim, double *fval);
    
    int adapt_integrate(
        unsigned fdim, integrand f, void *fdata,
        unsigned dim, const double *xmin, const double *xmax, 
        unsigned maxEval, double reqAbsError, double reqRelError, 
        double *val, double *err)
    {
        typedef int (*Fun)(unsigned,integrand,void*,unsigned,
            const double*,const double*, unsigned, double, double, double*, double*) ;
        Fun fun = (Fun) R_GetCCallable( "cubature", "adapt_integrate" ) ;           
        return fun(fdim,f,fdata,dim,xmin,xmax,maxEval,reqAbsError, reqRelError,val,err); 
    }
    

    It might be a good idea to negociate with the maintainer of cubature that he ships declarations in inst/include so that you'd only have to use LinkingTo.

提交回复
热议问题