#NEWLINE# def approx_hess1( x , f , epsilon = None , args = ( ) , kwargs = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } , return_grad = False ) : #NEWLINE# #INDENT# n = len ( x ) #NEWLINE# h = _get_epsilon ( x , 3 , epsilon , n ) #NEWLINE# ee = np . diag ( h ) #NEWLINE# f0 = f ( * ( ( x , ) + args ) , ** kwargs ) #NEWLINE# g = np . zeros ( n ) #NEWLINE# for i in range ( n ) : #NEWLINE# #INDENT# g [ i ] = f ( * ( ( ( x + ee [ i , : ] ) , ) + args ) , ** kwargs ) #NEWLINE# #UNINDENT# hess = np . outer ( h , h ) #NEWLINE# for i in range ( n ) : #NEWLINE# #INDENT# for j in range ( i , n ) : #NEWLINE# #INDENT# hess [ ( i , j ) ] = ( ( ( ( f ( * ( ( ( ( x + ee [ i , : ] ) + ee [ j , : ] ) , ) + args ) , ** kwargs ) - g [ i ] ) - g [ j ] ) + f0 ) / hess [ ( i , j ) ] ) #NEWLINE# hess [ ( j , i ) ] = hess [ ( i , j ) ] #NEWLINE# #UNINDENT# #UNINDENT# if return_grad : #NEWLINE# #INDENT# grad = ( ( g - f0 ) / h ) #NEWLINE# return ( hess , grad ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return hess