Implement gradient checking math.