Adding optimized left and right multiplications to the diagonal matrix.