Fix bugs

Bugs: derivative of d(U)AU^T is not necessarily the same as UAd(U)^T.
Improve RAM usage : O(dpm^2) (i.e., independent of n)