Formula for the transition from tensors to vectors in matrix calculus

derivativeskronecker productmatrix-calculuspartial derivativetensor-products

Given quadratic norm:

$f=\boldsymbol{g}^T\boldsymbol{p}=\boldsymbol{g}^TQ\left[\begin{array}{@{}c@{}} 0 \\ 0 \\ 1 \end{array} \right]$

where $\boldsymbol{g}=\left[\begin{array}{@{}c@{}} 0 \\ 0 \\ -9.81 \end{array} \right]$

$Q=R_z(\psi)R_y(\xi)R_x(\phi)$ – rotation matrix; $\boldsymbol{\theta}=\left[\begin{array}{@{}c@{}} \phi \\ \xi \\ \psi \end{array} \right]$

I need to get $\frac{df}{d\boldsymbol{\theta}}$, i.e. a vector $df=\left[\begin{array}{@{}c@{}} \frac{df}{d\boldsymbol{\phi}} \\ \frac{df}{d\boldsymbol{\xi}} \\ \frac{df}{d\boldsymbol{\psi}} \end{array} \right]$

My developments:

With the help of https://mathematica.stackexchange.com/questions/251079/derivative-matrix-by-vector-in-mathematica

I can get the derivative of the matrix $Q$ with respect to the vector $\boldsymbol{\theta}$ and get the tensor $T_{3 \times 9}$ as a result.

Then I use a formula

$\frac{dQ}{d\phi}=T_{3 \times 9}(I_{3 \times 3}\otimes\left[\begin{array}{@{}c@{}} 1 \\ 0 \\ 0 \end{array} \right])$

where $I_{3 \times 3}$ – identity matrix, $\otimes$ – Kronecker product;

to extract from the tensor the derivative of the rotation matrix $Q$ by angle $\phi$, but there is only $\frac{dQ}{d\phi}$.

What formula to use to get the whole vector $\frac{df}{d\boldsymbol{\theta}}$ ?

Code for the procedure in Mathematica:

Rx = RotationMatrix[\[Phi][t], {1, 0, 0}]; Ry = 
 RotationMatrix[\[Xi][t], {0, 1, 0}]; Rz = 
 RotationMatrix[\[Psi][t], {0, 0, 1}];

Q = Rz.Ry.Rx;

p = Q.{{0}, {0}, {1}};

g = {{0}, {0}, {-9.81}};

T = Flatten /@ D[Q, {{\[Phi][t], \[Xi][t], \[Psi][t]}}];

T.Transpose[KroneckerProduct[IdentityMatrix[3], {1, 0, 0}]] // 
  MatrixForm;

EDIT:

I found one method that only uses $\frac{dQ}{d\boldsymbol{\theta}}$, and the result is the same as direct differentiation $\frac{dQ^T}{d\boldsymbol{\theta}}$. The disadvantage of this method is the need to glue the matrix again. Maybe I can get around this with some kind of unified tensor operation ? To immediately receive the entire matrix, without additional gluing.

Rx = RotationMatrix[\[Phi][t], {1, 0, 0}];

Ry = RotationMatrix[\[Xi][t], {0, 1, 0}];

Rz = RotationMatrix[\[Psi][t], {0, 0, 1}];

Q = Rz.Ry.Rx;

v = {\[Phi][t], \[Xi][t], \[Psi][t]};

T1 = Flatten /@ D[Q, {v}];

T2 = Flatten /@ D[Transpose[Q], {v}];

P1 = {{1, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 0}, {0, 0,
     0, 0, 0, 0, 1, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 
    1, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 1, 0}, {0, 0, 1, 0, 0, 0, 0,
     0, 0}, {0, 0, 0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 1}};

A1 = T1.{{1, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 1, 0}, {0, 0, 0}, {0, 0,
      0}, {0, 0, 1}, {0, 0, 0}, {0, 0, 0}};

A2 = T1.{{0, 0, 0}, {1, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 1, 0}, {0, 0,
      0}, {0, 0, 0}, {0, 0, 1}, {0, 0, 0}};

A3 = T1.{{0, 0, 0}, {0, 0, 0}, {1, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 1,
      0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 1}};

