4
$\begingroup$

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?

$\endgroup$

4 Answers 4

6
$\begingroup$

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} *)
$\endgroup$
4
  • $\begingroup$ Very good, indeed. $\endgroup$ Commented May 25, 2018 at 12:26
  • $\begingroup$ @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... $\endgroup$ Commented May 25, 2018 at 12:47
  • $\begingroup$ I could not find it either. =/ $\endgroup$ Commented May 25, 2018 at 12:48
  • 1
    $\begingroup$ @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 $\endgroup$ Commented May 25, 2018 at 12:53
2
$\begingroup$
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\}$

$\endgroup$
6
  • 1
    $\begingroup$ 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]? $\endgroup$
    – yode
    Commented Mar 10, 2022 at 21:00
  • $\begingroup$ Thank you @yode; you are right; no need for Conjugate here. $\endgroup$
    – kglr
    Commented Mar 10, 2022 at 21:17
  • $\begingroup$ So you replaced it with another unnecessary Transpose? :) $\endgroup$
    – yode
    Commented Mar 10, 2022 at 21:25
  • $\begingroup$ 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 $\endgroup$
    – yode
    Commented Mar 10, 2022 at 21:49
  • $\begingroup$ @yode, I was trying to get the solution {Sqrt[2], Pi/4} from Ulrich's answer. Without Transpose signs are flipped. $\endgroup$
    – kglr
    Commented Mar 10, 2022 at 22:07
2
$\begingroup$

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.

$\endgroup$
1
$\begingroup$

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

Your Answer

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

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