5

For example I can construct:

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

then:

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?

rsaavedra
  • 205
  • 3
  • 7

2 Answers2

10

See D (search for Hessian):

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

Mathematica graphics

Michael E2
  • 235,386
  • 17
  • 334
  • 747
3

I think Tuples is the missing element for you:

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

enter image description here

Johu
  • 4,918
  • 16
  • 43