Skip to content

Error trap for reduction operations on vector arguments #1525

@rperry-usgs

Description

@rperry-usgs

Hi there! I ran into this problem, fixed it, and subsequently read that nimble functions don't handle negative indices, as in R. Indeed, in a function more complex than the reproduceable example below (a custom sampler), I received an error when indexing by x[-d] where d = length(x). Here's the specific error returned on compilation:

Error:
Error: NIMBLE compiler does not support reduction operations on scalar arguments.
This occurred for: sum(x_next[-(d)])
This was part of the call: totTime - sum(x_next[-(d)])

However, the function compiled successfully and happily returned a wrong result when using x[-c(i,d)] where i is a for loop index. I only caught this error because results did not match my sampler implemented in R, but I could see how it might go unnoticed or be difficult to debug in some cases. So I'm flagging this as a potential candidate for a stop-execution error trap that doesn't return a result, similar to the error message above.

Below is a simple case reproducing the error. I'm using nimble v1.3.0.

testNegIdx <- nimbleFunction(
run = function(x = double(1)) {
ans <- x[-c(1,4)]
return(ans)
returnType(double(1))
} )

Should return c(2, 3), and does.
x <- 1:4

testNegIdx(x)
[1] 2 3

Test compiled version
`CtestNegIdx <- compileNimble(testNegIdx)

CtestNegIdx(x)
[1] 7.410985e-323 8.332910e-312
CtestNegIdx(x)
[1] 0.000000e+00 8.332191e-312
CtestNegIdx(x)
[1] 1.022650e-312 1.447621e+166

Not c(2,3) and returns different values each time it's called.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions