Code Dim was given as known but evaluates to a non-scalar.

628 views
Skip to first unread message

David Pleydell

unread,
Feb 14, 2017, 1:38:08 PM2/14/17
to nimble-users
Hi

I am trying to simplify a Nimble model with a "greater than one" constraint applied to a set of parameters stored in a 3D array. 

The "bulky" version of the code evaluates the constraint inside nested loops and runs fine.

The "lite" version attempts to move this evaluation outside the loops in the hope that this results in fewer nodes, a lower memory demand and slightly faster code execution.  

For this I have written a new version of the function min called min3 that finds the minimum of a 3D array.  min3 compiles and runs fine by itself.  But when trying to build a BUGS model, the function nimbleModel complains about the line that uses min3 and throws the following error. 

defining model...
Error in replaceConstantsRecurse(x, constEnv, constNames) : 
  Error, hit end
In addition: Warning message:
In replaceConstantsRecurse(x, constEnv, constNames) :
  Code Dim was given as known but evaluates to a non-scalar.  This is probably not what you want.

What have I missed that's causing the array dimensions "Dim" to throw the error message? 

I have attached a simplified script that reproduces this error.  I'm using a development version of Nimble from February 1.

David

debug_nimblecode.R

nimble.stats

unread,
Feb 14, 2017, 7:11:32 PM2/14/17
to David Pleydell, nimble-users
Hi David,

It works with this: minParas <- min3(paras[1:T,1:S,1:2], Dim[1:3])

From your comment it looks like you tried including the paras indexing, but you also need Dim indexing.

Also min (like most of our vectorized arithmetic etc. at the moment) works on a vector or matrix but not greater than 2D.  So you’ll get a more efficient min3 like this:

min3 <- nimbleFunction(
    run=function(x=double(3),   ## 3D nimArray
                 dim=integer(1)) ## Vector of length 3 giving the dimensions of x
    {
        minx <- Inf
        for(ix in 1:dim[1]) {
            minx <- min(minx, min(x[ix,,]))
        }
        returnType(double(0))
        return(minx)
    }

Perry

--
You received this message because you are subscribed to the Google Groups "nimble-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To post to this group, send email to nimble...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/5d3cbf52-ee76-40ce-9fdc-f6adaad5c198%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
<debug_nimblecode.R>

nimble.stats

unread,
Feb 14, 2017, 7:13:05 PM2/14/17
to nimble.stats, David Pleydell, nimble-users
P.S. You should be able to get the dimensions from dim(x) but at the moment (due to a quirk) only if you index immediately, e.g. dim(x)[1]

David Pleydell

unread,
Feb 15, 2017, 11:49:06 AM2/15/17
to nimble-users, nimble...@gmail.com, d_ple...@yahoo.com
Great!!!  Thanks Perry!!


Reply all
Reply to author
Forward
0 new messages