File: Rcpp-Cuba.cpp

package info (click to toggle)
r-cran-cubature 2.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 9,984 kB
  • sloc: ansic: 9,395; sh: 3,223; cpp: 499; fortran: 226; makefile: 218
file content (337 lines) | stat: -rw-r--r-- 11,126 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
// Copyright (C) 2016 Balasubramanian Narasimhan
//
// We need both R and C/C++ interfaces!

#include <rcubature.h>
#include <cuba.h>

// Cuhre
// int my_Integrand(const int *ndim, const cubareal xx[],
//   const int *ncomp, cubareal ff[], void *userdata) {

// #define Y (4*xx[0]-2)
//     ff[0] = 4*sin(4*Y) * Y * ((Y * ( Y * (Y*Y-4) + 1) - 1));  
//     Rcpp::Rcout<< ff[0] <<std::endl;
//     return 0;
// }

// REMINDER nDim is 3, nComp = 1
int cuhre_fWrapper(const int *nDim, const double x[],
                  const int *nComp, double f[], void *userdata, const int *nVec,
                  const int *core) {

    Rcpp::NumericVector xVal = Rcpp::NumericVector(x, x + (*nDim) * (*nVec));  /* The x argument for the R function f */
    ii_ptr iip = (ii_ptr) userdata;
    
    if (iip -> vector_intf) {
        // Make the argument vector appear as a matrix for R
        xVal.attr("dim") = Rcpp::Dimension(*nDim, *nVec);
    }


    Rcpp::NumericVector fx = Rcpp::Function(iip -> fun)(xVal);

    double* fxp = fx.begin();         /* The ptr to f(x) (real) vector */
    for (int i = 0; i < (*nComp) * (*nVec); ++i) {
        f[i] = fxp[i];
    }
    // (iip -> count)++;
    return 0;
}

// [[Rcpp::export]]
Rcpp::List doCuhre(int nComp, SEXP f, int nDim,
		   int nVec, int minEval, int maxEval, double absTol, double relTol,
                   SEXP stateFile,
                   int key, int flag) {

    Rcpp::NumericVector integral(nComp);
    Rcpp::NumericVector errVals(nComp);
    Rcpp::NumericVector prob(nComp);

    // Create a structure to hold integrand function and initialize it
    integrand_info II;
    II.fun = f;                 /* R function */
    II.count = 0;
    II.vector_intf = (nVec > 1);
    int nregions, fail;

    // Set cores to be zero.
    cubacores(0, 0);

    char *filename = NULL;
    Rcpp::StringVector sv;
    if (!Rf_isNull(stateFile)) {
        sv = Rcpp::StringVector(stateFile);
        filename = sv(0);
    }

    // int count = 0;
    // Cuhre(nDim, nComp, (integrand_t) my_Integrand, NULL, nVec,
    //       relTol, absTol, flag,
    //       minEval, maxEval, key,
    //       filename,
    //       NULL,
    //       &nregions, &count, &fail,
    //       integral.begin(), errVals.begin(), prob.begin());


    Cuhre(nDim, nComp, (integrand_t) cuhre_fWrapper, (void *) &II, nVec,
          relTol, absTol, flag,
          minEval, maxEval, key,
          filename,
          NULL,
          &nregions, &(II.count), &fail,
          integral.begin(), errVals.begin(), prob.begin());

  return Rcpp::List::create(
			    Rcpp::_["integral"] = integral,
			    Rcpp::_["error"] = errVals,
			    Rcpp::_["nregions"] = nregions,
			    Rcpp::_["neval"] = II.count,
                            Rcpp::_["prob"] = prob,
			    Rcpp::_["returnCode"] = fail);
}

// Vegas

