title: Calculating the Gradient and Hessian of a symbolic function containing Matrices
body: <p>I have the following function:</p>
  <p><a href="https://i.stack.imgur.com/DLx1b.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DLx1b.png" alt="Function" /></a></p>
  <p>I want to do a couple of things with it:</p>
  <ol>
  <li><p>Translate it into a SymPy function (done)</p>
  <pre class="lang-py prettyprint-override"><code>x = MatrixSymbol('x', 2, 1)
  fx = x.T * MatMul(Matrix([[1, 2], [4, 7]]), x) + x.T * Matrix([3, 5]) + 6*Identity(1)
  </code></pre>
  <ul>
  <li><p>The output using <code>display()</code> seems to confirm that it is working:</p>
  <p><a href="https://i.stack.imgur.com/S8V2K.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/S8V2K.png" alt="enter image description here" /></a></p>
  </li>
  </ul>
  </li>
  <li><p>Calculate the Gradient and Hessian at point <a href="https://i.stack.imgur.com/WKTCK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WKTCK.png" alt="point" /></a>:</p>
  <ul>
  <li><p>At this point I have tried the following function for the gradient:</p>
  <pre class="lang-py prettyprint-override"><code>v = list(ordered(fx.free_symbols))
  gradient = lambda f, v: Matrix([f]).jacobian(v)
  fxd = gradient(fx, v)
  </code></pre>
  <p>However, this outputs <code>[0, 0]</code> as the result which is incorrect. The result should be:</p>
  <p><a href="https://i.stack.imgur.com/ZeNJF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZeNJF.png" alt="gradient result" /></a></p>
  </li>
  <li><p>For the hessian I tried the following using SymPy's built-in function:</p>
  <pre class="lang-py prettyprint-override"><code>v = list(ordered(fx.free_symbols))
  fxdd = hessian(fx, v)
  </code></pre>
  <p>However, this function gives me the following error:</p>
  <p><code>ShapeError: Matrix size mismatch: (2, 2) + (2, 1)</code></p>
  <p>The output should be:</p>
  <p><a href="https://i.stack.imgur.com/A7G7T.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/A7G7T.png" alt="hessian result" /></a></p>
  </li>
  </ul>
  </li>
  </ol>
  <p>So, my question is, how to perform the operations in the second step?</p>