[Rd] Fwd: Rcpp sugar dpois
Dirk Eddelbuettel
edd at debian.org
Tue Nov 3 13:23:17 CET 2015
On 3 November 2015 at 12:16, Enrico Bibbona wrote:
| Hi. Here is a piece of cpp code.
Somewhat wrong list -- try rcpp-devel for Rcpp questions.
| It works, but I do not understand the rational for the use of
| "R::dpois" to call the function dpois since in the examples I have always
Choice is good. In Rcpp, the R namespace also offers the scalar variants of
the d/p/q/r functions described in Rmath.h -- besides the vectorised variants
in the Rcpp namespace.
| found directly "dpois" or "Rcpp::dpois" that both do not work in my code.
As you do not show the non-working code, I cannot help with your problem.
Here is simple feasibility test:
R> cppFunction("NumericVector dp(NumericVector x, double lambda) { return dpois(x, lambda, false); }")
R> dp(0:4, 0.5)
[1] 0.60653066 0.30326533 0.07581633 0.01263606 0.00157951
R> dpois(0:4, 0.5)
[1] 0.60653066 0.30326533 0.07581633 0.01263606 0.00157951
R>
This uses Rcpp::dpois works with a vectorised first argument at the C++
level, and then behaves identical to the R function we call for
comparison. R::dpois, which you use, takes a scalar 'double' which is why you
need to loop by hand.
Please consider posting on Rcpp-devel next time. Remeber to subscribe before
you attempt to post.
Dirk
| Could anyone be so patient to explain me why should it be like that?
| Thaks a lot, Enrico
|
| #include <Rcpp.h>
| using namespace Rcpp;
| NumericVector pcUtildedatoU(NumericVector z, double j, double par)
|
| {
| int l_z=z.size();
| NumericVector out(l_z);
| for (int w=0; w<= (l_z-1); w++)
| {
| double outcand=0;
| double d=z[w];
| if (d >= j)
| outcand=1;
| else if (d>= -j)
| {
| for(int i=-j; i<=d; i+=2)
| outcand+= R::dpois((i+j)/2, par,false)* R::dpois((j-i)/2,
| par,false);
| outcand=outcand/R::dpois(j, 2*par,false);
| }
| out[w]=outcand;
| }
| return out;
| }
|
| --
| Enrico Bibbona
| Dipartimento di Matematica
| Università di Torino
| https://sites.google.com/site/enricobibbona/
|
|
|
| --
| Enrico Bibbona
| Dipartimento di Matematica
| Università di Torino
| https://sites.google.com/site/enricobibbona/
|
| [[alternative HTML version deleted]]
|
| ______________________________________________
| R-devel at r-project.org mailing list
| https://stat.ethz.ch/mailman/listinfo/r-devel
--
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
More information about the R-devel
mailing list