nimble-dev / nimble

The base NIMBLE package for R
http://R-nimble.org
BSD 3-Clause "New" or "Revised" License
156 stars 23 forks source link

C++ problem with casting in special case of non-scalar component-wise operators involving logical operation #1388

Open perrydv opened 8 months ago

perrydv commented 8 months ago

A user reported this problem:

foo <- nimbleFunction(
  run = function(x = double(1)) {
    returnType(double(1))
    return(x * ( x > 0 ))
  })
cfoo <- compileNimble(foo) # Fails

Note that the following two cases work:

foo <- nimbleFunction(
  run = function(x = double(1), y = integer(1)) {
    returnType(double(1))
    return(x * (y + 1)) # This has double * (integer operation instead of logical operation)
  })
cfoo <- compileNimble(foo)

foo <- nimbleFunction(
  run = function(x = double(1)) {
    returnType(double(1))
    temp <- x > 0 # create a temporary to hold the x>0 result
    return(x * temp)
  })
cfoo <- compileNimble(foo)

So the problem seems to be a particular Eigen incantation that is code-generated from the first case and looks superficially valid. The code generated for the last case looks exactly like code from the first case with the exception of assigning to the temporary variable.