For example I can construct:

  row1 = D[f(x,y),x,#]&/@{x,y}
  row2 = D[f(x,y),x,#]&/@{x,y}


matrix = {row1,row2}

That yields a matrix like:

$$\left(\matrix{\frac{\partial^2 f}{\partial x^2} \quad\frac{\partial^2 f}{\partial x\partial y}\\ \frac{\partial^2 f}{\partial y\partial x}\quad \frac{\partial^2f}{\partial y^2}}\right)$$

Question: how to construct such matrix in a single line of code?


See D (search for Hessian):

D[f[x, y], {{x, y}, 2}]

Mathematica graphics

  • $\begingroup$ Ok that works, but when I do that I cannot perform matrix summation, Mathematica don't evaluate the operation (even if I use Evaluate). $\endgroup$ – Saavestro Oct 16 '16 at 20:44
  • $\begingroup$ For example, for D[f[x, y], {{x, y}, 2}]+D[g[x, y], {{x, y}, 2}] $\endgroup$ – Saavestro Oct 16 '16 at 20:44
  • $\begingroup$ Ok, I solved that, I was placing a //MatrixForm that was messing the evaluation, thank you for your helpful answer $\endgroup$ – Saavestro Oct 16 '16 at 21:09
  • $\begingroup$ @Saavestro You can find out more about the MatrixForm problem here. -- And you're welcome! Thanks, $\endgroup$ – Michael E2 Oct 16 '16 at 21:19

I think Tuples is the missing element for you:

 Partition[D[f[x, y], Sequence @@ #] & /@ Tuples[{x, y}, 2], 2]

enter image description here

  • $\begingroup$ Outer is more elegant solution. $\endgroup$ – Johu Oct 16 '16 at 20:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.