I am having a nightmare question these days with my Matrix -> TRS ( translation / rotation / scaling ) conversion and back ( working on a skeletal animation sys ).

Given a SO(3) matrix, which should equivalently produce a unit quaternion ( with the real part positive ), I cannot successfully convert the matrix to a quaternion and then, converting back the quaternion, to obtain the original matrix back.

Code for conversion is straighforward, copy-pasted from Internet and also determined by me on the paper.

The matrix is: