def approx_fprime_cs(x, f, epsilon=None, args=(), kwargs={}):
 n = len(x)
 epsilon = _get_epsilon(x, 1, epsilon, n)
 increments = ((np.identity(n) * 1j) * epsilon)
 partials = [(f((x + ih), *args, **kwargs).imag / epsilon[i]) for (i, ih) in enumerate(increments)]
 return np.array(partials).T

def approx_fprime_cs ( x , f , epsilon = None , args = ( ) , kwargs = { } ) : n = len ( x ) epsilon = _get_epsilon ( x , 1 , epsilon , n ) increments = ( ( np . identity ( n ) * 1j ) * epsilon ) partials = [ ( f ( ( x + ih ) , * args , ** kwargs ) . imag / epsilon [ i ] ) for ( i , ih ) in enumerate ( increments ) ] return np . array ( partials ) . T
calculate gradient or jacobian with complex step derivative approximation parameters x : array parameters at which the derivative is evaluated f : function f(*(+args) .