compute gradient for faster l-bfgs