[Rcpp-devel] Sample function(s) for inclusion in RcppArmadillo
Perfect timing. Meant to email you yesterday regarding sample(), but that fell off the hectic wagon. We should get back to this.
On 25 December 2012 at 01:39, Christian Gunning wrote:
| I'm working on writing runit tests for the previously-discussed
| sample, and I just hit my competence threshold dealing with
| inst/include.
| As discussed, the (overloaded) sample() functions are to be called
| only from user C++ code and are thus not exposed to R.
| This means that the function definitions should go in inst/include, correct?
Yes.
| I don't really know what I'm doing here. If someone could recommend a
| good reference for namespace control, headers, and R-package specific
| issues I'd be grateful. Details follow:
|
| I'm trying to get the following test function to use RcppArmadillo's
| definition of sample, and the compiler never finds it:
| require(Rcpp)
| cppFunction( code='
| IntegerVector sample_int_noprob( IntegerVector x, int size,
| bool replace) {
| RNGScope scope;
| IntegerVector ret = RcppArmadillo::sample(x, size, replace);
| return ret;
| }',
| depends='RcppArmadillo'
| )
a) Is your #include file being loaded? Did you add a statement to your copy
of RcppArmadillo.h or some other header file? Else it is not included -- and
the compiler has no idea what you are talking about.
b) If a) is taken care of, is sample() defined inside 'namespace armadillo' ?
| I tried placing the definition of sample in
| RcppArmadillo/inst/include/sample.h.
| using namespace Rcpp;
| template <class T>
| T sample(const T &x, const int size, const bool replace,
| NumericVector prob_ = NumericVector(0) ) {
| // Templated sample -- should work on any Rcpp Vector
| ...
| }
That would be a no to b).
| I've tried to use RcppArmadilloWrap.h as an example, using:
| namespace Rcpp{
| namespace RcppArmadillo{
| ...
| }
| }
That would be two nested namespaces (which I don't we do for Armadillo) as in
Rcpp::RcppArmadillo::sample()
You don't really have a stanza as we haven't defined other utility functions
for RcppArmadillo yet.
If you have a simple clean diff we can maybe noodle over this this week.
Maybe you and should work on this off-list?
Dirk
| best,
| Christian
|
|
| On Thu, Nov 15, 2012 at 5:35 AM, Dirk Eddelbuettel <edd at debian.org> wrote:
| >
| > On 15 November 2012 at 04:00, Christian Gunning wrote:
| > | | Also, you may need to add RNGScope() if you use R's RNG. | > | | | > | | | > | | | > | | Yes, I should have mentioned that in my post. Calling RNGScope() is left | > | to | > | | the user, since multiple calls to sample() can conceivably happen inside | > | a | > | | single RNGScope(). | > | | > | With that, maybe best to be included as another example in RcppArmadillo ? | > | | > | | > | Why? The proposed behavior is the same as sugar's rnorm, etc.: a utility | > | function that uses R's RNG -- it's the | > | user's responsibility to properly scope the RNG. | > | > Fair point. | > | > | When time permits, I'll add Romain's comments on sugarizing sample (which I | > | *think* I understand), along with some testing code. | > | > Excellent plan. | > | > Dirk | > | > -- | > Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com | > | > | | | | -- | A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal ? Panama! | _______________________________________________ | Rcpp-devel mailing list | Rcpp-devel at lists.r-forge.r-project.org | https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com