update compute residual, add tests