For a model I am developing I found an matrix operation alternative of a for loop strategy
matrix[I1_dim, I1_dim] lambda_mat_1 = to_matrix(lambda[idx_1], I1_dim, I1_dim); // Bug prone matrix[Q, ct_in_levels] prop_mat_1 = vector_array_to_matrix(prop_1); //prop1 is a simplex matrix[I1_dim , Q] lambda_sum_1 = lambda_mat_1 * prop_mat_1'; matrix[I1_dim, I1_dim] sigma_mat_1 = to_matrix(sigma[idx_1], I1_dim, I1_dim); matrix[I1_dim, Q] sigma_sum_1 = square(lambda_sum_1) ./ (( square(lambda_mat_1) ./ sigma_mat_1 ) * square(prop_mat_1')) ;
Although can reduce execution time of 50%, I would like to improve further (considering that I can parallelise the for loop with map_rect).
In this list of commands I have to do multiple conversion between vectors, matrices etc…
Is there anything I should avoid? That is computationally heavy by definition? (e.g., the creation of so many intermediate variables)