Learn R Programming

broadcast (version 0.1.7)

broadcaster: Check or Set if an Array is a Broadcaster

Description

broadcaster() checks if an array or vector has the "broadcaster" attribute.
bcr() is a short-hand alias for broadcaster().

broadcaster()<- (or bcr()<-) sets or un-sets the class attribute "broadcaster" on an array or vector.

mbroadcasters() sets or un-sets multiple objects in an environment as broadcaster.

The broadcaster class attribute exists purely to overload the arithmetic, Boolean, bit-wise, and relational infix operators, to support broadcasting.
This makes mathematical expressions with multiple variables, where precedence may be important, far more convenient.
Like in the following calculation:
x / (y + z)

See broadcast_operators for more information.

Usage

broadcaster(x)

broadcaster(x) <- value

mbroadcasters(nms, value, env = NULL)

bcr(x)

bcr(x) <- value

Value

For broadcaster():

TRUE if an array or vector is a broadcaster, or FALSE if it is not.


For broadcaster()<-:

Returns nothing, but sets (if right hand side is TRUE) or removes (if right hand side is FALSE) the "broadcaster" class attribute.


For mbroadcasters():

Returns nothing, but sets (if value = TRUE) or removes (value = FALSE) the "broadcaster" class attribute.

If value = TRUE, objects that cannot become a broadcaster or are already a broadcaster will be ignored.

If value = FALSE, objects that are not broadcasters (according to broadcaster()) will be ignored.


Arguments

x

object to check or set.
Only S3 vectors and arrays are supported, and only up to 16 dimensions.

value

set to TRUE to make an array a broadcaster, or FALSE to remove the broadcaster class attribute from an array.

nms

a character vector of variable names.

env

the environment where to look for the variable names specified in nms.
If NULL, the environment from which the function was called is used.

See Also

broadcast_operators

Examples

Run this code

# maths ====

x <- 1:10
y <- 1:10
dim(x) <- c(10, 1)
dim(y) <- c(1, 10)
broadcaster(x) <- TRUE
broadcaster(y) <- TRUE



x + y / x
(x + y) / x

(x + y) * x


# relational operators ====
x <- 1:10
y <- array(1:10, c(1, 10))
broadcaster(x) <- TRUE
broadcaster(y) <- TRUE

x == y
x != y
x < y
x > y
x <= y
x >= y




# maths ====

x <- sample(1:10)
y <- sample(1:10)
dim(x) <- c(10, 1)
dim(y) <- c(1, 10)
mbroadcasters(c("x", "y"), TRUE)



x + y / x
(x + y) / x

(x + y) * x


# relational operators ====
x <- 1:10
y <- array(1:10, c(1, 10))
mbroadcasters(c("x", "y"), TRUE)

x == y
x != y
x < y
x > y
x <= y
x >= y


Run the code above in your browser using DataLab