Added functions to calculate gradient and hessian