[Rcpp-devel] Is it possible to call an R internal C function from Rcpp?

Dirk Eddelbuettel edd at debian.org
Mon Mar 2 23:17:45 CET 2015

On 2 March 2015 at 14:04, steven pav wrote:
| I want to write the density function for the doubly non-central F distribution
| using Rcpp. The algorithm (from Paolella's Intermediate Probability, listing
| 10.8) computes the weighted sum of the PDF function of the singly non-central F
| distribution. This is available in R via df; the internal df function is
| apparently 
| function (x, df1, df2, ncp, log = FALSE) 
| {
|     if (missing(ncp)) 
|         .External(C_df, x, df1, df2, log)
|     else .External(C_dnf, x, df1, df2, ncp, log)
| }
| Is it possible to call this C_dnf from Rcpp? One thought I had was to use
| Rinside, but I want to avoid that overhead if possible.

"Quite possibly" in this particular case as we have both the sugar functions
for df and dnf, and the single-value wrapper of the Rmath functions.

For sugar, definitions are a tad, ahem, "cryptic" for use of macros. From 




where the key are '2' and '3' for the number of arguments, as above. These
become sugar functions
  Rcpp::df(), Rcpp::qf(), Rcpp::pf()
  Rcpp::dnf(), Rcpp::qnf(), Rcpp::pnf()

And from


    /* F Distibution */
    inline double df(double x, double df1, double df2, int lg)		{ return ::Rf_df(x, df1, df2, lg); }

    /* Non-central F Distribution */
    inline double dnf(double x, double df1, double df2, double ncp, int lg)		{ return ::Rf_dnf(x, df1, df2, ncp, lg); }

Give that a whirl, and benchmark carefully against known values.  Once you
have, please do send me a short writeup for the Rcpp Gallery now that we have
mathjax (yay!!) and graphs (yay!!) thanks to Jonathan.


http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org

More information about the Rcpp-devel mailing list