I'm new to Mathematica (installed couple of hours ago) and I need to compute a few Lie brackets between two vector fields $f$ and $g$. $$ f\left(\mathbf{x}\right) = \left( \begin{array}{c} x_3\\ x_4\\ \frac{-2m_2x_2x_3x_4 -g\cos\!\left(x_1\right)\left(m_2x_2 + m_1r\right)}{m_2{x_2}^2 + m_1r^2 + J_1 + J_2}\\ x_2{x_3}^2 - g\sin\!\left(x_1\right)\\ \end{array} \right)$$ $$g\left(\mathbf{x}\right) = \left( \begin{array}{c} 0\\ 0\\ \frac{1}{m_2{x_2}^2 + m_1r^2 + J_1 + J_2}\\ 0\\ \end{array} \right) $$ Where the vector for variables is $\mathbf{x}=\left(x_1,x_2,x_3,x_4\right)^T$. To compute the Lie bracket of $f$ along $g$ I need to do the following: $$\left[f,g\right] = \frac{\partial g}{\partial x} f - \frac{\partial f}{\partial x} g $$ And to do that I need the Jacobian of $f$ and $g$ which seems I can't do in Mathematica. I used the following code for $f$:

f  = {{x3}, {x4}, {-(2 m2 x2 x3 x4 + g Cos[x1] (m2 x2 + m1 r))/ (m2 x2^2 + m1 r^2 + J1 + J2)}, {x2 x3^2 - g Sin[x1]}}
x = {{x1}, {x2}, {x3}, {x4}}
jf = D[f, {x}]
jf // MatrixForm

This code while it gives the numerical values somehow correct, it kinda mess up the dimensions of the results. It should be a $4 \times 4$ matrix, but it gives me a $4 \times 1 \times 4 \times 1$ tensor. What am I doing wrong here ? The output of jf:

{{{{0}, {0}, {1}, {0}}}, {{{0}, {0}, {0}, {1}}}, {{{(g (m1 r + m2 x2) Sin[x1])/(J1 + J2 + m1 r^2 + m2 x2^2)}, {(-2 m2 x3 x4 - g m2 Cos[x1])/(J1 + J2 + m1 r^2 + m2 x2^2) - (m2 x2 (-2 m2 x2 x3 x4 - g (m1 r + m2 x2) Cos[x1]))/(J1 + J2 + m1 r^2 + m2 x2^2)^2}, {-((2 m2 x2 x4)/(J1 + J2 + m1 r^2 + m2 x2^2))}, {-((2 m2 x2 x3)/(J1 + J2 + m1 r^2 + m2 x2^2))}}}, {{{-g Cos[x1]}, {x3^2}, {2 x2 x3},0}}}}
{4, 1, 4, 1}
  • 1
    $\begingroup$ Mathematica doesn't distinguish between column and row vectors--all Lists are treated as general tensors. Therefore, if you don't want the extra dimensions in the output, you can feel free to omit them in the input. $\endgroup$ Commented Nov 26, 2012 at 17:29
  • $\begingroup$ Thanks didn't knew that, now it works as i intended $\endgroup$
    – Tabjones
    Commented Nov 26, 2012 at 17:35
  • 4
    $\begingroup$ Another way to look at it can be seen by looking Dimensions[{{a},{b}}] vs. Dimensions[{a,b}]. The first is interpreted as a matrix, and is transposable, the second is a vector and is not internally transposable. This is partially due to no distinctions existing between a vector and its dual in a Cartesian space. That, and internally they're just arrays of memory. $\endgroup$
    – rcollyer
    Commented Nov 26, 2012 at 17:37

1 Answer 1


You have some extra brackets { } that are causing your answer to have the wrong dimensions.

f = {x3, x4, -(2 m2 x2 x3 x4 + g Cos[x1] (m2 x2 + m1 r))/(m2 x2^2 + 
     m1 r^2 + J1 + J2), x2 x3^2 - g Sin[x1]}
x = {x1, x2, x3, x4}
jf = D[f, {x}]
jf // MatrixForm

Then you get a matrix jf that's 4 by 4

Mathematica graphics

  • 1
    $\begingroup$ Oh Jeez you are right, it was that simple! Thanks a lot $\endgroup$
    – Tabjones
    Commented Nov 26, 2012 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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