nimble icon indicating copy to clipboard operation
nimble copied to clipboard

use of `%*%` with two vectors has a strange behavior with unhelpful warning message

Open paciorek opened this issue 1 year ago • 2 comments

The manual says that doing matrix multiplication with two vectors should work but creates a 1x1 matrix. One would think one can therefore do this:

code <- nimbleCode({
y[1,1] <- A[1:3]%*%x[1:3] 
})
m = nimbleModel(code)
cm = compileNimble(m)

But it doesn't work and the warning is hard to parse:

> cm = compileNimble(m)
Compiling
  [Note] This may take a minute.
  [Note] Use 'showCompilerOutput = TRUE' to see C++ compilation details.
Warning, in eigenizing model_y[1, 1] the [ is still there but nDim is not 0 (not a scalar).
Error: Failed to create the shared library. Run 'printErrors()' to see the compilation errors.

One would think that if nDim is not 0, then having [ is what one would want.

paciorek avatar Aug 09 '23 21:08 paciorek

Actually, I think we basically cover this in "Understanding dimensions and sizes from linear algebra" in the manual. One needs to do:

y <- (A[1:3]%*%x[1:3])[1,1]

One reason it may be confusing is that this works fine, and I'm not sure we ever explain why/how it works in the manual.

y[1:3] <- A[1:3,1:3]%*%x[1:3]

without additional subscripting.

This also works, and is more consistent with our discussion in the manual (and is given in the linear predictor example on the website).

y[1:3] <- (A[1:3,1:3]%*%x[1:3])[1:3,1]

paciorek avatar Dec 08 '23 17:12 paciorek

I think the only action item on this issue might be to modify this statement in the manual (in the RCfunction chapter), to address the case of y[1:3] <- A[1:3,1:3]%*%x[1:3]. I haven't dug into the details enough in terms of understanding what is going on to know quite what to say. @perrydv do you have a suggestion?

  - `M1 %*% v1` defaults to promoting `v1` to a 1-column matrix,
unless it is known at compile time that `M1` has 1 column, in which case
`v1` is promoted to a 1-row matrix.

paciorek avatar Dec 08 '23 17:12 paciorek