def approx_fprime(x, f, epsilon=None, args=(), kwargs={}, centered=False):
 n = len(x)
 f0 = f(*((x,) + args), **kwargs)
 dim = np.atleast_1d(f0).shape
 grad = np.zeros(((n,) + dim), np.promote_types(float, x.dtype))
 ei = np.zeros((n,), float)
 if (not centered):
  epsilon = _get_epsilon(x, 2, epsilon, n)
  for k in range(n):
   ei[k] = epsilon[k]
   grad[k, :] = ((f(*(((x + ei),) + args), **kwargs) - f0) / epsilon[k])
   ei[k] = 0.0
 else:
  epsilon = (_get_epsilon(x, 3, epsilon, n) / 2.0)
  for k in range(len(x)):
   ei[k] = epsilon[k]
   grad[k, :] = ((f(*(((x + ei),) + args), **kwargs) - f(*(((x - ei),) + args), **kwargs)) / (2 * epsilon[k]))
   ei[k] = 0.0
 return grad.squeeze().T

def approx_fprime ( x , f , epsilon = None , args = ( ) , kwargs = { } , centered = False ) : n = len ( x ) f0 = f ( * ( ( x , ) + args ) , ** kwargs ) dim = np . atleast_1d ( f0 ) . shape grad = np . zeros ( ( ( n , ) + dim ) , np . promote_types ( float , x . dtype ) ) ei = np . zeros ( ( n , ) , float ) if ( not centered ) : epsilon = _get_epsilon ( x , 2 , epsilon , n ) for k in range ( n ) : ei [ k ] = epsilon [ k ] grad [ k , : ] = ( ( f ( * ( ( ( x + ei ) , ) + args ) , ** kwargs ) - f0 ) / epsilon [ k ] ) ei [ k ] = 0.0 else : epsilon = ( _get_epsilon ( x , 3 , epsilon , n ) / 2.0 ) for k in range ( len ( x ) ) : ei [ k ] = epsilon [ k ] grad [ k , : ] = ( ( f ( * ( ( ( x + ei ) , ) + args ) , ** kwargs ) - f ( * ( ( ( x - ei ) , ) + args ) , ** kwargs ) ) / ( 2 * epsilon [ k ] ) ) ei [ k ] = 0.0 return grad . squeeze ( ) . T
central difference approximation of the gradient of a scalar function .