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?

Thanks a lot in advance!

  • 3
    $\begingroup$ 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}]`` $\endgroup$
    – Michael E2
    Mar 19 '19 at 1:11
  • $\begingroup$ @Michael E2 okay thanks ! $\endgroup$
    Mar 19 '19 at 1:14
  • $\begingroup$ "can we convert this loop to C/Fortran in MMA without ACEGEN now?" Which loop? There is none. $\endgroup$ Mar 19 '19 at 8:02
  • $\begingroup$ @HenrikSchumacher, I have changed.:) $\endgroup$
    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

enter image description here

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

enter image description here

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.


$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.

  • 1
    $\begingroup$ 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. $\endgroup$
    – QuantumDot
    Mar 19 '19 at 3:23
  • $\begingroup$ Yes, excellent! Will do. $\endgroup$
    – mjw
    Mar 19 '19 at 3:24
  • $\begingroup$ @mjw, Transpose[xx].ccc.xx , is correct??? $\endgroup$
    Mar 19 '19 at 9:35
  • 1
    $\begingroup$ 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.) $\endgroup$
    – mjw
    Mar 19 '19 at 11:36
  • 1
    $\begingroup$ 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. $\endgroup$
    – mjw
    Mar 19 '19 at 11:42

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