# Decomposing an Affine transformation

An affine transformation is composed of rotations, translations, scaling and shearing.

In 2D, such a transformation can be represented using an augmented matrix by $$\begin{bmatrix} \vec{y} \\ 1 \end{bmatrix} =\begin{bmatrix} A & \vec{b} \ \\ 0, \ldots, 0 & 1 \end{bmatrix} \begin{bmatrix} \vec{x} \\ 1 \end{bmatrix}$$

vector b represents the translation. Bu how can I decompose A into rotation, scaling and shearing? I am trying to "interpolate" an affine transform so that, if I have points in a frame 1 and in a frame 3 such that a transform $T$ takes points from frame 1 to points in frame 3, then the interpolated transform would take my points from frame 1 to frame 2. If, for example, an 60° rotation is needed to go from frame 1 to frame 3, then a 30° one would be used to go from frame 1 to frame 2.

Thank you for your help.

• I find this question of interpolation an interesting one, but also difficult. Essentially you'd be asking about “nice” paths in the parameter space of all transformations. But characterizing what is a nice path might be more difficult than it looks at first glance. While your example with rotation only looks easy enough, it might be difficult to distinguish rotational and other effects. Take e.g. a 180° rotation. For interpolation, you can choose either direction, but you could also treat this as a scaling by $-1$, and interpolate that. Can you define which interpretation is correct, and why? – MvG Dec 21 '13 at 9:27
• What also has me worried is the fact that most shearing definitions have a close relation to the chosen coordinate system. As do anisotropic scalings. I'd concentrate on aspects that do not depend on coordinate system. Diagonalization of the matrix might proove useful here: you could then do a linear interpolation of the diagonal matrix between identity and the final matrix, and with a bit of luck that would result in reasonable geometric behavior. You'd have to be prepared to deal with complex eigenvalues, though. And this is quite a different approach to what your question here asks. – MvG Dec 21 '13 at 9:36
• @PeterSheldrick but quaternions would work only for rotations wouldn't they? – bigTree Dec 21 '13 at 17:49

## 2 Answers

If you scale, shear, and rotate (in that order) then you'll have:

$$\begin{bmatrix} A_{11} & A_{12} \ \\ A_{21} & A_{22} \end{bmatrix} = \begin{bmatrix} \cos \theta & -\sin \theta \\ \sin \theta & \cos \theta \end{bmatrix} \begin{bmatrix} 1 & m \\ 0 & 1 \end{bmatrix} \begin{bmatrix} s & 0 \\ 0 & s \end{bmatrix}.$$

Then

\begin{align*} A_{11} &= s \cos \theta\\ A_{12} &= s m \cos \theta - s \sin \theta\\ A_{21} &= s \sin \theta\\ A_{22} &= s m \sin \theta + s \cos \theta\\ \end{align*}

From the first and third equations,

$$s = \sqrt{A_{11}^2 + A_{21}^2},$$

and

$$\theta = \tan^{-1}\left( \frac{A_{21}}{A_{11}} \right).$$

Finally, substituting the first and third into the second and fourth,

$$m = \frac{A_{12} + A_{21}}{A_{11}} = \frac{A_{22} - A_{11}}{A_{21}}.$$

• I solved the equations and found the same results as you, but when I tried to test the equality for the matrix coefficients given by m (last substitution) on a real affine transformation matrix, it didn't work (the values are different...) – bigTree Dec 19 '13 at 20:40
• It may depend on how you're doing the transformation. I guessed at the order of operations. I also assumed an $x$ shear rather than a $y$ shear. As well as uniform scaling, now that I look at it again. But if you know specifically how the transformation is made, then just order the matrices right to left and multiply them out. – John Dec 19 '13 at 21:05
• Your equations are underspecified: you're solving for three variables $(s, m, \theta)$, but there's four parameters. One way to deal with this would be to add a $y$ shear as well. – Steve Jun 6 '15 at 4:16
• I might miss something here, but affine transformation has scale x and scale y – TripleS Dec 21 '15 at 9:38

I am trying to "interpolate" an affine transform […]

I'm suggesting a different approach, which should be well suited to interpolation, although it does not depend on splitting the transformation into separate elementary operations the way your question suggests. Instead, I'd use fractional powers of a matrix, as I'll describe now.

Suppose $A$ is diagonalizable (which will be the case for most transformations), then there exists an orthogonal matrix $P$ and a diagonal matrix $D$ such that $A=P\,D\,P^{-1}$. The entries of $D$ are the eigenvalues of $A$, which I'll call $\lambda_1$ and $\lambda_2$. Now you can define $A$ raised to the $t$-th power like this:

$$A^t = P\,D^t\,P^{-1} = P\,\begin{pmatrix}\lambda_1^t&0\\0&\lambda_2^t\end{pmatrix}\,P^{-1}$$

Now if you change $t$ continuously from $0$ to $1$, the matrix $A^t$ will change from identity to the matrix $A$. So this is your interpolation.

One thing you have to be careful about is the fact that the $\lambda_k$ will very likely be a conjugate pair of complex number. You can express them as $\lambda_k=e^{z_k}$, where $z_k=\log\lambda_k$, but the logarithm of a number is only defined up to multiples of $2\pi i$. So in this case, you should make sure that the imaginary part doesn't become too big, namely you want $-\pi\le\operatorname{Im}(z_k)\le\pi$. This ensures that the interpolation will not take more turns for a rotation than actually required. Furthermore, you should maintain $z_2=\bar{z_1}$ to make sure that the interpolating matrices will be real as well. With this choice, $\lambda_k^t=e^{t\cdot z_k}$ is well defined and behaves as you described for the case of rotation.

I've created a proof of concept implementation which you can use to experiment, in order to decide whether this is what you want. Here is a snapshot of the kind of interpolation this will create: With that experiment, I realized that one should include the translative part into the matrix as well, in just the way you composed a matrix including $A$ and $b$ in your question. Otherwise, the positions of the interpolated frames will depend on the location of the defining triples in the plane, which I consider undesirable.

• Therefore if we need to include b, the matrices P and D would be 3 by 3 matrices right? (I am currently trying to include your method in the code I have and will let you know when I have results) – bigTree Dec 21 '13 at 19:57
• @bigTree: Yes, they are $3\times3$ matrices. I'll probably edit my post one day to use that version from the start, but not just now. Your code is matlab, right? Reading the docs, it seems mpower should help there, and do most of the work already. If you end up with a working matlab code snippet, it would be nice if you could share it. – MvG Dec 21 '13 at 21:08
• I see the matrix no worries. My code is in matlab. I'll write the code and run some tests to see whether I encounter non diagonalizable matrices and share it. Thanks – bigTree Dec 21 '13 at 21:37
• So I just tried to implement the code. It turns out it doesn't work because many of the matrices I have are singular... – bigTree Dec 22 '13 at 2:59
• @bigTree: I think that this should work even for singular or non-diagonalizable matrices. But I'm not sure, and asked this question for details of matrices raised to fractional powers. – MvG Dec 22 '13 at 14:42