Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find minimum of vector in Rcpp

Tags:

c++

r

rcpp

Since last night I have been trying out Rcpp and inline, and so far I am really enjoying it. But I am kinda new to C in general and can only do basic stuff yet, and I am having a hard time finding help online on things like functions.

Something I was working on was a function that finds the minimum of a vector in the global environment. I came up with:

library("inline")
library("Rcpp")

foo <- rnorm(100)

bar <- cxxfunction( signature(),
'
Environment e = Environment::global_env();  
NumericVector foo = e["foo"];
int min;

for (int i = 0; i < foo.size(); i++)
{
    if ( foo[i] < foo[min] ) min = i;
}
return wrap(min+1);
', plugin = "Rcpp")

bar()

But it seems like there should be an easier way to do this, and it is quite slower than which.max()

system.time(replicate(100000,bar()))
   user  system elapsed 
   0.27    0.00    0.26 
system.time(replicate(100000,which.min(foo)))
   user  system elapsed 
    0.2     0.0     0.2 

Am I overlooking a basic c++ or Rcpp function that does this? And if so, where could I find a list of such functions?

I guess this question is related to: Where can I learn how to write C code to speed up slow R functions?

but different in that I am not really interested in how to incorporate c++ in R, but more on how and where to learn basic c++ code that is usable in R.

like image 507
Sacha Epskamp Avatar asked Mar 01 '11 17:03

Sacha Epskamp


People also ask

What is RCPP function?

Description The 'Rcpp' package provides R functions as well as C++ classes which offer a seamless integration of R and C++. Many R data types and objects can be mapped back and forth to C++ equivalents which facilitates both writing of new code as well as easier integration of third-party libraries.

How do you find the index of a minimum value in R?

Return the Index of the First Minimum Value of a Numeric Vector in R Programming – which. min() Function. which. min() function in R Language is used to return the location of the first minimum value in the Numeric Vector.

What is RCPP sugar?

Rcpp sugar brings a higher-level of abstraction to C++ code written using the Rcpp API. Rcpp sugar is based on expression templates (Abrahams and Gurtovoy, 2004; Vandevoorde and Josuttis, 2003) and provides some 'syntactic sugar' facilities directly in Rcpp.


1 Answers

Glad you are finding Rcpp useful.

The first comment by Billy is quite correct. There is overhead in the function lookup and there is overhead in the [] lookup for each element etc.

Also, a much more common approach is to take a vector you have in R, pass it to a compiled function you create via inline and Rcpp, and have it return the result. Try that. There are plenty of examples in the package and scattered over the rcpp-devel mailing list archives.

Edit: I could not resist trying to set up a very C++ / STL style answer.

R> src <- '
+   Rcpp::NumericVector x(xs);
+   Rcpp::NumericVector::iterator it =       // iterator type
+     std::min_element(x.begin(), x.end());  // STL algo
+   return Rcpp::wrap(it - x.begin()); '
R> minfun <- cxxfunction(signature(xs="numeric"), body=src, plugin="Rcpp")
R> minfun(c(7:20, 3:5))
[1] 14
R>

That is not exactly the easiest answer but it shows how by using what C++ offers you can find a minimum element without an (explicit) loop even at the C++ level. But the builtin min() function is still faster.

*Edit 2: Corrected as per Romain's comment below.

like image 108
Dirk Eddelbuettel Avatar answered Oct 18 '22 20:10

Dirk Eddelbuettel