to_array: Turn (lists of) numerics into arrays (for Stan input)

to_arrayR Documentation

Turn (lists of) numerics into arrays (for Stan input)

Description

Takes 'NULL', numeric atomics (single scalars, vectors, matrices) or lists of numeric elements as inputs and turns them into numeric arrays. For lists, the outer dimension of the array iterate over the list elements (for details, see argument 'outer_dims').

Usage

to_array(
  x,
  inner_dims = NULL,
  outer_dims = NULL,
  dimnames = NULL,
  simplify = T
)

Arguments

x

The input to be turned into an array.

inner_dims

Integer vector of intended dimensions of x. If 'NULL', 'inner_dims' will be inferred from the input. If not 'NULL', 'inner_dims' will be used for checks and to enforce the correct dimensions of 'x'. (default: 'NULL')

outer_dims

An integer vector of outer dimensions for the array. If 'x' is not a list, an array of 'x' will be repeated for each outer dimension. If 'x' is list, combinations of the outer dimensions will each index one element of 'x'. The total length of 'x' must match the product of outer dimensions. The first dimension will be iterated over first (and thus alternating fastest), the second dimension will be iterated over second, etc. See details.

dimnames

A list of character vectors with names for each dimension of the array. If not 'NULL', the length of this list must match the total number of dimensions, and each character vector must match the length of the corresponding dimension. Dimensions are named from the outside-in (i.e. first outer than inner dimensions). (default: 'NULL')

simplify

Should the array be simplified by removing inner dimensions whenever the inner element(s) are just scalars? (e.g., if the inner elements are a one-element vector or a 1x1 matrix). Note that atomic values that can be simplified will be returned as scalars (and thus without dimnames) if there are no outer dimensions, since the returned object cannot be an array in that case. (default: 'TRUE')

Details

  • 'NULL' will be turned in an 'array(numeric(0), dim = c(outer_dims, inner_dims))'. This is sometimes required for optional Stan inputs.

  • Atomics (scalars, vectors, and matrices) will be turned into an 'array(x, dim = c(outer_dims, inner_dims))'. This essentially creates copies of 'x' for each combination of 'outer_dims'.

  • Lists will be turned into an 'array(x, dim = c(outer_dims, inner_dims))'. 'prod(outer_dims)' must match the length of the list.

E.g., if the input is a list of six 2x2 covariance matrices, and '...' is '3, 2', then output will be an array of dimensionality 'c(3, 2, 2, 2)'.

Value

An array.


hlplab/MVBeliefUpdatr documentation built on July 5, 2025, 6:42 a.m.