[R] Viewing function source

Paul Meagher paul at datavore.com
Tue Aug 26 20:52:41 CEST 2003

> Just type the name of the function to see the R code
>   > rpois
>   function (n, lambda)
>   .Internal(rpois(n, lambda))
> But in this case it tells you that rpois is implemented in C code :(
>  By convention, it is likely to be a function called do_rpois,
>  however in this case we have an exception to the convention.  You can
> look in src/main.names.c for the name of the C function.
> abacus% fgrep rpois names.c
> {"rpois",       do_random1,     3,      11,     2,      {PP_FUNCALL,
> PREC_FN,0}},
> so the function do_random1 is actually involved. If you grep for that, you
> are directed to src/main/random.c and you will eventually find that the
> underlying code is in src/nmath/rpois.c and is commented with a reference
> to the ACM Transactions on Mathematical Software.

Thank you for helping me with the research on this.

I am interested in the rpois source in particular because I am wanting to
implement a compact algorithm for it.

The rpois.c code is estimated at around 140 lines of code when you subtract
out line spaces and commenting.   Fairly inscrutable if you haven't read the
article or don't have a mathematical programming background.

I had hoped that I would see a compact algorithm of around 20 lines of code
based on a counting process sensitive to a lambda parameter.  Instead it
uses a normal deviates method that is probably much more optimal in many
mathematical senses.

Personally, I would like to see a counting process implementation of an
poisson random number generator.  I suspect it would be much slower than
rpois.c (because it would likely depend upon setting a num_frames iteration
counter) and less accurate, but would be much more compact and give more
intuitive insight and understanding of a numerical process that generates
the poisson random deviates.  I am not very fluent yet in R programming to
attempt this.  I could take a stab at it with PHP which I am much more
fluent in if I am not barking up the wrong tree on this conjecture.

Paul Meagher

> -thomas

More information about the R-help mailing list