#!/usr/bin/python import scipy from scipy.optimize import * import numpy from numpy import * def rosen(x): """The Rosenbrock function""" r = sum(100.0*(x[1:]-x[:-1]**2.0)**2.0 + (1-x[:-1])**2.0) print "rosenbrock: ", x, r return r def rosen_der(x): """The derivative (i.e. gradient) of the Rosenbrock function. Parameters ---------- x : array_like, 1D The point at which the derivative is to be computed. Returns ------- der : 1D numpy array The gradient of the Rosenbrock function at `x`. See Also -------- rosen, rosen_hess, rosen_hess_prod """ x = asarray(x) xm = x[1:-1] xm_m1 = x[:-2] xm_p1 = x[2:] der = numpy.zeros_like(x) der[1:-1] = 200*(xm - xm_m1**2) - 400*(xm_p1 - xm**2)*xm - 2*(1 - xm) der[0] = -400*x[0]*(x[1] - x[0]**2) - 2*(1 - x[0]) der[-1] = 200*(x[-1] - x[-2]**2) print " rosen_der:", der return der # starting point x0 = array([1.3, 0.7, 0.8, 1.9, 1.2]) fmin_l_bfgs(rosen, x0, fprime=rosen_der, args=(), gtol=1e-05, norm=inf, epsilon=1.4901161193847656e-08, maxiter=None, full_output=0, disp=1, retall=0, callback=None)