# Second Derivative in MMA [closed]

I would like to ask, why the following second derivative is wrong in MMA

xx = {{x1, x2, x12}}
ccc = {{c11, c12, c13}, {c21, c22, c23}, {c31, c32, c33}}
fff = 0.5 xx.ccc. Transpose[xx]
FF1 = D[fff, {xx, 2}]


FF1 must be equal to ccc, but ...

Another question is:

Can we convert this code to C/Fortran in MMA without ACEGEN now?

• Well, the second derivative will be symmetric, but ccc is not symmetric. Also, the way arrays work in M, you probably want to change xxx and fff as follows: xx = {x1, x2, x12}; fff = 0.5 ccc.xx.xx; FF1 = D[fff, {xx, 2}] – Michael E2 Mar 19 '19 at 1:11
• @Michael E2 okay thanks ! – ABCDEMMM Mar 19 '19 at 1:14
• "can we convert this loop to C/Fortran in MMA without ACEGEN now?" Which loop? There is none. – Henrik Schumacher Mar 19 '19 at 8:02
• @HenrikSchumacher, I have changed.:) – ABCDEMMM Mar 19 '19 at 8:45

Here is the code supplied in the question with small corrections and with the use of MatrixForm to better see the results:

xx = {x1, x2, x12};
ccc = {{c11, c12, c13}, {c21, c22, c23}, {c31, c32, c33}};
fff = 0.5 xx.ccc.xx ;
FF1 = D[fff, {xx, 2}] // MatrixForm


This produces

0.5 (ccc + Transpose[ccc]) // MatrixForm
`

We see that Mathematica computes $$FF1 = \frac{1}{2} (ccc+ccc^T)$$, which is actually correct.

$$\displaystyle f=\frac{1}{2} \mathbf{x}^T C \mathbf{x}$$

$$\displaystyle \frac{df}{dx} = \frac{1}{2} (C + C^T) \mathbf{x}$$

$$\fbox{{ \displaystyle \frac{d^2f}{dx^2} = \frac{1}{2} (C + C^T)} }$$

See, for example, Appendix A.3 Vector and Matrix Differentiation in Horn, Robot Vision, MIT Press, pp. 459, 461.

Also,

$$C = \frac{1}{2} (C+C^T) + \frac{1}{2} (C-C^T)$$, known as the symmetric and antisymmetric parts of $$C$$.

$$C=\frac{1}{2} (C+C^T) \iff C$$ is symmetric.

• It could be improved by showing the Mathematica code that computes the formula you have posted. And also providing an explanation for why MMA apparently gives a "wrong" result. – QuantumDot Mar 19 '19 at 3:23
• Yes, excellent! Will do. – mjw Mar 19 '19 at 3:24
• @mjw, Transpose[xx].ccc.xx , is correct??? – ABCDEMMM Mar 19 '19 at 9:35
• The Mathematica Documentation "Multiplying Vectors and Matrices Tutorial" states: It is important to realize that you can use "dot" for both left- and right-multiplication of vectors by matrices. The Wolfram Language makes no distinction between "row" and "column" vectors. Dot carries out whatever operation is possible. (In formal terms, a.b contracts the last index of the tensor a with the first index of b.) – mjw Mar 19 '19 at 11:36
• If your question is about multiplying a vector by a matrix, in this example if $\mathbf{x}$ is a column vector, then $\mathbf{x}^T C \mathbf{x}$ is correct. If $\mathbf{x}$ is a row vector, then we would write it as $\mathbf{x} C \mathbf{x}^T.$ Mathematica makes no distinction and carries out the one that is permitted. – mjw Mar 19 '19 at 11:42