# Decompose affine transformation (including shear in x and y)

How can I fully decompose an affine transformation matrix that includes tx, ty, rotation (theta), scale-x (sx), scale-y (sy), shear-x and shear-y?

Using this matrix as example:

$$A = \begin{pmatrix}a & b & t_x\\c & d & t_y\\0 & 0 & 1\end{pmatrix}$$

I have seen similar questions in here, but they all limit the decomposition to a one value of skew/shear only (eg. Decomposition of a nonsquare affine matrix).

In this case, my parameters a-b-c-d contain rotation, scaling and shear, so I assume that no even rotation is as easy as an arctangent, given that:

a = sx * cos(theta)
b = sx * (-sin(theta)) * shear-x
c = sy * sin(theta) * shear-y
d = sy * cos(theta)


Therefore, typical equations that assume that shear-x and shear-y are equal, or that assume that one of them is zero are not used. Am I rigt?

In this general case with all four transformations, how can one find the parameters of each transformation?

## 1 Answer

The difficulty here is non-uniqueness.

Consider the two shear matrices (I'm going to use $2 \times 2$ to make typing easier; the translation part's easy to deal with in general, and then we just have the upper-left $2 \times 2$ anyhow): $$A = \begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix}, B = \begin{bmatrix} 1 & 0 \\ -0.5 & 1 \end{bmatrix}$$ Their product is $$AB = \begin{bmatrix} 0.5 & 1 \\ -0.5 & 1 \end{bmatrix}$$ That's exactly the same thing as scaling by $\sqrt{2}$ in $x$ and by $\frac{\sqrt{2}}{2}$ in $y$, and then rotating by 45 degrees.

So if I gave you the matrix $$\begin{bmatrix} 0.5 & 1 \\ -0.5 & 1 \end{bmatrix},$$ which answer would you want? The two shears, or the scale and rotation?

Why does this happen? Because you have five free parameters (rotation, 2 scales, 2 shears) and a four-dimensional set of matrices (all possible $2 \times 2$ matrices in the upper-left corner of your transformation). A continuous map from the first onto the second will necessarily be many-to-one.

In short: I think you need to ask a different question.

One possibility is to say "scale has to be the same in $x$ and $y$", or, perhaps worded better, "we only allow uniform scaling". Then there's generally a unique solution, although there are some bad cases: rotation-by-180 degrees and scaling-by-negative-one yield the same matrix, for instance.

• Thank you. What is then the meaning and usefulness of a single shear parameter, like in the other maths.se question I cited? I understand 4 parameters are needed (rot, 2 scale, at least 1 shear), but surely as you pointed out any kind of shear can be represented as rotation+2 scaling?
– user261459
Commented Aug 31, 2015 at 23:11
• To be honest ... I think that shear is a silly thing to include in a decomposition of $2 \times 2$ matrices, at least for computer graphics. I prefer to factor such a matrix into $R_1 S R_2$, where $S$ is a (possibly nonuniform) scale, and $R_1$ and $R_2$ are both rotations, which can be interpreted as rigid-change-of-basis in the domain and codomain; one then sees that after a choice of basis in both spaces, every transformation is just a (possibly nonuniform) scaling. Furthermore, this decomposition is exactly the one provided by the Singular Value Decomposition. Commented Sep 1, 2015 at 12:36
• Why include shear in ANY decomposition? Because the space of $2 \times 2$ matrix transformations is tough to understand, and representing it via compositions of simpler things (like $SO(2)$, the rotations of the plane, which correspond to points on a circle), $x$-shear (which is governed by a single real-number parameter) and scaling (two independent real numbers) can be pretty useful for one's intuition about the topology/geometry of the space (esp. if you're considering only the invertible $2\times 2$ matrices). Commented Sep 1, 2015 at 12:39