Create a new Quat from a Matrix44.

        Note that the matrix and indexes are column major.

def set_from_matrix44(self, mat):
        """Create a new Quat from a Matrix44.

        Note that the matrix and indexes are column major.
        """

        # Matrix trace
        trace = mat.data[0][0] + mat.data[1][1] + mat.data[2][2] + 1.0

        if trace > 0.00000001:
            # n4 is norm of quaternion multiplied by 4.
            n4 = math.sqrt(trace) * 2
            self.x = (mat.data[1][2] - mat.data[2][1]) / n4
            self.y = (mat.data[2][0] - mat.data[0][2]) / n4
            self.z = (mat.data[0][1] - mat.data[1][0]) / n4
            self.w = n4 / 4.0
            return self

        # TODO: unittests for code below when trace is small.

        # matrix trace <= 0
        if mat.data[0][0] > mat.data[1][1] and mat.data[0][0] > mat.data[2][2]:
            s = 2.0 * math.sqrt(1.0 + mat.data[0][0] - mat.data[1][1] -
                                mat.data[2][2])
            self.x = s / 4.0
            self.y = (mat.data[1][0] + mat.data[0][1]) / s
            self.z = (mat.data[2][0] + mat.data[0][2]) / s
            self.w = (mat.data[2][1] - mat.data[1][2]) / s
            return self
        elif mat.data[1][1] > mat.data[2][2]:
            s = 2.0 * math.sqrt(1.0 - mat.data[0][0] + mat.data[1][1] -
                                mat.data[2][2])
            self.x = (mat.data[1][0] + mat.data[0][1]) / s
            self.y = s / 4.0
            self.z = (mat.data[2][1] + mat.data[1][2]) / s
            self.w = (mat.data[2][0] - mat.data[0][2]) / s
            return self
        else:
            s = 2.0 * math.sqrt(1.0 - mat.data[0][0] - mat.data[1][1] +
                                mat.data[2][2])
            self.x = (mat.data[2][0] + mat.data[0][2]) / s
            self.y = (mat.data[2][1] + mat.data[1][2]) / s
            self.z = s / 4.0
            self.w = (mat.data[1][0] - mat.data[0][1]) / s
            return self