pyquaternion icon indicating copy to clipboard operation
pyquaternion copied to clipboard

What to do when matrix is not quite within orthogonality tolerance

Open fishbotics opened this issue 3 years ago • 2 comments

Hi,

Thanks for writing this library. I use it all the time and love its simplicity for 3D representations. I have lately been dealing with an issue pretty commonly where I am getting a transformation matrix from somewhere and it's very close to being orthogonal, but not quite within the tolerance. These transformations should be valid, but I'm guessing there's some lost precision when using floats vs double. Here is an example:

import numpy as np
from pyquaternion import Quaternion

a = np.array([[-7.0710677e-01, -7.0710677e-01,  0.0000000e+00], 
    [-7.0710677e-01,  7.0710677e-01, -9.7931776e-12], 
    [ 6.9248223e-12, -6.9248223e-12, -1.0000000e+00], 
    [ 0.0000000e+00,  0.0000000e+00,  0.0000000e+00]], 
    dtype=np.float32) 
    
 print(np.dot(a, a.conj().transpose()))

Gives

array([[ 9.99999940e-01, -1.26880515e-08, -8.15222941e-20],
      [-1.26880515e-08,  9.99999940e-01,  0.00000000e+00],
      [-8.15222941e-20,  0.00000000e+00,  1.00000000e+00]], dtype=float32)

This looks really close to the identity, but not quite. Any tips on how to project this back onto SO3 so I can produce the corresponding quaternion?

Thanks!

fishbotics avatar Feb 26 '21 01:02 fishbotics

I found the solution in one of the old PRs and in docs/index.md (you can set atol and rtol when initializing). This is missing from the online docs. I can submit a PR to add it.

fishbotics avatar Feb 26 '21 02:02 fishbotics

@fishbotics I think that would be here? https://github.com/KieranWynn/pyquaternion/pull/44

DanielTakeshi avatar Sep 27 '22 20:09 DanielTakeshi