If statements within loops in lp{m,d}fs

For code such as this:


why is the if (include_summand<propto>::value) inside the loop. In other words, since that condition does not vary across i why isn’t it

if (include_summand<propto>::value)
  for (size_t i = 0; i < max_size(N, n); i++)
    normalizing_constant[i] = binomial_coefficient_log(N_vec[i], n_vec[i]); 

We seem to put these constant conditions inside loops a lot. Is the compiler much smarter than I think or am I much dumber than I think?

That general pattern of looping over the data size was used to translate the original densities.

The condition is a metaprogram, so that gets evaluated statically. If the body of the loop is empty, the compiler should be able to easily optimize it away.

The bigger problem is that there are distributions like the normal where we do a bunch of additions where a multiplication would suffice if we reordered some of these. For instance, if sigma is a parameter but it’s a scalar and everything else is a vector of size N, then we should be able to use lp -= n * log(sigma) rather than doing lp -= log(sigma) inside a loop N times.