Tell me more ×
Cross Validated is a question and answer site for statisticians, data analysts, data miners and data visualization experts. It's 100% free, no registration required.

In R, c(3,1,0) * c(2,0,1) == c(6,0,0). This is not dot product and it's not cross product. First, what is the name for this product, and second, does it work in WinBUGS, OpenBUGS and/or JAGS?

share|improve this question
1  
this is just element-wise multiplication. i'm not sure if it works in WinBUGS, OpenBUGS, or JAGS. – Max Jan 6 '12 at 7:05

4 Answers

up vote 1 down vote accepted

Unlike JAGS, WinBUGS and OpenBUGS does not do this form of vectorization; you have to write a loop, and compute each element 'by hand', as described above.

share|improve this answer

To do element-wise multiplication you can just make a for loop in those languages and that's it! I've used for loops in WinBUGS with no problems.

share|improve this answer
What question does this reply address? It does not seem to be relevant here. – whuber Jan 6 '12 at 23:46
@whubber, why? It is perfectly relevant. Ok, I changed the post a little to be more clear. – Tomas Jan 6 '12 at 23:49
Yup, a for loop is what I've been doing so far; I'd just wondered if a vectorized version was possible. – Jack Tanner Jan 6 '12 at 23:59
I've submitted a feature request to JAGS: sourceforge.net/tracker/… – Jack Tanner Jan 7 '12 at 0:04
Thanks, Tomas. Now I see the connection: you're not answering the question as stated, but you are offering a workaround. – whuber Jan 7 '12 at 0:08

Martyn Plummer points out that this is implemented in JAGS, which I missed when reading the manual. From Ch 5:

Scalar functions taking scalar arguments are automatically vectorized. They can also be called when the arguments are arrays with conforming dimensions, or scalars. So, for example, the scalar $c$ can be added to the matrix $A$ using

B <- A + c

instead of the more verbose form

D <- dim(A)
for (i in 1:D[1])
  for (j in 1:D[2]) {
    B[i,j] <- A[i,j] + c
  }
}
share|improve this answer

Incidentally, element-wise multiplication of two equal length vectors is called the Hadamard product (aka the Schur product).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.