gradient descent code simplification