[Rcpp-devel] Named vectors

Romain Francois romain at r-enthusiasts.com
Thu Sep 9 20:20:44 CEST 2010


Le 09/09/10 18:09, Dirk Eddelbuettel a écrit :
>
> On 9 September 2010 at 08:52, Andrew Redd wrote:
> | What is the appropriate way to use/convert named vectors in C++ with
> | Rcpp.  Basically I have a named vector of parameters that pass into
> | the function.  I cannot be certain of their order, so would like to
> | extract them by name.  For those initialized in C++ a
> | std:map<std::string, double>  should work, but how do declare one from
> | a SEXP pointer that is passed in?  For example:
> |
> | draw<- cxxfunction(signature(A="numeric",t="numeric", params="numeric"),body="
> |     SEXP draw( SEXP A, SEXP t, SEXP params){
> |       Rcpp::NumericVector Admissions(A);
> | 			Rcpp::NumericVector T(t);
> | 			std::map<std::string, double>  Params(params);
> | 			std::vector<double>  draws;
> | 			for(int i=0;i<params[\"n\"];i++){
> | 				draws.pushback(rbinom(Admissions[T[i]+1,2], 1/(1+exp(-params[\"nu\"])))));
> | 			}
> | 			return wrap(draws);
> |     }
> | 	",plugin='Rcpp')
> | This of course does not compile because of std::map<std::string,
> | double>  Params(params); is not valid.  But is shows more of less what
> | I'm trying to do.
>
> There were numerous other errors I fixed:
>
> 1)  Do not add a function header as inline does that for you.
>
> 2)  Do not add a final } either.
>
> 3)  Your Params can be passed to a list as I showed.
>
> 4)  I do not understand what Admissions[T[i]+1,2] could possible do for a
>      NumericVector Admissions. Did you mean a matrix?  Changed to vector.
>
> 5)  rbinom is now defined in SVN (wait for 0.8.6 "soon"). I presume you meant
>      the function from R itself returning a double so I changed that with
>      explicit prefixes.
>
> So this now builds for me:
>
>
>
>> draw<- cxxfunction(signature(A="numeric",t="numeric", params="numeric"),body="
> +       Rcpp::NumericVector Admissions(A);
> +       Rcpp::NumericVector T(t);
> +       Rcpp::List Params(params);
> +       int len = Rcpp::as<int>(Params[\"n\"]);
> +       Rcpp::NumericVector draws(len);
> +       for(int i=0; i<len; i++){
> +           draws[i] = ::Rf_rbinom(Admissions[T[i]], 1/(1+exp(-Rcpp::as<double>(Params[\"nu\"]))));
> +       }
> +       return draws;
> + ",plugin='Rcpp')
>>
>
> We don;t need the Rcpp:: prefixes but I like'em too.

Same with Rcpp::NumericVector instead of Rcpp::List, which spares one 
call to "as.list".

Also : 'x[i,j]' is not legal C++ code. So if Admissions really is a 
matrix, you could use the "(" instead of "[" :

Admissions( T[i+1], 2 )

but given you always use the second column, maybe something like this 
would work for you:

draw <- cxxfunction(signature(A="numeric",t="numeric", 
params="numeric"),body='
	NumericMatrix Admissions(A);
	NumericMatrix::Column col2 = Admissions.column(2) ;
	
	NumericVector T(t);
	NumericVector Params(params);
	int n = static_cast<int>( Params["n"] ) ;
	NumericVector draws( n ) ;
	double nu = Params["nu"] ;
	for(int i=0;i<n;i++){
		draws[i] = ::Rf_rbinom( col2[i], 1/(1+exp(-nu)) ) ;
	}
	return draws;
',plugin='Rcpp')

Perhaps also taking ' 1/(1+exp(-nu)) ' out of the loop since it does not 
change.

Romain

>  From your next mail:
>
> On 9 September 2010 at 09:43, Andrew Redd wrote:
> | That is good to know, and if it were up to me I would be using lists
> | or environments. Unfortunately, I'm working with writing compiled code
> | for use with the pomp package that has the constraint that model
> | parameters are only passed on as a named numeric vector.  So a way to
> | convert the named vector into an equivalent structure would be
> | helpful.
>
> a) I'd write a quick utility function to convert.
>
> b) You could try as.list() as well.
>
>
> Hth, Dirk
>
>
>


-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/bzoWrs : Rcpp svn revision 2000
|- http://bit.ly/b8VNE2 : Rcpp at LondonR, oct 5th
`- http://bit.ly/aAyra4 : highlight 0.2-2




More information about the Rcpp-devel mailing list