5
$\begingroup$

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?

$\endgroup$
3

2 Answers 2

10
$\begingroup$

See D (search for Hessian):

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

Mathematica graphics

$\endgroup$
4
  • $\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$
    – rsaavedra
    Commented Oct 16, 2016 at 20:44
  • $\begingroup$ For example, for D[f[x, y], {{x, y}, 2}]+D[g[x, y], {{x, y}, 2}] $\endgroup$
    – rsaavedra
    Commented Oct 16, 2016 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$
    – rsaavedra
    Commented Oct 16, 2016 at 21:09
  • $\begingroup$ @Saavestro You can find out more about the MatrixForm problem here. -- And you're welcome! Thanks, $\endgroup$
    – Michael E2
    Commented Oct 16, 2016 at 21:19
3
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ Outer is more elegant solution. $\endgroup$
    – Johu
    Commented Oct 16, 2016 at 20:23

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