#!/usr/bin/python import numpy as np 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 = np.asarray(x) xm = x[1:-1] xm_m1 = x[:-2] xm_p1 = x[2:] der = np.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 _epsilon = 1e-10 def check_grad(func, grad, x0, subset): return np.sqrt(sum((grad(x0)[subset, 0]-approx_fprime(x0,func,_epsilon, subset)[subset, 0])**2)) def approx_fprime(xk,f,epsilon, subset): f0 = f(xk) grad = np.zeros((len(xk),1), float) ei = np.zeros((len(xk),1), float) for k in subset: ei[k] = epsilon grad[k] = (f(xk+ei) - f0)/epsilon ei[k] = 0.0 return grad # starting point x0 = np.array([1.3, 0.7, 0.8, 1.9, 1.2]).reshape([5, 1]) print check_grad(rosen, rosen_der, x0, [1, 3, 2])