# Vector multiplication in BUGS and JAGS

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?

• this is just element-wise multiplication. i'm not sure if it works in WinBUGS, OpenBUGS, or JAGS. – assumednormal Jan 6 '12 at 7:05

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.

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
}
}


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.

• 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. – Curious 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

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