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?
4 Answers
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} *)
-
-
$\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 decompositionM=R.S
$\endgroup$ 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\}$
-
1$\begingroup$ Why
ConjugateTranspose
? Any problem aboutm = {{1, 1}, {-1, 1}}; FullSimplify@ Solve[ScalingMatrix[{s1, s2}] . RotationMatrix[\[Theta]] == m && -Pi < \[Theta] < Pi, {s1, s2, \[Theta]}, Reals]
? $\endgroup$– yodeCommented Mar 10, 2022 at 21:00 -
$\begingroup$ Thank you @yode; you are right; no need for
Conjugate
here. $\endgroup$– kglrCommented Mar 10, 2022 at 21:17 -
$\begingroup$ So you replaced it with another unnecessary
Transpose
? :) $\endgroup$– yodeCommented Mar 10, 2022 at 21:25 -
$\begingroup$ I add a
-Pi < θ < Pi
because trigonometric functions are periodic functions, which has nothing to do withTranspose
.Transpose
orConjugate
before it is totally no need in your code $\endgroup$– yodeCommented Mar 10, 2022 at 21:49 -
$\begingroup$ @yode, I was trying to get the solution
{Sqrt[2], Pi/4}
from Ulrich's answer. WithoutTranspose
signs are flipped. $\endgroup$– kglrCommented 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
]
-
$\begingroup$ When the rotation matrix is
RotationMatrix[280 \[Degree]]
yourArcTan
cannot get the right angle. $\endgroup$– yodeCommented 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$ It should. It is the two-argument version of ˋArcTanˋ. $\endgroup$ Commented Mar 3, 2022 at 7:25
-