Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
paciorek opened this issue Aug 9, 2023 · 2 comments
Labels

Comments

@paciorek
Copy link
Contributor

paciorek commented Aug 9, 2023

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
Copy link
Contributor Author

paciorek commented Dec 8, 2023

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
Copy link
Contributor Author

paciorek commented Dec 8, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant