user8934968
user8934968

Reputation:

Rcpp use outer with pmax

I have got an R function which I need to calculate approximately one million times for vectors of length ~ 5000. Is there any possibily to speed it up by implementing it in Rcpp? I hardly worked with Rcpp before and the code below does not to work:

set.seet(1)
a <- rt(5e3, df = 2)
b <- rt(5e3, df = 2.5)
c <- rt(5e3, df = 3)
d <- rt(5e3, df = 3.5)
sum((1 - outer(a, b, pmax)) * (1 - outer(c, d, pmax)))
#[1] -367780.1

#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
double f_outer(NumericVector u, NumericVector v, NumericVector x, NumericVector y) {
double result = sum((1 - Rcpp::outer(u, v, Rcpp::pmax)) * (1 - Rcpp::outer(x, y, Rcpp::pmax)));
return(result);
}

Thank you very much!

Upvotes: 3

Views: 380

Answers (1)

duckmayr
duckmayr

Reputation: 16930

F. Privé is right -- we'll want to go with loops here; I've got the following C++ code in a file so-answer.cpp:

#include <Rcpp.h>

using namespace Rcpp;

// [[Rcpp::export]]
double f_outer(NumericVector u, NumericVector v, NumericVector x, NumericVector y) {
    // We'll use the size of the first and second vectors for our for loops
    int n = u.size();
    int m = v.size();
    // Make sure the vectors are appropriately sized for what we're doing
    if ( (n != x.size() ) || ( m != y.size() ) ) {
        ::Rf_error("Vectors not of compatible sizes.");
    }
    // Initialize a result variable
    double result = 0.0;
    // And use loops instead of outer
    for ( int i = 0; i < n; ++i ) {
        for ( int j = 0; j < m; ++j ) {
            result += (1 - std::max(u[i], v[j])) * (1 - std::max(x[i], y[j]));
        }
    }
    // Then return the result
    return result;
}

Then we see in R that the C++ code gives the same answer as your R code, but runs much faster:

library(Rcpp) # for sourceCpp()
library(microbenchmark) # for microbenchmark() (for benchmarking)
sourceCpp("so-answer.cpp") # compile our C++ code and make it available in R
set.seed(1) # for reproducibility
a <- rt(5e3, df = 2)
b <- rt(5e3, df = 2.5)
c <- rt(5e3, df = 3)
d <- rt(5e3, df = 3.5)
sum((1 - outer(a, b, pmax)) * (1 - outer(c, d, pmax)))
#> [1] -69677.99
f_outer(a, b, c, d)
#> [1] -69677.99
# Same answer, so looking good. Which one's faster?
microbenchmark(base = sum((1 - outer(a, b, pmax)) * (1 - outer(c, d, pmax))),
               rcpp = f_outer(a, b, c, d))
#> Unit: milliseconds
#>  expr       min        lq      mean    median        uq        max neval
#>  base 3978.9201 4119.6757 4197.9292 4131.3300 4144.4524 10121.5558   100
#>  rcpp  118.8963  119.1531  129.4071  119.4767  122.5218   909.2744   100
#>  cld
#>    b
#>   a

Created on 2018-12-13 by the reprex package (v0.2.1)

Upvotes: 4

Related Questions