These functions perform logical operations on arrays, with broadcasting. They
power the logical operators of &
, |
, and !
with rrays, but are
also exported for use with base R objects.
rray_logical_and(x, y) rray_logical_or(x, y) rray_logical_not(x) rray_any(x, axes = NULL) rray_all(x, axes = NULL)
x, y | Vectors, matrices, arrays, or rrays. |
---|---|
axes | An integer vector specifying the axes to reduce over.
|
The value of the logical comparison, with broadcasting.
rray_any()
and rray_all()
return a logical object with the same shape
as x
everywhere except along axes
, which have been reduced to size 1.
The operators themselves rely on R's dispatching rules to
dispatch to the correct rray logical operator. When comparing rrays with
base R matrices and arrays, this generally works fine. However, if you
compare classed objects like factor("x") & rray(1)
then a fall through
error is thrown. There is nothing we can do about
this. See ?groupGeneric
for more information on this.
The behavior of comparing an array with a length 0 dimension
with another array is slightly different than base R since
broadcasting behavior is well defined. Length 0 dimensions are not exceptions
to the normal broadcasting rules. Comparing dimensions of 0
and 1
, the
common dimension is 0
because 1
always becomes the other dimension in the
comparison. On the other hand, comparing dimensions 0
and 2
is an error
because neither are 1
, and they are not identical.
x <- rray(TRUE, c(2, 2, 3)) y <- matrix(c(TRUE, FALSE)) # `TRUE` wherever `y` is broadcasted to be `TRUE` x & y#> <rray<lgl>[,2,3][2]> #> , , 1 #> #> [,1] [,2] #> [1,] TRUE TRUE #> [2,] FALSE FALSE #> #> , , 2 #> #> [,1] [,2] #> [1,] TRUE TRUE #> [2,] FALSE FALSE #> #> , , 3 #> #> [,1] [,2] #> [1,] TRUE TRUE #> [2,] FALSE FALSE #># --------------------------------------------------------------------------- # Behavior with edge cases x <- rray(TRUE, c(1, 2)) # The common dim is (0, 2) logical() & x#> <rray<lgl>[,2][0]> #> [,1] [,2]# You can have empty arrays with shape # The common dim is (0, 2, 2) y <- array(logical(), c(0, 1, 2)) x & y#> <rray<lgl>[,2,2][0]> #> , , 1 #> #> [,1] [,2] #> #> , , 2 #> #> [,1] [,2] #># You cannot broadcast dimensions # of 2 and 0. Following standard # broadcasting rules, they do not # match and neither are 1, so an # error should be thrown try(x & array(logical(), c(1, 0)))#> Error : Non-broadcastable dimensions: (1, 2) and (1, 0).