Transpose[P1.ArrayFlatten[{{A1}, {A2}, {A3}}]] == 
   Flatten /@ D[Transpose[Q], {v}] // MatrixForm;

Best Answer

You can use Rodrigues rotation formula to represent rotation matrix in $\mathbb{R}^3$

$$\mathbf{R}(\alpha) = \mathbf{I} + \sin{\alpha}\mathbf{K} + (1-\cos{\alpha})\mathbf{K}^2$$

where matrix $\mathbf{K}$ encodes vector product with axis of rotation. For given vector $\mathbf{k}$ and any vector $\mathbf{x}$:

$$\mathbf{K} \mathbf{x} = \mathbf{k}\times \mathbf{x}$$

Then it is easy to compute derivative of $\mathbf{R}(\alpha)$ against $\alpha$ $$\frac{d}{d \alpha} \mathbf{R}(\alpha) = \cos\alpha\mathbf{K} + \sin\alpha\mathbf{K}^2 = \mathbf{K}\mathbf{R}(\alpha) $$ The last transition is tricky but useful. You can derive it by computing derivative of composition of rotations: $$\frac{d}{d \alpha} \mathbf{R}(\alpha) = \left.\frac{d}{d \beta}\mathbf{R}(\alpha+\beta) \right|_{\beta=0} = \left.\frac{d}{d \beta} \mathbf{R}(\beta)\mathbf{R}(\alpha) \right|_{\beta=0} = (\cos\beta\mathbf{K} + \sin\beta\mathbf{K}^2)\mathbf{R}(\alpha) = \mathbf{K}\mathbf{R}(\alpha)$$ The solution to your problem becomes then: $$\frac{d}{d \phi} \mathbf{g}^T\mathbf{p} = \frac{d}{d \phi} \mathbf{g}^T\mathbf{R}_z(\phi)\mathbf{R}_y(\varepsilon)\mathbf{R}_x(\psi)\mathbf{p}_0 = \mathbf{g}^T\mathbf{K}_z\mathbf{R}_z(\phi)\mathbf{R}_y(\varepsilon)\mathbf{R}_x(\psi)\mathbf{p}_0$$ Analogously for other axes $$\frac{d}{d \varepsilon} \mathbf{g}^T\mathbf{p} = \mathbf{g}^T\mathbf{R}_z(\phi)\mathbf{K}_y\mathbf{R}_y(\varepsilon)\mathbf{R}_x(\psi)\mathbf{p}_0$$ $$\frac{d}{d \psi} \mathbf{g}^T\mathbf{p} = \mathbf{g}^T\mathbf{R}_z(\phi)\mathbf{R}_y(\varepsilon)\mathbf{K}_x\mathbf{R}_x(\psi)\mathbf{p}_0$$ The three formulas produce scalars, which can be packed in vector $\mathbf{\theta}$. You can express the packing step as a matrix operation as well. If we now interpret your matrix $T$ as a concatenation of matrix derivatives: $$\mathbf{T}_{9\times 3} = \left[ \begin{array}{c} \mathbf{K}_z\mathbf{R}_z(\phi)\mathbf{R}_y(\varepsilon)\mathbf{R}_x(\psi) \\ \mathbf{R}_z(\phi)\mathbf{K}_y\mathbf{R}_y(\varepsilon)\mathbf{R}_x(\psi) \\ \mathbf{R}_z(\phi)\mathbf{R}_y(\varepsilon)\mathbf{K}_x\mathbf{R}_x(\psi) \end{array}\right]$$ If your software produces matrix $\tilde{T}$ with differently ordered columns and rows, you can shuffle them by applying relevant permutation matrices $\mathbf{P}_{9\times 9}$ and $\mathbf{P}_{3\times 3}$: $$\mathbf{T}_{9\times 3} = \mathbf{P}_{9\times 9} \tilde{\mathbf{T}}_{9\times 3} \mathbf{P}_{3\times 3}$$ The packing step can now be expressed as a matrix operation. $$\frac{d}{d\mathbf{\theta}} f = (\mathbf{g}^T \otimes \mathbf{I}_{3\times 3})\mathbf{T}\mathbf{p}_0$$