0

I am trying to obtain a logical vector from two other logical vectors connected through elementwise '&':

//[[Rcpp::export]]
arma::uvec test1(arma::vec t1, double R1, double R2){
arma::uvec t = (t1 >= R1) & (t1 < R2);
return t;
}

It returns the following error while I try to compile

error: no match for 'operator&' (operand types are 'arma::enable_if2<true, const arma::mtOp<unsigned int, arma::Col<double>, arma::op_rel_gteq_post> >::result {aka const arma::mtOp<unsigned int, arma::Col<double>, arma::op_rel_gteq_post>}' and 'arma::enable_if2<true, const arma::mtOp<unsigned int, arma::Col<double>, arma::op_rel_lt_post> >::result {aka const arma::mtOp<unsigned int, arma::Col<double>, arma::op_rel_lt_post>}')
arma::uvec t = (t1 >= R1) & (t1 < R2);
                          ^

I have no idea what was going on. I am guessing that Armadillo does things differently. But I can't find any sources to help me clear things out. Any help would be appreciated! Thank you very much!

NamelessGods
  • 185
  • 6
  • 2
    Did you mean `&&` instead of `&` ? The operators `&&` and `&` have completely different meaning, even if they look related. `&&` means "logical and". `&` means "bitwise and". Try changing your code to: `arma::uvec t = (t1 >= R1) && (t1 < R2);` – mtall Sep 29 '19 at 12:50
  • && does work but it doesn't give me the right answer. I need the elementwise &. && seems to just go through the shortcut option. – NamelessGods Sep 29 '19 at 15:41
  • @davidolohowski What does elementwise mean when your comparisons are doubles? – duckmayr Sep 29 '19 at 17:15
  • @duckmayr Sorry I wasn't clear enough. I want the result to be a vector of true or false. But && only gives me one single true or false answer which is not something I want. But weird thing is && has no compiling issue while & does. – NamelessGods Sep 29 '19 at 19:03
  • @davidolohowski See the updates to my answer -- I think it should work how you want it – duckmayr Sep 30 '19 at 00:51

1 Answers1

3

I have no idea what was going on. I am guessing that Armadillo does things differently. But I can't find any sources to help me clear things out.

The ultimate source here is the Armadillo docs. If you go down to the section on operators, you'll see that the & operator is not one of those listed among the "[o]verloaded operators for Mat, Col, Row and Cube classes." So, if you want such an operator you'll have to code it up yourself (or see if someone else already has it floating around the internet). There is such an operator for Rcpp::NumericVectors:

#include <Rcpp.h>

// [[Rcpp::export]]
Rcpp::LogicalVector test1(const Rcpp::NumericVector& t1, double R1, double R2){
    return (t1 >= R1) & (t1 < R2);
}
test1(1:10, 3, 7)
# [1] FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE
# [9] FALSE FALSE

Of course, that doesn't help much if the rest of your code really relies on Armadillo.

Update: Just Use &&

As pointed out by mtall in the comments, the && operator actually is available, even though it's not discussed in the Armadillo docs (maybe it's not as ultimate of a source as I thought).

So, just change your code to the following:

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

//[[Rcpp::export]]
arma::uvec test1(arma::vec t1, double R1, double R2){
    arma::uvec t = (t1 >= R1) && (t1 < R2);
    return t;
}

and it works how I believe you want it to based on your question and response to comments:

test1(1:10, 3, 7)
      [,1]
 [1,]    0
 [2,]    0
 [3,]    1
 [4,]    1
 [5,]    1
 [6,]    1
 [7,]    0
 [8,]    0
 [9,]    0
[10,]    0
duckmayr
  • 16,303
  • 3
  • 35
  • 53
  • Perhaps there is a confusion between `&&` and `&` ? – mtall Sep 29 '19 at 12:52
  • @mtall I believe so; that was my first thought, and I should have just tried the switch as you suggest in the comments. However, like a sucker I looked at the docs first and didn't see that operator listed. Having tried it, I see it works. I wonder why the `&&` operator is not listed in the docs? – duckmayr Sep 29 '19 at 13:02
  • @mtall Would you prefer to add your own answer re: this issue? If not, I'll update mine to include your suggestion. – duckmayr Sep 29 '19 at 13:03
  • 1
    @duckmayr Thanks, it did work. It was in fact my own mistake. I took it for granted since R produce one single valued bool answer if && is used. It was actually some other parts of my code had a bug that caused the problem. Apologies. – NamelessGods Sep 30 '19 at 04:12