#NEWLINE# def value_and_grad( fun , argnum = 0 ) : #NEWLINE# #INDENT# 'Returns a function that returns both value and gradient. Suitable for use\n    in scipy.optimize' #NEWLINE# def double_val_fun( * args , **kwargs ) : #NEWLINE# #INDENT# val = fun ( * args , ** kwargs ) #NEWLINE# return ( val , getval ( val ) ) #NEWLINE# #UNINDENT# gradval_and_val = grad_and_aux ( fun , argnum ) #NEWLINE# flip = ( lambda x , y : ( y , x ) ) #NEWLINE# return ( lambda * args , **kwargs : flip ( * gradval_and_val ( * args , ** kwargs ) ) )