aaa01_broadcast_operators: Details on Broadcasted Operators

aaa01_broadcast_operatorsR Documentation

Details on Broadcasted Operators

Description

Base 'R' comes with relational, arithmetic, integer, logical, and bit-wise operators.
'broadcast' provides 2 ways to use these operators with broadcasting.

The first (and simple) way is to use the broadcaster class, which comes with it's own method dispatch for the above mentioned operators.
This approach supports operator precedence, and for the average 'R' user, this is sufficient.

The second way is to use the large set of bc.- functions.
These offer much greater control and more operators than the previous method, and has less risk of running into conflicting methods.
But they do not support operator precedence.

Operators Overloaded via Broadcaster Class

The 'broadcast' package provides the broadcaster class, which comes with its own method dispatch for the base operators.
If at least one of the 2 arguments of the base operators has the broadcaster class attribute, and no other class (like bit64) interferes, broadcasting will be used.

The following operators have a 'broadcaster' method:

# Group      : Operators             ; Comments
# relational : ==, !=, <, >, <=, >=  ;
# arithmetic : +, -, *, /, ^         ;
# integer    : %%, %/%               ; round2int53() will be applied to input
# logical    : &, |                  ;
# bit-wise   : &, |                  ; if both sides are type of raw

See also the Examples section below.
Unary operations (i.e. + x, - x) return the original object, with only the sign adjusted.

Available bc. functions

'broadcast' provides a set of functions for broadcasted element-wise binary operations with broadcasting.
These functions use an API similar to the outer function.

The following functions for simple operations are available:

  • bc.rel: General relational operations (⁠==, !=, <, >, <=, >=⁠).
    The various other bc. methods below have specialised relational operators available for specialised needs.

  • bc.b: Boolean/logical operations;

  • bc.i: integer operations;

  • bc.d: decimal arithmetic operations;

  • bc.cplx: complex arithmetic operations;

  • bc.str: string (in)equality, concatenation, and distance operations;

  • bc.raw: operations that take in arrays of type raw and return an array of type raw;

  • bc.bit: BIT-WISE operations, supporting the raw and integer types;

  • bc.list: apply any 'R' function to 2 recursive arrays with broadcasting.

Examples


# maths ====

x <- sample(as.double(1:10))
y <- sample(as.double(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 <- as.double(1:10)
y <- array(as.double(1:10), c(1, 10))
mbroadcasters(c("x", "y"), TRUE)

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



broadcast documentation built on Nov. 28, 2026, 1:07 a.m.