Learn R Programming

nimble (version 1.2.1)

setSize: set the size of a numeric variable in NIMBLE

Description

set the size of a numeric variable in NIMBLE. This works in R and NIMBLE, but in R it usually has no effect.

Usage

setSize(numObj, ..., copy = TRUE, fillZeros = TRUE)

Arguments

numObj

This is the object to be resized

...

sizes, provided as scalars, in order, or as a single vector

copy

logical indicating whether values should be preserved (in column-major order)

fillZeros

logical indicating whether newly allocated space should be initialized with zeros (in compiled code)

Author

NIMBLE development team

Details

Note that assigning the result of numeric, integer, logical, matrix, or array is often as good or better than using setSize. For example, `x <- matrix(nrow = 5, ncol = 5)` is equivalent to `setSize(x, 5, 5)` but the former allows more control over initialization.

This function is part of the NIMBLE language. Its purpose is to explicitly resize a multivariate object (vector, matrix or array), currently up to 4 dimensions. Explicit resizing is not needed when an entire object is assigned to. For example, in Y <- A %*% B, where A and B are matrices, Y will be resized automatically. Explicit resizing is necessary when assignment will be by indexed elements or blocks, if the object is not already an appropriate size for the assignment. E.g. prior to Y[5:10] <- A %*% B, one can use setSize to ensure that Y has a size (length) of at least 10.

This does work in uncompiled (R) and well as compiled execution, but in some cases it is only necessary for compiled execution. During uncompiled execution, it may not catch bugs due to resizing because some R objects will be dynamically resized during assignments anyway.

If preserving values in the resized object and/or initializing new values with 0 is not necessary, then setting these arguments to FALSE will yield slightly more efficient compiled code.