bc.b

Broadcasted Boolean Operations

Description

The bc.b() function performs broadcasted Boolean operations on 2 arrays.

Please note that these operations will treat the input as logical.
Therefore, something like bc.b(1, 2, “==”) returns TRUE, because both 1 and 2 are TRUE when treated as logical.

Usage

bc.b(x, y, op, ...)

## S4 method for signature 'ANY'
bc.b(x, y, op)

Arguments

x, y conformable arrays of type logical, integer(32 bit), or raw.
op a single string, giving the operator.
Supported Boolean operators: &, |, xor, nand, ==, !=, <, >, <=, >=.
further arguments passed to or from methods.

Details

bc.b() efficiently casts the input to logical without making copies of the entire vectors/arrays.
Since the input is treated as logical, the following equalities hold for bc.b():

  • "==" is equivalent to (x & y) | (!x & !y), but faster;

  • "!=" is equivalent to xor(x, y);

  • "<" is equivalent to (!x & y), but faster;

  • ">" is equivalent to (x & !y), but faster;

  • "<=" is equivalent to (!x & y) | (y == x), but faster;

  • ">=" is equivalent to (x & !y) | (y == x), but faster.

Value

A logical array as a result of the broadcasted Boolean operation.

Examples

library("broadcast")

x.dim <- c(4:2)
x.len <- prod(x.dim)
x.data <- sample(c(TRUE, FALSE, NA), x.len, TRUE)
x <- array(x.data, x.dim)
y <- array(1:50, c(4,1,1))

bc.b(x, y, "&")
## , , 1
## 
##       [,1]  [,2]  [,3]
## [1,]    NA    NA  TRUE
## [2,] FALSE  TRUE    NA
## [3,]  TRUE    NA FALSE
## [4,] FALSE FALSE  TRUE
## 
## , , 2
## 
##       [,1] [,2]  [,3]
## [1,]  TRUE TRUE    NA
## [2,]    NA   NA    NA
## [3,] FALSE   NA    NA
## [4,]    NA   NA FALSE
bc.b(x, y, "|")
## , , 1
## 
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
## [4,] TRUE TRUE TRUE
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
## [4,] TRUE TRUE TRUE
bc.b(x, y, "xor")
## , , 1
## 
##       [,1]  [,2]  [,3]
## [1,]    NA    NA FALSE
## [2,]  TRUE FALSE    NA
## [3,] FALSE    NA  TRUE
## [4,]  TRUE  TRUE FALSE
## 
## , , 2
## 
##       [,1]  [,2] [,3]
## [1,] FALSE FALSE   NA
## [2,]    NA    NA   NA
## [3,]  TRUE    NA   NA
## [4,]    NA    NA TRUE
bc.b(x, y, "nand")
## , , 1
## 
##       [,1]  [,2]  [,3]
## [1,] FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE
## [3,] FALSE FALSE FALSE
## [4,] FALSE FALSE FALSE
## 
## , , 2
## 
##       [,1]  [,2]  [,3]
## [1,] FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE
## [3,] FALSE FALSE FALSE
## [4,] FALSE FALSE FALSE
bc.b(x, y, "==")
## , , 1
## 
##       [,1]  [,2]  [,3]
## [1,]    NA    NA  TRUE
## [2,] FALSE  TRUE    NA
## [3,]  TRUE    NA FALSE
## [4,] FALSE FALSE  TRUE
## 
## , , 2
## 
##       [,1] [,2]  [,3]
## [1,]  TRUE TRUE    NA
## [2,]    NA   NA    NA
## [3,] FALSE   NA    NA
## [4,]    NA   NA FALSE
bc.b(x, y, "!=")
## , , 1
## 
##       [,1]  [,2]  [,3]
## [1,]    NA    NA FALSE
## [2,]  TRUE FALSE    NA
## [3,] FALSE    NA  TRUE
## [4,]  TRUE  TRUE FALSE
## 
## , , 2
## 
##       [,1]  [,2] [,3]
## [1,] FALSE FALSE   NA
## [2,]    NA    NA   NA
## [3,]  TRUE    NA   NA
## [4,]    NA    NA TRUE