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:

  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- and apply-like functions.

  4. The acast() function, for casting/pivoting an array into a new dimension.

  5. 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).

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 and integer 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, while abind::abind does not support broadcasting.

  • bind_array() differs from abind::abind in that it can handle recursive arrays properly
    (the abind::abind function would unlist everything to atomic arrays, ruining the structure).

  • bind_array() is generally faster than abind::abind, as bind_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 to abind::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:

  • bc_ifelse: Broadcasted version of ifelse.

  • bcapply: Broadcasted apply-like function.

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.

Author(s)

Author, Maintainer: Tony Wilkes tony_a_wilkes@outlook.com (ORCID)

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).