#NEWLINE# def derivative( func , x0 , dx = 1.0 , n = 1 , args = ( ) , order = 3 ) : #NEWLINE# #INDENT# '\n    Find the n-th derivative of a function at a point.\n\n    Given a function, use a central difference formula with spacing `dx` to\n    compute the `n`-th derivative at `x0`.\n\n    Parameters\n    ----------\n    func : function\n        Input function.\n    x0 : float\n        The point at which `n`-th derivative is found.\n    dx : float, optional\n        Spacing.\n    n : int, optional\n        Order of the derivative. Default is 1.\n    args : tuple, optional\n        Arguments\n    order : int, optional\n        Number of points to use, must be odd.\n\n    Notes\n    -----\n    Decreasing the step size too small can result in round-off error.\n\n    Examples\n    --------\n    >>> from scipy.misc import derivative\n    >>> def f(x):\n    ...     return x**3 + x**2\n    >>> derivative(f, 1.0, dx=1e-6)\n    4.9999999999217337\n\n    ' #NEWLINE# if ( order < ( n + 1 ) ) : #NEWLINE# #INDENT# raise ValueError ( "'order' (the number of points used to compute the derivative), must be at least the derivative order 'n' + 1." ) #NEWLINE# #UNINDENT# if ( ( order % 2 ) == 0 ) : #NEWLINE# #INDENT# raise ValueError ( "'order' (the number of points used to compute the derivative) must be odd." ) #NEWLINE# #UNINDENT# if ( n == 1 ) : #NEWLINE# #INDENT# if ( order == 3 ) : #NEWLINE# #INDENT# weights = ( array ( [ ( - 1 ) , 0 , 1 ] ) / 2.0 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( order == 5 ) : #NEWLINE# #INDENT# weights = ( array ( [ 1 , ( - 8 ) , 0 , 8 , ( - 1 ) ] ) / 12.0 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( order == 7 ) : #NEWLINE# #INDENT# weights = ( array ( [ ( - 1 ) , 9 , ( - 45 ) , 0 , 45 , ( - 9 ) , 1 ] ) / 60.0 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( order == 9 ) : #NEWLINE# #INDENT# weights = ( array ( [ 3 , ( - 32 ) , 168 , ( - 672 ) , 0 , 672 , ( - 168 ) , 32 , ( - 3 ) ] ) / 840.0 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# weights = central_diff_weights ( order , 1 ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( n == 2 ) : #NEWLINE# #INDENT# if ( order == 3 ) : #NEWLINE# #INDENT# weights = array ( [ 1 , ( - 2.0 ) , 1 ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( order == 5 ) : #NEWLINE# #INDENT# weights = ( array ( [ ( - 1 ) , 16 , ( - 30 ) , 16 , ( - 1 ) ] ) / 12.0 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( order == 7 ) : #NEWLINE# #INDENT# weights = ( array ( [ 2 , ( - 27 ) , 270 , ( - 490 ) , 270 , ( - 27 ) , 2 ] ) / 180.0 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( order == 9 ) : #NEWLINE# #INDENT# weights = ( array ( [ ( - 9 ) , 128 , ( - 1008 ) , 8064 , ( - 14350 ) , 8064 , ( - 1008 ) , 128 , ( - 9 ) ] ) / 5040.0 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# weights = central_diff_weights ( order , 2 ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# weights = central_diff_weights ( order , n ) #NEWLINE# #UNINDENT# #UNINDENT# val = 0.0 #NEWLINE# ho = ( order >> 1 ) #NEWLINE# for k in range ( order ) : #NEWLINE# #INDENT# val += ( weights [ k ] * func ( ( x0 + ( ( k - ho ) * dx ) ) , * args ) ) #NEWLINE# #UNINDENT# return ( val / product ( ( ( dx , ) * val ) , axis = 0 ) )