Optimizing a function in Eigen
It's hard to help without knowing the sizes, but:


The first thing to do would be to measure the relative cost of each of the 5 statements to see where the bottleneck is.
-(a5/2 + d1/2 + log(a1)) could be precomputed into a temporary outside the loop.
Replace pow(A1.row(j).transpose(), 2) by A1.row(j).transpose().square().
Compilation flags might also help: make sure you enabled AVX and if supported FMA using e.g., -march=native. 
Depending on the sizes, you might want to rewrite the expressions to benefits from faster matrix-matrix products:


'

T = (M2-M4).transpose() * M1;
for(j...)
  ...
  A1.row(j) = a1*a2 * (T.col(j) - M1.col(j).squaredNorm() * M3.row(j).transpose());
  ...