4

I'm trying to pass a vector of bool as an argument to a function using Rcpparmadillo. A silly example looks like this:

// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>

// [[Rcpp::export]]
arma::mat myfun(arma::mat A, arma::vec mybool)
{
    int n = A.n_rows;
    arma::vec B(n);

    for(unsigned int i = 0; i < n; ++i)
    {
        if(mybool.row(i) && i < 10) // mybool.row(i) && throws the error
        {
            B.row(i) = arma::accu(A.row(i));
        }
        else
        {
            B.row(i) = pow(arma::accu(A.row(i)), 0.5);
        }
    }

    return B;
}

Here a mat<unsigned char> type is suggested but doesn't work for me. I've tried uvec and std::vector<bool> as well but doesn't work either. What is the best way to pass a logical vector as an argument using Rcpparmadillo?

Community
  • 1
  • 1
nopeva
  • 1,583
  • 5
  • 22
  • 38
  • Try to format your code in a more standard way. – Dirk Eddelbuettel Aug 31 '15 at 11:24
  • maybe this help: http://stackoverflow.com/questions/18899665/armadillo-c-linear-algebra-library-how-to-create-vector-of-boolean –  Aug 31 '15 at 11:26
  • 1
    The row member function returns a row vector from a matrix. In this case you have a vector so you want mybool(i) to return an element with bounds check or mybool[i] without bounds check. – Manos Nikolaidis Aug 31 '15 at 11:41

1 Answers1

4

You want uvec from Armadillo -- it does not have a bool type. Here is a reformatted version of your code which * used uvec * indexes vectors directly

// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>

// [[Rcpp::export]]
arma::mat myfun(arma::mat A, arma::uvec mybool) {
    unsigned int n = A.n_rows;
    arma::vec B(n);
    for (unsigned int i=0; i<n; ++i) {
        if (mybool[i] && i < 10) {
           B[i] = arma::accu(A.row(i)) ;
        } else {
           B[i] = pow(arma::accu(A.row(i)), 0.5);
        }
    } //end loop
    return B;
}

/*** R
A <- matrix(1:16,4,4)
mybool <- c(FALSE, TRUE, TRUE, FALSE)
myfun(A, mybool)
*/

If we sourceCpp() this, it runs the R at the bottom for us:

R> sourceCpp("/tmp/ap13.cpp")

R> A <- matrix(1:16,4,4)

R> mybool <- c(FALSE, TRUE, TRUE, FALSE)

R> myfun(A, mybool)
         [,1]
[1,]  5.29150
[2,] 32.00000
[3,] 36.00000
[4,]  6.32456
R> 
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Couple of points to note. (1). the function can be made more efficient by avoiding copying `A` and `mybook` at the start. It should be `arma::mat myfun(const arma::mat& A, const arma::uvec& mybool)`. (2) Data in `A` should be stored and accessed column-wise instead of row-wise, as Armadillo stores data in a column-by-column manner. – mtall Aug 31 '15 at 17:14
  • @mtall: You have the attentive eyes of a C/C++ programmer but I invite you to profile this. Our objects are actually thin proxies around underlying `SEXP` objects that are pointers. _So there are no copies_. You can now write via `const &` too _but performance will be the (about) same_. – Dirk Eddelbuettel Aug 31 '15 at 18:23