To be more concrete, I'm working on an iOS app, and have a CATransform3D struct (basically a 4x4 transform array).

Is it possible to deduce all the different "operations" this matrix implies? Things like how much rotation, scale, etc. it implies?


1 Answer 1


You can decompose the matrix $\mathbf{M} = \mathbf{TRS}$ into basic transformations: translation, scaling, and rotation. Given this matrix:

$$\mathbf{M} = \begin{bmatrix} a_{00} & a_{01} & a_{02} & a_{03}\\ a_{10} & a_{11} & a_{12} & a_{13}\\ a_{20} & a_{21} & a_{22} & a_{23}\\ 0 & 0 & 0 & 1 \end{bmatrix}$$

You can decompose the translation by inspection using the last column $\mathbf{t} = (a_{03}, a_{13}, a_{23})$.

For scaling, we know that the first three columns of the matrix corresponds to the bases (axes). We can get the scale by the length/norm of these vectors, i.e. how much the bases were scaled. So the scale is $\mathbf{s} = (s_0, s_1, s_2)$ where:

$$\begin{matrix} s_0 = \left \|(a_{00}, a_{10}, a_{20}) \right \|\\ s_1 = \left \|(a_{01}, a_{11}, a_{21}) \right \|\\ s_2 = \left \|(a_{02}, a_{12}, a_{22}) \right \|\\ \end{matrix}$$

Now you have the scale, you can get rid of it using the $3\times 3$ sub-matrix that corresponds to $\mathbf{RS}$ by multiplying the matrix with the inverse of the scale $\mathbf{S}^{-1}$ to get $\mathbf{R}$

$$\begin{align} \mathbf{(RS)S}^{-1} &= \begin{bmatrix} a_{00} & a_{01} & a_{02}\\ a_{10} & a_{11} & a_{12}\\ a_{20} & a_{21} & a_{22}\\ \end{bmatrix} \begin{bmatrix} s_0 & 0 & 0\\ 0 & s_1 & 0\\ 0 & 0 & s_2 \end{bmatrix}^{-1} \\ &= \begin{bmatrix} a_{00} & a_{01} & a_{02}\\ a_{10} & a_{11} & a_{12}\\ a_{20} & a_{21} & a_{22}\\ \end{bmatrix} \begin{bmatrix} 1/s_0 & 0 & 0\\ 0 & 1/s_1 & 0\\ 0 & 0 & 1/s_2 \end{bmatrix} \end{align}$$

Thus ($\mathbf{(RS)S}^{-1} = \mathbf{RI} = \mathbf{R}$):

$$\mathbf{R} = \begin{bmatrix} a_{00}/s_0 & a_{01}/s_1 & a_{02}/s_2\\ a_{10}/s_0 & a_{11}/s_1 & a_{12}/s_2\\ a_{20}/s_0 & a_{21}/s_1 & a_{22}/s_2\\ \end{bmatrix}$$

This is the final rotation matrix. You can further decompose it using many ways. It is quit lengthy but you can search for decomposing a rotation matrix.

This method only gives an equivalent values in the form of translation, scaling and rotation (the original matrix maybe the result of other types of transformations). It may has problems with floating point precision with the rotation angles if you further used the decomposed angles, rounding errors may accumulate in the computations. You should not use it unless you did not construct the matrix yourself.

If you are the one who constructed the matrix and wanted the decomposition in order to be able to edit and display the translation, scale and rotation individually and independently, probabbly the cleanest why is to store the components of $\mathbf{t}$, $\mathbf{s}$ and $\mathbf{r}$ in a transform class individually as vectors (maybe quaternion for the rotation). Only when you need the transform matrix, construct a $\mathbf{TRS}$ matrix from these components (You can cache the matrix until some component is changed).

  • 2
    $\begingroup$ Can you clarify what the problems with floating-point precision are? I don't see anything in this method that would cause precision problems, unless the scale is really extreme. Also, worth noting that this method may fail if the matrix was composed from a sequence of matrices that includes both non-uniform scales and rotations. The $\mathbf{R}$ matrix will turn out not to be a rotation in that case, but will include some shear. $\endgroup$ Nov 16, 2016 at 16:11
  • 2
    $\begingroup$ All floating point numbers have intrinsic (bounded) error. Any time you perform operations, and particularly addition or subtraction, you compound the error, increasing the magnitude of the bounds. Hidden in the decomposition algorithm are many addition operations (both in the matrix multiplication and the scale magnitude calculation) and a square root (in the scale). Further decomposition will introduce further error. $\endgroup$ Nov 16, 2016 at 19:14
  • 1
    $\begingroup$ @Timbo There isn't any full matrix multiplication here though, just multiplying the columns of the matrix by the inverse scales. And a vector magnitude involves adding all positive quantities, so there's no catastrophic cancellation there; it doesn't produce much relative error, AFAICT. Anyway, the author clarified that they're talking about further decomposing the rotation matrix into Euler angles or suchlike, which makes more sense. $\endgroup$ Nov 16, 2016 at 20:31
  • $\begingroup$ Thanks – great answer. Follow-up: to get the original matrix back, I am assuming we need to follow a certain order of operations, starting from the identity matrix. Would this order be TRS? $\endgroup$
    – elsurudo
    Nov 28, 2016 at 15:45

Your Answer

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

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