broadcast Package Overview
Description
broadcast:
‘Numpy’-Like Broadcasted Operations for Atomic and Recursive Arrays with Minimal Dependencies.
Implements efficient broadcasted operations for atomic and recursive arrays.
Besides linking to ‘Rcpp’, ‘broadcast’ does not depend on, vendor, link to, include, or otherwise use any external libraries; ‘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:
-
Broadcasted element-wise operations on any 2 arrays; they support a large set of relational-, arithmetic-, Boolean-, string- and bit-wise operations.
-
A faster, more memory efficient, and broadcasted abind-like function, for binding arrays along an arbitrary dimension.
-
Broadcasted ifelse- and apply-like functions.
-
The
acast()
function, for casting/pivoting an array into a new dimension. -
Simple linear algebra functions for statistics.
The functions in the ‘broadcast’ package strive to minimize computation time and memory usage (which is not just good for efficient computing, but also for the environment).
Links to Get Started
-
GitHub main page: https://github.com/tony-aw/broadcast
-
Quick Start Guide: https://tony-aw.github.io/broadcast/vignettes/b_quickstart.html
-
Reporting Issues: https://github.com/tony-aw/broadcast/issues
Functions
Functions for broadcasted element-wise binary operations
‘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 binary operations are available:
-
bc.b: Boolean (i.e. logical) operations;
-
bc.i: integer arithmetic and relational operations;
-
bc.d: decimal arithmetic and relational operations;
-
bc.cplx: complex arithmetic and (in)equality operations;
-
bc.str: string (in)equality, concatenation, and distance operations;
-
bc.raw: byte- and relational operations for vectors/arrays of type
raw
; -
bc.bit: BIT-WISE operations, supporting the
raw
andinteger
types; -
bc.list: apply any ‘R’ function to 2 recursive arrays with broadcasting.
bind_array()
‘broadcast’ provides the bind_array function, to bind arrays along an arbitrary dimension, with support for broadcasting.
The API of bind_array()
is inspired by the fantastic abind::abind()
function by Tony Plare & Richard Heiberger (2016).
But bind_array()
differs considerably from abind::abind
in the following ways:
-
bind_array()
allows for broadcasting, whileabind::abind
does not support broadcasting. -
bind_array()
differs fromabind::abind
in that it can handle recursive arrays properly
(theabind::abind
function would unlist everything to atomic arrays, ruining the structure). -
bind_array()
is generally faster thanabind::abind
, asbind_array()
relies heavily on ‘C’ and ‘C++’ code. -
unlike
abind::abind
,bind_array()
only binds (atomic/recursive) arrays and matrices.
bind_array()
does not attempt to convert things to arrays when they are not arrays, but will give an error instead.
This saves computation time and prevents unexpected results. -
bind_array()
has more streamlined naming options, compared toabind::abind
.
acast()
‘broadcast’ provides the acast function, for casting (i.e. pivoting) an array into a new dimension.
Useful for, for example, computing grouped broadcasted operations.
General functions
‘broadcast’ also comes with 2 general broadcasted functions:
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:
bc_dim, ndim, lst.ndim, rep_dim.
Overloading
Sometimes broadcasting is needed in a large mathematical expression, involving multiple variables, where precedence is of importance.
For example in an expression like x + y / z^y
.
For such cases, you may want to overload the base operators.
To that end, the ‘broadcast’ package provides the broadcaster class attribute, which comes with its own method dispatch for the base operators.
References
Plate T, Heiberger R (2016). abind: Combine Multidimensional Arrays. R package version 1.4-5, https://CRAN.R-project.org/package=abind.
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).