broadcast Package Overview
Description
broadcast:
‘Numpy’-Like Broadcasted Operations for Atomic and Recursive Arrays with Minimal Dependencies.
Implements simple 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:
-
A set of type-specific functions for broadcasted element-wise operations on any 2 arrays; they support a large set of relational-, arithmetic-, Boolean-, and string operations.
-
A faster, more memory efficient, and broadcasted version of
abind()
, 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.
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).
Getting Started
An introduction and overview of the package can be found HERE.
Note that ‘broadcast’ is still somewhat experimental; if you find bugs or other issues, please report them promptly on the ‘broadcast’ GitHub issues tab.
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 type-specific 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()
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()
allows for broadcasting, whileabind::abind
does not support broadcasting. -
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
.
General functions
‘broadcast’ also comes with 2 general broadcasted functions:
Other functions
‘broadcast’ provides the acast function, for casting (i.e. pivoting) an array into a new dimension.
‘broadcast’ also provides type-casting functions, which preserve names and dimensions - convenient for arrays.
Overloading
Sometimes broadcasting is needed in 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, 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.