[Rcpp-devel] more on stats functions

Romain Francois romain at r-enthusiasts.com
Wed Dec 8 09:18:12 CET 2010


Le 08/12/10 05:25, Christian Gunning a écrit :
>> Your contributions are useful. It helps us understanding how people from
>> outside the team use Rcpp.
>
> Thanks.  As per Dirk's comment, I went back to the unit tests and will
> use those as guidance for quickref.
>
> The problem I got stuck on looks to be the use of int versus double
> values for dnorm's sd argument, due to argument ambiguity:
>
> This works (copied from unitTests/runit.stats.R):
>
> dtest1 = cxxfunction(signature( x = "numeric" ),
>            'NumericVector xx(x);
>             return( xx = dnorm( xx, 0.0, 1.0));'
>   ,plugin='Rcpp')
>
> This doesn't compile (copied from Rcpp-sugar.pdf):
>
> dtest2 = cxxfunction(signature( x = "numeric" ),
>            'NumericVector xx(x);
>             return(xx = dnorm( xx, 0, 1));'
>   ,plugin='Rcpp')
>
> Compilation ERROR, function(s)/method(s) not created!
> file49322701.cpp: In function ‘SEXPREC* file49322701(SEXPREC*)’:
> file49322701.cpp:30: error: call of overloaded
> ‘dnorm4(Rcpp::NumericVector&, int, int)’ is ambiguous
> /home/xian/R/x86_64-pc-linux-gnu-library/2.8/Rcpp/include/Rcpp/stats/norm.h:107:
> note: candidates are: Rcpp::stats::D1<RTYPE, NA, T>  Rcpp::dnorm4(const
> Rcpp::VectorBase<RTYPE, NA, VECTOR>&, double, bool) [with int RTYPE =
> 14, bool NA = true, T = Rcpp::Vector<14>]
> /home/xian/R/x86_64-pc-linux-gnu-library/2.8/Rcpp/include/Rcpp/stats/norm.h:108:
> note:                 Rcpp::stats::D2<RTYPE, NA, T>  Rcpp::dnorm4(const
> Rcpp::VectorBase<RTYPE, NA, VECTOR>&, double, double, bool) [with int
> RTYPE = 14, bool NA = true, T = Rcpp::Vector<14>]
> make: *** [file49322701.o] Error 1

Yep. Thanks.

The compiler cannot decide between these:

dnorm( NumericVector, double, bool log = true)
dnorm( NumericVector, double, double, bool log = true )

It might make sense to get rid of the log = argument and maybe add a 
ldnorm, or log_dnorm, or dnorm_log instead. so that there is no ambiguity.

> This works:
>
> dtest2a = cxxfunction(signature( x = "numeric" ),
>            'NumericVector xx(x);
>             return(xx = dnorm( xx, 0, 1, true));'
>   ,plugin='Rcpp')
>
> This works, and is equivalent to dnorm(-2:2, 0, log=T) in R:
>
> dtest3 = cxxfunction(signature( x = "numeric" ),
>            'NumericVector xx(x);
>             return(xx= dnorm( xx, 0, true));'
>   ,plugin='Rcpp')
> x=-2:2; identical(dtest3(x),  dnorm(x, 0, log=T))
>
> This works, and is equivalent to dnorm(-2:2, 0, log=F) in R:
>
> dtest4 = cxxfunction(signature( x = "numeric" ),
>            'NumericVector xx(x);
>             return(xx = dnorm( xx, 0, false));'
>   ,plugin='Rcpp')
> x=-2:2; identical(dtest4(x),  dnorm(x, 0, log=F))
>
> To avoid ambiguity, is it reasonable to recommend that users of the
> stats functions provide all scalar arguments as doubles rather than
> integers, as is done in the unit tests (but not in Rcpp-sugar.pdf)?
> e.g.
>    pgamma( xx, 2.0, 1.0 )
> vs.
>    pgamma( xx, 2, 1 )
>
> If so, I'll also patch Rcpp-sugar.pdf to reflect this.

I think this is a good recommendation. There might be something we can 
do code-wise to prevent these errors.

> In addition, some user guidance on what arguments are required for the
> stats functions should cut down on questions in the future (esp. since
> template compile errors can be daunting). For rnorm, all 3 of these
> work:
>
> rtest = cxxfunction(,
>            'NumericVector xx = rnorm( 5);
>             xx = rnorm( 5, 0.0);
>             return(xx = rnorm( 5, 0.0, 1.0));'
>   ,plugin='Rcpp')
>
> With dnorm, a smaller subset work (commented don't compile):
>
> dtest5 = cxxfunction(signature( x = "numeric" ),
>            'NumericVector xx(x), yy(x);
>             //yy = dnorm( xx);
>             yy = dnorm( xx, 0.0);
>             yy = dnorm( xx, 0.0, 1.0);
>             yy = dnorm( xx, 0.0, 1.0, true);
>             yy = dnorm( xx, 0.0, true);
>             //yy = dnorm( xx, true);
>             return(yy);'
>   ,plugin='Rcpp')

Thanks. fixed now.

It would be great to recycle this effort of yours into testing. Can you 
have a look at the runit.stats.R file and add some test cases.

Thanks again.

Romain

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/fT2rZM : highlight 0.2-5
|- http://bit.ly/gpCSpH : Evolution of Rcpp code size
`- http://bit.ly/hovakS : RcppGSL initial release




More information about the Rcpp-devel mailing list