Updated computation of the similarity matrix. The new implementation is more stable numerically.