# Using rows_dot_product in a loop to set column contents; any optimization ideas?

Removing extraneous structures, I have:

``````data{
int m ;
int n ;
matrix[m,n] X ;
int p ;
...
}
parameters{
matrix[n,p] Z ;
...
}
model{
matrix[m,p] Q ;
for(i_p in 1:p){
Q[,i_p]= rows_dot_product(
rep_matrix(to_row_vector(Z[,i_p]),m)
, X
) ;
}
...
}
``````

I’m sure that loop can be made into a single call to rows_dot_product but I’ve been I’ve been looking at this model for too long and am having trouble “seeing” it. Any suggestions?

Solved it! Just need to index to expand the matrices for a single rows_dot_product call, then wrap the resulting vector to a matrix:

``````data{
int m ;
int n ;
matrix[m,n] X ;
int p ;
...
}
transformed data{
array[m*p] int mp ; //will be rep(1:m,times=p)
array[p*m] int pm ; // will be rep(1:p,each=m)
int i_mp = 0 ;
for(i_p in 1:p){
for(i_m in 1:m){
i_mp += 1 ;
mp[i_mp] = i_m ;
pm[i_mp] = i_p ;
}
}
}
parameters{
matrix[p,n] Z ; //n.b. transposed wrt original, obviating to_row_vector
...
}
model{
matrix[m,p] Q = to_matrix( rows_dot_product(Z[pm],X[mp]) , m, p)
...
}
``````

Now to check if I got the indexing right…

Yup, had an indexing error, but worked it out and edited the above to have the correct solution.

Wonder if it would be slightly more efficient to pre-define `X[mp]` as a TD variable:

``````data{
int m ;
int n ;
matrix[m,n] X ;
int p ;
...
}
transformed data{
array[m*p] int mp ; //will be rep(1:m,times=p)
array[p*m] int pm ; // will be rep(1:p,each=m)
int i_mp = 0 ;
for(i_p in 1:p){
for(i_m in 1:m){
i_mp += 1 ;
mp[i_mp] = i_m ;
pm[i_mp] = i_p ;
}
}
matrix[m*p,n] X_mp = X[mp] ;
}
parameters{
matrix[p,n] Z ; //n.b. transposed wrt original, obviating to_row_vector
...
}
model{
matrix[m,p] Q = to_matrix( rows_dot_product(Z[pm],X_mp) , m, p)
...
}
``````