broadcast Package Overview

Description

broadcast:
Efficient Broadcasted Array Operations Like ‘NumPy’

Implements efficient ‘NumPy’-like broadcasted operations for atomic and recursive arrays.

In the context of operations involving 2 (or more) arrays, “broadcasting” (AKA singleton expansion) refers to efficiently recycling array dimensions, without making copies.

Besides linking to ‘Rcpp’, ‘broadcast’ does not use any external libraries or generative AI in any way; ‘broadcast’ was essentially made from scratch and can be installed out-of-the-box.

The implementations available in ‘broadcast’ include, but are not limited to, the following:

  1. Broadcasted element-wise operations on any 2 arrays; they support a large set of relational, arithmetic, Boolean, string, and bit-wise operations.

  2. A faster, more memory efficient, and broadcasted abind-like function, for binding arrays along an arbitrary dimension.

  3. Broadcasted ifelse-like and apply-like functions.

  4. Casting functions, that cast subset-groups of an array to a new dimension, cast nested lists to dimensional lists, and vice-versa.

  5. A few linear algebra functions for statistics.

The functions in the ‘broadcast’ package strive to minimize computation time and memory usage (which is not just better for efficient computing, but also for the environment).

Methods/Functions

Broadcasted Operators
Base ‘R’ comes with relational (==, !=, etc.), arithmetic (+, -, *, /, etc.), and logical/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 method support 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 it does not support operator precedence.

More information about both methods can be found here:
broadcast_operators.

Binding Arrays
‘broadcast’ provides the bind_array function, to bind arrays along an arbitrary dimension, with support for broadcasting.
See bind_array.

Casting Methods
‘broadcast’ provides several "casting" methods.
These can facility complex forms of broadcasting that would normally not be possible.
But these "casting" methods also have their own merit, beside empowering complex broadcasting.

More information about the casting methods can be found here:
broadcast_casting.

General Pairwise Broadcasted Methods
‘broadcast’ also comes with a few general pairwise broadcasted methods:

  • bc_ifelse: Broadcasted version of ifelse.

  • bcapply: Broadcasted apply-like method.

  • bc_strrep: Broadcasted version of strrep.

Other functions
‘broadcast’ provides type-casting functions, which preserve names and dimensions - convenient for arrays.

‘broadcast’ also provides simple linear algebra functions for statistics.

And ‘broadcast’ comes with some helper functions:
checkNA, checkNULL,
bc_dim, ndim, lst.ndim, rep_dim,
vector2array.

Supported Structures

‘broadcast’ supports atomic/recursive arrays (up to 16 dimensions), and atomic/recursive vectors.
As in standard Linear Algebra convention, dimensionless vectors are interpreted as column-vectors in broadcasted array operations.

Method dispatch and Attribute handling

The broadcasted methods (i.e. all bc_ methods, all bc. methods, and the operaters overloaded by the broadcaster class) generally only preserve the "special" attributes between the arrays involved:
names, dimnames, comment (if appropriate), and the broadcaster class attribute (and related attributes).
Other (usually class-specific) attributes are not preserved , unlike the base ‘R’ operators.

The reason only special attributes are preserved, is because broadcasting often results in an object with more dimensions, larger dimensions, and/or larger length than the original objects.
This is relevant as some class-specific attributes are only appropriate for certain dimensions or lengths.
Custom matrix classes, for example, presumes an object to have exactly 2 dimensions.
And the various classes provided by the ‘bit’ package have length-related attributes.
So class attributes cannot be guaranteed to hold for the resulting objects when broadcasting is involved.

Almost all functions provided by ‘broadcast’ are S3 or S4 generics;
methods can be written for them for specific classes, so that class-specific attributes can be supported when needed.

Author(s)

Author, Maintainer: Tony Wilkes tonywilkes.nl@gmail.com (ORCID)

References

Harris, C.R., Millman, K.J., van der Walt, S.J. et al. Array programming with NumPy. Nature 585, 357–362 (2020). doi:10.1038/s41586-020-2649-2. (Publisher link).