2D Matrix Decomposition

This demonstrates the polar decomposition of 2D matrices into their angle, scale, and shear components. The polarDecomp algorithm is taken from Shoemake & Duff "Matrix Animation and Polar Decomposition", 1992.

I found that the formula for the polar decomposition, on page 3, produces rotation matrices that don't account for diagonal axis flipping (swapping x with y). The article seems to factor out the flipping, but I didn't want to have a fourth component. Removing the sign(det(M)) factor eliminated this problem by incorporating the flipping into the rotation matrix.

It's very desirable to decompose a matrix generated by {angle, scale, shear} back into the same part values, but this happens only when scale is positive and shear is zero. If not, the rotation is affected. At least, the parts are stable once derived from the first matrix. This may help in UI, and certainly in interpolation.

Matrix generators

All the matrix ops are Vec . Mat

Rotation

In[17]:=

2d matrix decomposition_1.gif

Scaling

In[18]:=

2d matrix decomposition_2.gif

In[19]:=

2d matrix decomposition_3.gif

Shear

In[20]:=

2d matrix decomposition_4.gif

In[21]:=

2d matrix decomposition_5.gif

A handy pre-defined symbolic matrix

In[22]:=

2d matrix decomposition_6.gif

Out[22]//MatrixForm=

2d matrix decomposition_7.gif

Returns Q (the rotation matrix) and S, the stretch matrix

Unlike the version in Shoemake & Duff's polar_decomp paper, this one always returns a normal rotation matrix, and thereby accounts for axis flipping

In[23]:=

2d matrix decomposition_8.gif

Returns the angle of a rotation matrix

In[24]:=

2d matrix decomposition_9.gif

Returns the {x, y} scale of a stretch matrix

In[25]:=

2d matrix decomposition_10.gif

Returns the {x, y} shear of a stretch matrix

In[26]:=

2d matrix decomposition_11.gif

Converts the Q and S matrices into {angle, scale, shear}

In[27]:=

2d matrix decomposition_12.gif

Composition to produce the parts directly from the original matrix

In[28]:=

2d matrix decomposition_13.gif

Recreate the original matrix from the parts

In[29]:=

2d matrix decomposition_14.gif

Testing

This loses precision enough to require the SetPrecision

In[30]:=

2d matrix decomposition_15.gif

Test that matrix decomposition and recomposition is equal to the original

In[31]:=

2d matrix decomposition_16.gif

In[32]:=

2d matrix decomposition_17.gif

Out[32]=

2d matrix decomposition_18.gif

Test that the same components are recovered from matrices generated by random components

Note that they aren't the same unless scale is positive and shear is zero

In[33]:=

2d matrix decomposition_19.gif

In[34]:=

2d matrix decomposition_20.gif

In[35]:=

2d matrix decomposition_21.gif

Out[35]=

2d matrix decomposition_22.gif

Test that components are equal after two passes of decomp/comp. The 1st pass output is "canonical"

This always succeeds, so long as the precision is kept down

In[36]:=

2d matrix decomposition_23.gif

In[37]:=

2d matrix decomposition_24.gif

In[38]:=

2d matrix decomposition_25.gif

Out[38]=

2d matrix decomposition_26.gif

Examples

Note that Geometric transform does M . V, but that doesn't matter so long as it's consistent

Draw a transformed unit square, colored to make it easy to see axis flipping

In[39]:=

2d matrix decomposition_27.gif

Draw some random examples

In[40]:=

2d matrix decomposition_28.gif

Out[40]=

2d matrix decomposition_29.gif

Spikey Created withWolfram Mathematica7.0