Objective Function And Gradient Computation In Python