#NEWLINE# def hessian( f , varlist , constraints = [ ] ) : #NEWLINE# #INDENT# "Compute Hessian matrix for a function f wrt parameters in varlist\n    which may be given as a sequence or a row/column vector. A list of\n    constraints may optionally be given.\n\n    Examples\n    ========\n\n    >>> from sympy import Function, hessian, pprint\n    >>> from sympy.abc import x, y\n    >>> f = Function('f')(x, y)\n    >>> g1 = Function('g')(x, y)\n    >>> g2 = x**2 + 3*y\n    >>> pprint(hessian(f, (x, y), [g1, g2]))\n    [                   d               d            ]\n    [     0        0    --(g(x, y))     --(g(x, y))  ]\n    [                   dx              dy           ]\n    [                                                ]\n    [     0        0        2*x              3       ]\n    [                                                ]\n    [                     2               2          ]\n    [d                   d               d           ]\n    [--(g(x, y))  2*x   ---(f(x, y))   -----(f(x, y))]\n    [dx                   2            dy dx         ]\n    [                   dx                           ]\n    [                                                ]\n    [                     2               2          ]\n    [d                   d               d           ]\n    [--(g(x, y))   3   -----(f(x, y))   ---(f(x, y)) ]\n    [dy                dy dx              2          ]\n    [                                   dy           ]\n\n    References\n    ==========\n\n    http://en.wikipedia.org/wiki/Hessian_matrix\n\n    See Also\n    ========\n\n    sympy.matrices.mutable.Matrix.jacobian\n    wronskian\n    " #NEWLINE# if isinstance ( varlist , MatrixBase ) : #NEWLINE# #INDENT# if ( 1 not in varlist . shape ) : #NEWLINE# #INDENT# raise ShapeError ( '`varlist` must be a column or row vector.' ) #NEWLINE# #UNINDENT# if ( varlist . cols == 1 ) : #NEWLINE# #INDENT# varlist = varlist . T #NEWLINE# #UNINDENT# varlist = varlist . tolist ( ) [ 0 ] #NEWLINE# #UNINDENT# if is_sequence ( varlist ) : #NEWLINE# #INDENT# n = len ( varlist ) #NEWLINE# if ( not n ) : #NEWLINE# #INDENT# raise ShapeError ( '`len(varlist)` must not be zero.' ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# raise ValueError ( 'Improper variable list in hessian function' ) #NEWLINE# #UNINDENT# if ( not getattr ( f , 'diff' ) ) : #NEWLINE# #INDENT# raise ValueError ( ( 'Function `f` (%s) is not differentiable' % f ) ) #NEWLINE# #UNINDENT# m = len ( constraints ) #NEWLINE# N = ( m + n ) #NEWLINE# out = zeros ( N ) #NEWLINE# for ( k , g ) in enumerate ( constraints ) : #NEWLINE# #INDENT# if ( not getattr ( g , 'diff' ) ) : #NEWLINE# #INDENT# raise ValueError ( ( 'Function `f` (%s) is not differentiable' % f ) ) #NEWLINE# #UNINDENT# for i in range ( n ) : #NEWLINE# #INDENT# out [ ( k , ( i + m ) ) ] = g . diff ( varlist [ i ] ) #NEWLINE# #UNINDENT# #UNINDENT# for i in range ( n ) : #NEWLINE# #INDENT# for j in range ( i , n ) : #NEWLINE# #INDENT# out [ ( ( i + m ) , ( j + m ) ) ] = f . diff ( varlist [ i ] ) . diff ( varlist [ j ] ) #NEWLINE# #UNINDENT# #UNINDENT# for i in range ( N ) : #NEWLINE# #INDENT# for j in range ( ( i + 1 ) , N ) : #NEWLINE# #INDENT# out [ ( j , i ) ] = out [ ( i , j ) ] #NEWLINE# #UNINDENT# #UNINDENT# return out