Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions nimbleModel/R/indexRuleBlock.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ indexRuleBlockClass <- R6Class(
getIDs = function(indexRange) {
init <- as.integer(setupResults$fromMin + setupResults$offset)
switch(class(indexRange)[1],
indexRangeScalarClass = as.integer(indexRange$value) - init + 1L,
indexRangeSequenceClass = (indexRange$start - init + 1):(indexRange$end - init + 1),
indexRangeMatrixClass = as.integer(c(indexRange$values)) - init + 1L,
stop("invalid type of indexRange provided for creating IDs")
)
indexRangeScalarClass = as.integer(indexRange$value) - init + 1L,
indexRangeSequenceClass = (indexRange$start - init + 1):(indexRange$end - init + 1),
indexRangeMatrixClass = as.integer(c(indexRange$values)) - init + 1L,
stop("invalid type of indexRange provided for creating IDs")
)
},
invertIDs = function(relativeNodeIDs) {
if (setupResults$fromMin + setupResults$offset != 1) {
Expand Down
23 changes: 19 additions & 4 deletions nimbleModel/R/modelValues.R
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ make_modelValues_nClass <- function(varInfo,
if (is.null(defaultSizes)) defaultSizes <- list()
for (v in names(varInfo$vars)) {
if (is.null(defaultSizes[[v]]) || length(defaultSizes[[v]]) == 0) {
defaultSizes[[v]] <- rep(0, max(varInfo$vars[[v]]$nDim, 1))
nDim <- varInfo$vars[[v]]$nDim
# Scalars get size 1 so uncompiled rows are numeric(1). C++ ignores sizes for nDim 0.
defaultSizes[[v]] <- if (nDim == 0) 1 else rep(0, nDim)
}
}

Expand Down Expand Up @@ -468,10 +470,11 @@ as.list.modelValues <- function(x, varNames, stack = TRUE, flatten = FALSE, iter
if (length(varNames)) {
ans <- vector("list", length(varNames)) |> setNames(varNames)
for (var in varNames) {
res <- as.list(nCompiler::value(x, var)) # converts nList to list; compiled or uncompiled.
nl <- nCompiler::value(x, var)
res <- as.list(nl) # converts nList to list; compiled or uncompiled.
if (flatten) {
rawDims <- dimOrLength(res[[1]])
this_colnames <- dims_2_colnames(rawDims, var = var)
this_colnames <- nList_2_colnames(nl, rawDims, var = var)
}
if (stack) {
res <- simplify2array(res, higher = !flatten)
Expand Down Expand Up @@ -567,6 +570,18 @@ dims_2_colnames <- function(dims, var = "x") {
colnames <- paste0(var, "[", dim_strings, "]")
}

# Column names for the variable held in nList nl.
# A true scalar (nDim 0) is named by the variable name alone, as in nimble,
# whereas a length-1 vector is named with "[1]".
# R values do not distinguish these cases, so we use the nList's element type.
nList_2_colnames <- function(nl, dims, var) {
if (isTRUE(nl$get_nDim() == 0L)) {
var
} else {
dims_2_colnames(dims, var = var)
}
}

#' @exportS3Method
#' @method as.matrix modelValues
as.matrix.modelValues <- function(x) {
Expand All @@ -579,7 +594,7 @@ as.matrix.modelValues <- function(x) {
if (length(this_list) == 0) next
dim_x <- dimOrLength(this_list[[1]])
tot_len <- prod(dim_x)
this_colnames <- dims_2_colnames(dim_x, var = vn)
this_colnames <- nList_2_colnames(this_list, dim_x, var = vn)
# Possibly add check that all elements have the same dim
this_matrix <- this_list |>
lapply(\(x) {
Expand Down
14 changes: 7 additions & 7 deletions nimbleModel/inst/generate_predefined.R
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
## # To update the set of predefined nClasses:
##
## # generate new predef/instr_nC. Move that directly to package code inst/nimbleModel/predef/instr_nC (i.e. move to package)
nCompiler::nCompile(instr_nClass = nimbleModel:::instr_nClass, control=list(generate_predefined=TRUE))
nCompiler::nCompile(instr_nClass = nimbleModel:::instr_nClass, control = list(generate_predefined = TRUE))
test <- nCompiler::nCompile(instr_nClass = nimbleModel:::instr_nClass)
## #
## # generate new predef/declFunBase_nC. Move to package.
## # You should see "#include <nimbleModel/predef/declFunClass_/declFunClass_.h>" in the hContent
## # after declaration of declFunBase_nClass
## # and "// [[Rcpp::depends(nimbleModel)]]" in the cppContent
nCompiler::nCompile(nimbleModel:::declFunBase_nClass, control=list(generate_predefined=TRUE))
nCompiler::nCompile(nimbleModel:::declFunBase_nClass, control = list(generate_predefined = TRUE))
test <- nCompiler::nCompile(nimbleModel:::declFunBase_nClass)
## #
## # generate new predef/modelBase_nC. Move to package.
## # You should see "#include <nimbleModel/predef/modelClass_/modelClass_.h>" in the hContent
## # after the declaration of modelBase_nClass.
## # and "// [[Rcpp::depends(nimbleModel)]]" in the cppContent
nCompiler::nCompile(modelBase_nClass = nimbleModel:::modelBase_nClass, control=list(generate_predefined=TRUE))
nCompiler::nCompile(modelBase_nClass = nimbleModel:::modelBase_nClass, control = list(generate_predefined = TRUE))
test <- nCompiler::nCompile(nimbleModel:::modelBase_nClass)

## # generate new predef/copier_nClass. Move to package.
## # You should see "#include <nimbleModel/predef/copier_nC_base/copier_nC_base.h>" in the hContent BEFORE the class declaration
## # and "// [[Rcpp::depends(nimbleModel)]]" in the cppContent
nCompiler::nCompile(copier_nClass = nimbleModel:::copier_nClass, control=list(generate_predefined=TRUE))
nCompiler::nCompile(copier_nClass = nimbleModel:::copier_nClass, control = list(generate_predefined = TRUE))
test <- nCompiler::nCompile(nimbleModel:::copier_nClass)

## # generate new predef/mCopy_nC Move to package.
## # You should see "#include <nimbleModel/predef/mCopy_nC_base/mCopy_model_nC_base.h>" in the hContent BEFORE the class declaration
## # and "// [[Rcpp::depends(nimbleModel)]]" in the cppContent
nCompiler::nCompile(multiCopier_nClass = nimbleModel:::multiCopier_nClass, control=list(generate_predefined=TRUE))
nCompiler::nCompile(multiCopier_nClass = nimbleModel:::multiCopier_nClass, control = list(generate_predefined = TRUE))
test <- nCompiler::nCompile(nimbleModel:::multiCopier_nClass)

## # generate new predef/mCopyMV_nC. Move to package.
## # You should see "#include <nimbleModel/predef/mCopy_nC_base/mCopy_modelValues_nC_base.h>" in the hContent BEFORE the class declaration
## # and "// [[Rcpp::depends(nimbleModel)]]" in the cppContent
nCompiler::nCompile(multiCopierMV_nClass = nimbleModel:::multiCopierMV_nClass, control=list(generate_predefined=TRUE))
nCompiler::nCompile(multiCopierMV_nClass = nimbleModel:::multiCopierMV_nClass, control = list(generate_predefined = TRUE))
test <- nCompiler::nCompile(nimbleModel:::multiCopierMV_nClass)

## # generate new predef/MVBase_nC Move to package.
## # You should see "#include <nimbleModel/predef/modelValuesClass_/modelValuesClass_.h>" in the hContent
## # and "// [[Rcpp::depends(nimbleModel)]]" in the cppContent
nCompiler::nCompile(modelValuesBase_nClass = nimbleModel:::modelValuesBase_nClass, control=list(generate_predefined=TRUE))
nCompiler::nCompile(modelValuesBase_nClass = nimbleModel:::modelValuesBase_nClass, control = list(generate_predefined = TRUE))
test <- nCompiler::nCompile(nimbleModel:::modelValuesBase_nClass)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
list(saved_at = structure(1792534431.59537, class = c("POSIXct",
list(saved_at = structure(1793056666.55996, class = c("POSIXct",
"POSIXt")), packet_name = "MVBase_nC", elements = c("preamble",
"cppContent", "hContent", "filebase", "post_cpp_compiler", "copyFiles",
"depends"), files = list(preamble = "MVBase_nC_preamble.cpp",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
list(saved_at = structure(1792534392.88472, class = c("POSIXct",
list(saved_at = structure(1793056472.33843, class = c("POSIXct",
"POSIXt")), packet_name = "copier_nClass", elements = c("preamble",
"cppContent", "hContent", "filebase", "post_cpp_compiler", "copyFiles",
"depends"), files = list(preamble = "copier_nClass_preamble.cpp",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
list(saved_at = structure(1792534340.26498, class = c("POSIXct",
list(saved_at = structure(1793056375.6661, class = c("POSIXct",
"POSIXt")), packet_name = "declFunBase_nClass", elements = c("preamble",
"cppContent", "hContent", "filebase", "post_cpp_compiler", "copyFiles",
"depends"), files = list(preamble = "declFunBase_nClass_preamble.cpp",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
list(saved_at = structure(1792534327.40934, class = c("POSIXct",
list(saved_at = structure(1793056317.67685, class = c("POSIXct",
"POSIXt")), packet_name = "instr_nClass", elements = c("preamble",
"cppContent", "hContent", "filebase", "post_cpp_compiler", "copyFiles",
"depends"), files = list(preamble = "instr_nClass_preamble.cpp",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
list(saved_at = structure(1792534418.86996, class = c("POSIXct",
list(saved_at = structure(1793056640.44678, class = c("POSIXct",
"POSIXt")), packet_name = "multiCopierMV_nClass", elements = c("preamble",
"cppContent", "hContent", "filebase", "post_cpp_compiler", "copyFiles",
"depends"), files = list(preamble = "multiCopierMV_nClass_preamble.cpp",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
list(saved_at = structure(1792534403.251, class = c("POSIXct",
list(saved_at = structure(1793056548.82963, class = c("POSIXct",
"POSIXt")), packet_name = "mCopy_nC", elements = c("preamble",
"cppContent", "hContent", "filebase", "post_cpp_compiler", "copyFiles",
"depends"), files = list(preamble = "mCopy_nC_preamble.cpp",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
list(saved_at = structure(1792534353.3121, class = c("POSIXct",
list(saved_at = structure(1793056441.24624, class = c("POSIXct",
"POSIXt")), packet_name = "modelBase_nClass", elements = c("preamble",
"cppContent", "hContent", "filebase", "post_cpp_compiler", "copyFiles",
"depends"), files = list(preamble = "modelBase_nClass_preamble.cpp",
Expand Down
Loading
Loading