int vegas_or_suave_fWrapper(const int *nDim, const double x[],
                            const int *nComp, double f[], void *userdata, const int *nVec,
                            const int *core, const double weight[], const int *iter) {

    Rcpp::NumericVector xVal = Rcpp::NumericVector(x, x + (*nDim) * (*nVec));  /* The x argument for the R function f */
    ii_ptr iip = (ii_ptr) userdata;
    if (iip -> vector_intf) {
        // Make the argument vector appear as a matrix for R
        xVal.attr("dim") = Rcpp::Dimension(*nDim, *nVec);
    }

    Rcpp::NumericVector fx;

    if (iip -> cuba_args) {
        Rcpp::NumericVector weightVal = Rcpp::NumericVector(weight, weight + (*nVec));  /* The weight argument for the R function f */
        Rcpp::IntegerVector iterVal = Rcpp::IntegerVector(iter, iter + 1);  /* The iter argument for the R function f */        
        fx = Rcpp::Function(iip -> fun)(xVal, Rcpp::_["cuba_weight"] = weightVal, Rcpp::_["cuba_iter"] = iterVal);
    } else {
        fx = Rcpp::Function(iip -> fun)(xVal);
    }

    
    double* fxp = fx.begin();         /* The ptr to f(x) (real) vector */
    for (int i = 0; i < (*nComp) * (*nVec); ++i) {
        f[i] = fxp[i];
    }
    return 0;
}

// [[Rcpp::export]]
Rcpp::List doVegas(int nComp, SEXP f, int nDim,
		   int nVec, int minEval, int maxEval, double absTol, double relTol,
                   int nStart, int nIncrease, int nBatch, int gridNo,
                   SEXP stateFile,
                   int seed, int flag, int cuba_args) {

    
    Rcpp::NumericVector integral(nComp);
    Rcpp::NumericVector errVals(nComp);
    Rcpp::NumericVector prob(nComp);

    // Create a structure to hold integrand function and initialize it
    integrand_info II;
    II.cuba_args = cuba_args;   /* vegas specific args */
    II.fun = f;                 /* R function */
    II.count = 0;
    II.vector_intf = (nVec > 1);
    
    int fail;

    // Set cores to be zero.
    cubacores(0, 0);
    
    char *filename = NULL;
    Rcpp::StringVector sv;
    if (!Rf_isNull(stateFile)) {
        sv = Rcpp::StringVector(stateFile);
        filename = sv(0);
    }
    Vegas(nDim, nComp, (integrand_t) vegas_or_suave_fWrapper, (void *) &II, nVec,
          relTol, absTol, flag,
          seed, minEval, maxEval,
          nStart, nIncrease, nBatch, gridNo, 
          filename,
          NULL,
          &(II.count), &fail,
          integral.begin(), errVals.begin(), prob.begin());

  return Rcpp::List::create(
			    Rcpp::_["integral"] = integral,
			    Rcpp::_["error"] = errVals,
			    Rcpp::_["neval"] = II.count,
                            Rcpp::_["prob"] = prob,
			    Rcpp::_["returnCode"] = fail);
}


// [[Rcpp::export]]
Rcpp::List doSuave(int nComp, SEXP f, int nDim,
		   int nVec, int minEval, int maxEval, double absTol, double relTol,
                   int nNew, int nMin, double flatness, 
                   SEXP stateFile,
                   int seed, int flag, int cuba_args) {

    
    Rcpp::NumericVector integral(nComp);
    Rcpp::NumericVector errVals(nComp);
    Rcpp::NumericVector prob(nComp);

    // Create a structure to hold integrand function and initialize it
    integrand_info II;
    II.cuba_args = cuba_args; /* suave specific args */
    II.fun = f;                 /* R function */
    II.count = 0;
    II.vector_intf = (nVec > 1);
    
    int nregions, fail;

    // Set cores to be zero.
    cubacores(0, 0);
    
    char *filename = NULL;
    Rcpp::StringVector sv;
    if (!Rf_isNull(stateFile)) {
        sv = Rcpp::StringVector(stateFile);
        filename = sv(0);
    }
    Suave(nDim, nComp, (integrand_t) vegas_or_suave_fWrapper, (void *) &II, nVec,
          relTol, absTol, flag,
          seed, minEval, maxEval,
          nNew, nMin, flatness, 
          filename,
          NULL,
          &nregions, &(II.count), &fail,              
          integral.begin(), errVals.begin(), prob.begin());

  return Rcpp::List::create(
			    Rcpp::_["integral"] = integral,
			    Rcpp::_["error"] = errVals,
			    Rcpp::_["neval"] = II.count,
                            Rcpp::_["prob"] = prob,
			    Rcpp::_["returnCode"] = fail);
}

// Divonne

