Avoid recomputing residual

Compute dot product recursively