Given a row_vector of size M and a matrix of size MxN, I’m doing matrix product by row_vector * matrix.

I’m thinking if it’ll be faster or slower if we define a MATRIX of size NxM and a vector of size M, and then do matrix product by MATRIX * vector.

In my problem, M is small (e.g., = 10) but N may be large (e.g., = 10000). Very much appreciate if you could advise which way of matrix product will be faster.

This is far from my expertise, but since there are no replies so far I’ll give it a try; if I get it wrong enough maybe I’ll get yelled at and you’ll get a better answer.

I believe the efficiency of that in Stan will follow that of the libraries it uses in C++: Eigen (and also Boost, maybe). I’m not sure if that implementation of matrices as row- or column-major will make a difference for the speed of this operations in practice (especially since there are usually more costly operations at every iteration).

In practice, instead of wondering, you could just try both options and run some trials runs to see if you notice any difference. I’m guessing it won’t really make that much of a difference.

For a row_vector * matrix the row_vector is multplied by each column of the matrix. Since Eigen matrices are column major I’d assume that’s more performant. On the flip side matrix * vector multiplies each row of the matrix by the vector so it’s non-contiguous access. But eigen might have tricks for this so you may not even up with a difference either way. It’s always better just to benchmark these things