void peak_finder(const int *nDim, const double b[],
                 int *n, double x[], void *userdata) {

    ii_ptr iip = (ii_ptr) userdata;
    Rcpp::NumericMatrix bVal = Rcpp::NumericMatrix(2, *nDim, b); /* The b for the peakFinder */
    Rcpp::IntegerVector nVal = Rcpp::IntegerVector(n, n + 1);    /* The max number of pts to write */
    Rcpp::NumericMatrix fx = Rcpp::Function(iip -> peakFinder)(bVal, nVal);

    /* Update the actual number of points */
    *n = fx.nrow();
    double* fxp = fx.begin();         /* The ptr to f(x) (real) vector */
    for (int i = 0; i < (*n) * (*nDim); ++i) {
        x[i] = fxp[i];
    }
}

int divonne_fWrapper(const int *nDim, const double x[],
                   const int *nComp, double f[], void *userdata, const int *nVec,
                   const int *core, const int *phase) {

    Rcpp::NumericVector xVal = Rcpp::NumericVector(x, x + (*nDim) * (*nVec));  /* The x argument for the R function f */
    ii_ptr iip = (ii_ptr) userdata;
    if (iip -> vector_intf) {
        // Make the argument vector appear as a matrix for R
        xVal.attr("dim") = Rcpp::Dimension(*nDim, *nVec);
    }

    Rcpp::NumericVector fx;

    if (iip -> cuba_args) {
        Rcpp::IntegerVector phaseVal = Rcpp::IntegerVector(phase, phase + 1);  /* The phase argument for the R function f */        
        fx = Rcpp::Function(iip -> fun)(xVal, Rcpp::_["cuba_phase"] = phaseVal);
    } else {
        fx = Rcpp::Function(iip -> fun)(xVal);
    }
    double* fxp = fx.begin();         /* The ptr to f(x) (real) vector */
    for (int i = 0; i < (*nComp) * (*nVec); ++i) {
        f[i] = fxp[i];
    }
    return 0;
}

// [[Rcpp::export]]
Rcpp::List doDivonne(int nComp, SEXP f, int nDim,
                     int nVec, int minEval, int maxEval,
                     double absTol, double relTol,
                     int key1, int key2, int key3,
                     int maxPass, double border,
                     double maxChisq, double minDeviation,
                     int nGiven, int ldxGiven,
                     SEXP xGiven, int nExtra,
                     SEXP peakFinder,
                     SEXP stateFile,
                     int seed, int flag, int cuba_args) {

    Rcpp::NumericVector integral(nComp);
    Rcpp::NumericVector errVals(nComp);
    Rcpp::NumericVector prob(nComp);

    // Create a structure to hold integrand function and initialize it
    integrand_info II;
    II.cuba_args = cuba_args;   /* cuba specific args */
    II.fun = f;                 /* R function */
    II.count = 0;
    int peak_finder_null = Rf_isNull(peakFinder);
    if (!peak_finder_null) {
        II.peakFinder = peakFinder;
    }
    II.vector_intf = (nVec > 1);
    
    int nregions, fail;

    // Set cores to be zero.
    cubacores(0, 0);
    
    char *filename = NULL;
    Rcpp::StringVector sv;
    if (!Rf_isNull(stateFile)) {
        sv = Rcpp::StringVector(stateFile);
        filename = sv(0);
    }

    double* xGivenPtr = NULL; /* Ptr to xGiven start */
    Rcpp::NumericMatrix xGivenVal;
    if (!Rf_isNull(xGiven)) {
        xGivenVal = Rcpp::NumericMatrix(xGiven);
        xGivenPtr = xGivenVal.begin();
    }

    Divonne(nDim, nComp, (integrand_t) divonne_fWrapper, (void *) &II, nVec,
            relTol, absTol, flag,
            seed, minEval, maxEval,
            key1, key2, key3,
            maxPass, border, maxChisq, minDeviation,
            nGiven, ldxGiven, xGivenPtr,
            nExtra,
            peak_finder_null ? NULL : (peakfinder_t) peak_finder,
            filename,
            NULL,
            &nregions, &(II.count), &fail,              
            integral.begin(), errVals.begin(), prob.begin());
    
    return Rcpp::List::create(
                              Rcpp::_["integral"] = integral,
                              Rcpp::_["error"] = errVals,
                              Rcpp::_["neval"] = II.count,
                              Rcpp::_["prob"] = prob,
                              Rcpp::_["returnCode"] = fail);
}