Nk

Here's the proposed generalization of the expectations.  Not quite as clean as I would have hoped, and took a while to get it to this state, but does eliminate a lot of the non-vectorized loops.

It appears to be slightly slower for larger N (500K to 2M states), and I have not yet tested the performance on large numbers of states (sampled or unsampled) between the codes.  It may require a bit of tweaking.

I didn't try to run the PMF code through it as well; that would have meant a number of other conditionals that seemed to complicated the code too much.

It may make sense to split out the computePerturbedFreeEnergies functions; that will simplify the inner loop a bit more.