# Computing angle of rotation and scaling factor directly from given transformation matrix

I have a simple 2D transformation matrix performing rotation combined with scaling \begin{bmatrix} 1 & 1 \\ -1 & 1 \end{bmatrix} Where the scalar $r$ is multiplied with \begin{bmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{bmatrix} Is there a simple method that can be performed on the input matrix to compute $r$ and $\theta$ directly?

An alternative to Henrik's version, perhaps more easily understandable is the use of

{Q, R} = QRDecomposition[{{1, 1}, {-1, 1}}]
(* {{{1/Sqrt[2], -(1/Sqrt[2])}, {1/Sqrt[2], 1/Sqrt[2]}}, {{Sqrt[2],0}, {0, Sqrt[2]}}} *)


Q is a rotation matrix and R contains the scaling

Solve[RotationMatrix[\[CurlyPhi] ] == Q, \[CurlyPhi]][[1]] /.C[1] -> 0
(*{\[CurlyPhi] -> \[Pi]/4} *)

• Very good, indeed. Commented May 25, 2018 at 12:26
• @Henrik: Thanks, what is needed for the direct solution is the the polar decomposition of a matrix, but I didn't find a Mathematica implementation... Commented May 25, 2018 at 12:47
• I could not find it either. =/ Commented May 25, 2018 at 12:48
• @Henrik: For "regular" matrix M one could take S = MatrixPower[Transpose[M].M, 1/2]; R = Inverse[Transpose[M]].S(* rotationmatrix R^t.R=Identity*) with polar decomposition M=R.S Commented May 25, 2018 at 12:53
m = {{1, 1}, {-1, 1}};
FullSimplify @ Solve[Transpose[RotationMatrix[θ]].ScalingMatrix[{s1, s2}] == m,
{s1, s2, θ}, Reals] /. C[1] -> 0 // TeXForm


$$\left\{\left\{\text{s1}\to -\sqrt{2},\text{s2}\to -\sqrt{2},\theta \to -\frac{3 \pi }{4}\right\},\left\{\text{s1}\to \sqrt{2},\text{s2}\to \sqrt{2},\theta \to \frac{\pi }{4}\right\}\right\}$$

• Why ConjugateTranspose? Any problem about m = {{1, 1}, {-1, 1}}; FullSimplify@ Solve[ScalingMatrix[{s1, s2}] . RotationMatrix[\[Theta]] == m && -Pi < \[Theta] < Pi, {s1, s2, \[Theta]}, Reals]?
– yode
Commented Mar 10, 2022 at 21:00
• Thank you @yode; you are right; no need for Conjugate here.
– kglr
Commented Mar 10, 2022 at 21:17
• So you replaced it with another unnecessary Transpose? :)
– yode
Commented Mar 10, 2022 at 21:25
• I add a -Pi < θ < Pi because trigonometric functions are periodic functions, which has nothing to do with Transpose. Transpose or Conjugate before it is totally no need in your code
– yode
Commented Mar 10, 2022 at 21:49
• @yode, I was trying to get the solution {Sqrt[2], Pi/4} from Ulrich's answer. Without Transpose signs are flipped.
– kglr
Commented Mar 10, 2022 at 22:07

# Solution one

This is your mat:

MatrixForm[mat = {{1, 1}, {-1, 1}}]


$$\small \left( \begin{array}{cc} 1 & 1 \\ -1 & 1 \\ \end{array} \right)$$

And this is the angle of rotation(Positive is counterclockwise, negative is counterclockwise):

Arg[mat.{1, 0}.{1, I}]


$$-\frac{\pi }{4}$$

This is the scale on each axis:

Eigenvalues[mat.Inverse[mat/Sqrt[Det[mat]]]]


$$\left\{\sqrt{2},\sqrt{2}\right\}$$

# Solution two

As this comment, this is rotation matrix is:

R = Inverse[Transpose[mat]].MatrixPower[Transpose[mat].mat, 1/2]


$$\begin{pmatrix} \frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}} \\ -\frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}} \\ \end{pmatrix}$$

We can get two scaling matrix generally:

scaleLeft = MatrixPower[mat.Transpose[mat], 1/2]


$$\begin{pmatrix} \sqrt{2} & 0 \\ 0 & \sqrt{2} \\ \end{pmatrix}$$

This mean $$\rm mat=scaleLeft.R$$

scaleRight = MatrixPower[Transpose[mat].mat, 1/2]


$$\begin{pmatrix} \sqrt{2} & 0 \\ 0 & \sqrt{2} \\ \end{pmatrix}$$

This mean $$\rm mat=R.scaleRight$$. But in this case, the scaleRight equal to scaleLeft.

You may try to apply this function:

{#[[2, 1, 1]], -ArcTan @@ (#[[1]].#Transpose[[[3]]])[[1]]} &[SingularValueDecomposition[#]] &


A bit simpler, using complex numbers:

f = AbsArg[({1, I}.#)[[1]]] &
f[{{1, 1}, {-1, 1}}]


$$\left\{\sqrt{2},-\frac{\pi }{4}\right\}$$

General case:

A random scale-rotation:

scale = If[Variance[Diagonal[S]] < $$MachinePrecision^2, Mean[Diagonal[S]],$$Failed
]


First we compute the polar decomposition of M

{U, Σ, V} = SingularValueDecomposition[M];
R = U.Transpose[V];
S = V.Σ.Transpose[V];


Checking its validity:

Norm[R.S - M, "Frobenius"]
Norm[Transpose[R].R - IdentityMatrix[dim], "Frobenius"]
Norm[S - Transpose[S], "Frobenius"]


2.01949*10^-15

6.36666*10^-16

7.11972*10^-16

Everything seems to be good. R is the rotation matrix we are looking for. If S is a mupltiple of the identy matrix then this multiple is the scaling factor. Otherwise, we have non-uniform scalings.

scale = If[
Norm[Mean[Diagonal[S]] IdentityMatrix[dim] - S,
"Frobenius"] < $$MachinePrecision^2, Mean[Diagonal[S]],$$Failed
]

• When the rotation matrix is RotationMatrix[280 \[Degree]] your ArcTan cannot get the right angle.
– yode
Commented Mar 3, 2022 at 6:41
• Thanks for letting me know. I think a Transpose was missing. Commented Mar 3, 2022 at 6:54
• No, I don't think ArcTan and do this. :)
– yode
Commented Mar 3, 2022 at 7:23
• It should. It is the two-argument version of ˋArcTanˋ. Commented Mar 3, 2022 at 7:25
• And I would recommend Arg here
– yode
Commented Mar 3, 2022 at 8:51