nimMatrix: Creates matrix or array objects for use in nimbleFunctions

View source: R/nimbleFunction_Rexecution.R

nimMatrixR Documentation

Creates matrix or array objects for use in nimbleFunctions


In a nimbleFunction, matrix and array are identical to nimMatrix and nimArray, respectively


  value = 0,
  nrow = NA,
  ncol = NA,
  init = TRUE,
  fillZeros = TRUE,
  recycle = TRUE,
  type = "double"

  value = 0,
  dim = c(1, 1),
  init = TRUE,
  fillZeros = TRUE,
  recycle = TRUE,
  type = "double"



value(s) for initialization (default = 0). This can be a vector, matrix or array, but it will be used as a vector.


the number of rows in a matrix (default = 1)


the number of columns in a matrix (default = 1)


logical, whether to initialize values (default = TRUE)


logical, whether to initialize any elements not filled by (possibly recycled) value with 0 (or FALSE for nimLogical) (default = TRUE)


logical, whether value should be recycled to fill the entire contents of the new object (default = TRUE)


character representing the data type, i.e. 'double', 'integer', or 'logical' (default = 'double')


vector of dimension sizes in an array (default = c(1, 1))


number of dimensions in an array. This is only necessary for compileNimble if the length of dim cannot be determined during compilation.


These functions are similar to R's matrix and array functions, but they can be used in a nimbleFunction and compiled using compileNimble. Largely for compilation purposes, finer control is provided over initialization behavior, similarly to nimNumeric, nimInteger, and nimLogical. If init = FALSE, no initialization will be done, and value, fillZeros and recycle will be ignored. If init=TRUE and recycle=TRUE, then fillZeros will be ignored, and value will be repeated (according to R's recycling rule) as much as necessary to fill the object. If init=TRUE and recycle=FALSE, then if fillZeros=TRUE, values of 0 (or FALSE for nimLogical) will be filled in after value. Compiled code will be more efficient if unnecessary initialization is not done, but this may or may not be noticeable depending on the situation.

When used in a nimbleFunction (in run or other member function), matrix and array are immediately converted to nimMatrix and nimArray, respectively.

The nDim argument is only necessary for a use like dim <- c(2, 3, 4); A <- nimArray(0, dim = dim, nDim = 3). It is necessary because the NIMBLE compiler must determine during compilation that A will be a 3-dimensional numeric array. However, the compiler doesn't know for sure what the length of dim will be at run time, only that it is a vector. On the other hand, A <- nimArray(0, dim = c(2, 3, 4)) is allowed because the compiler can directly determine that a vector of length three is constructed inline for the dim argument.


Daniel Turek and Perry de Valpine

See Also

nimNumeric nimInteger nimLogical

nimble documentation built on March 18, 2022, 8:03 p.m.