How to get the Gradient and Hessian | Sympy : <p>Here is the situation: I have a symbolic function <strong>lamb</strong> which is function of the elements of the variable z and the functions elements of the variable h. Here is an image of the lamb symbolic function</p>

<p><a href="https://i.stack.imgur.com/ESNXb.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ESNXb.png" alt="enter image description here"></a>.</p>

<p>Now I would like the compute the Gradient and Hessian of this function with respect to the variables eta and xi. Of course I googled for it but I could not find a straight way for doing this. What I found is <a href="http://docs.sympy.org/dev/modules/vector/fields.html" rel="noreferrer">here</a>, but as I said it does not seen to be the best approach for this situation. Any idea? 
Bellow, the source code. Thanks.</p>

<pre><code>from sympy import Symbol, Matrix, Function, simplify

eta = Symbol('eta')
xi = Symbol('xi')

x = Matrix([[xi],[eta]])

h = [Function('h_'+str(i+1))(x[0],x[1]) for i in range(3)]
z = [Symbol('z_'+str(i+1)) for i in range(3)]

lamb = 0
for i in range(3):
    lamb += 1/(2*sigma**2)*(z[i]-h[i])**2
simplify(lamb)
</code></pre>
