[Rcpp-devel] R.e.: Problems with rlnorm

Christian Gunning xian at unm.edu
Mon May 30 05:17:30 CEST 2011


On Sun, May 29, 2011 at 3:29 PM,
<rcpp-devel-request at r-forge.wu-wien.ac.at> wrote:
>
> I reviewed the discussion at:
> http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2010-September/0
> 01078.html
>
> This made me realize that I should go ahead and code a simple putcol
> function loop when I need to populate a column in a Matrix "array".  (I
> had spent some time looking for such a defined method.)
>
> An array or Matrix class would be nice, but for now I seem to be able to
> find work-arounds.

Take a look at the quickref in the section "Using matrices",
particularly the part about "_" -- Romain has done a lot of work since
the conversation that you reference :)
Also, see previous list message from Vincent and Dirk for namespace
notes on using _ outside of inline.

> Now I am calling the r-prefixed distributions with success, but this
> same code will fail on compile if the rlnorm() function is called.
> compile error if rlnorm is called instead of rnorm:
> "file3a7d3a6f.cpp:38:28: error: 'rlnorm' was not declared in this scope"

It's working now in the most recent SVN version (I'm guessing
someone's already pointed this out).

Below is an example that includes by-column assignment.  Also, unless
you need to reset the seed mid-function-call, RNGScope grabs the seed
from R, so you can use a regular set.seed call.  So, the return value
of the following is identical to that of your example:

require(inline)
src <- '
Rcpp::RNGScope Scope; // gets value from set.seed call in R
int NumRands = 5;
int NumTrials = 3;
Rcpp::NumericMatrix RandVals(NumRands, NumTrials*2);
RandVals(_,3)=rlnorm(NumRands,2.0,1.0);
return RandVals;
'
 fun1 <- cxxfunction(signature(),
 src, plugin = "Rcpp")

set.seed(20)
fun1()

-Christian

-- 
A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal – Panama!


More information about the Rcpp-devel mailing list