def mat2quat(M):
 (Qxx, Qyx, Qzx, Qxy, Qyy, Qzy, Qxz, Qyz, Qzz) = M.flat
 K = (np.array([[((Qxx - Qyy) - Qzz), 0, 0, 0], [(Qyx + Qxy), ((Qyy - Qxx) - Qzz), 0, 0], [(Qzx + Qxz), (Qzy + Qyz), ((Qzz - Qxx) - Qyy), 0], [(Qyz - Qzy), (Qzx - Qxz), (Qxy - Qyx), ((Qxx + Qyy) + Qzz)]]) / 3.0)
 (vals, vecs) = np.linalg.eigh(K)
 q = vecs[([3, 0, 1, 2], np.argmax(vals))]
 if (q[0] < 0):
  q *= (-1)
 return q

def mat2quat ( M ) : ( Qxx , Qyx , Qzx , Qxy , Qyy , Qzy , Qxz , Qyz , Qzz ) = M . flat K = ( np . array ( [ [ ( ( Qxx - Qyy ) - Qzz ) , 0 , 0 , 0 ] , [ ( Qyx + Qxy ) , ( ( Qyy - Qxx ) - Qzz ) , 0 , 0 ] , [ ( Qzx + Qxz ) , ( Qzy + Qyz ) , ( ( Qzz - Qxx ) - Qyy ) , 0 ] , [ ( Qyz - Qzy ) , ( Qzx - Qxz ) , ( Qxy - Qyx ) , ( ( Qxx + Qyy ) + Qzz ) ] ] ) / 3.0 ) ( vals , vecs ) = np . linalg . eigh ( K ) q = vecs [ ( [ 3 , 0 , 1 , 2 ] , np . argmax ( vals ) ) ] if ( q [ 0 ] < 0 ) : q *= ( - 1 ) return q
calculate quaternion corresponding to given rotation matrix parameters m : array-like 3x3 rotation matrix returns q : array closest quaternion to input matrix .