KieranWynn / pyquaternion

A fully featured, pythonic library for representing and using quaternions
http://kieranwynn.github.io/pyquaternion/
MIT License
338 stars 69 forks source link

axis for unit quaterion given as (0, 0, 0) #86

Open jodyxha opened 1 week ago

jodyxha commented 1 week ago

Hi I noticed that the axis of the unit quaternion (1,0,0,0) is returned as (0,0,0).

This code

from pyquaternion import Quaternion

q=Quaternion()`
print("q: %s"%q)`
print("axis %s, angle %f"%(q.axis,q.angle))`

produces this output:

q: 1.000 +0.000i +0.000j +0.000k`
axis [0. 0. 0.], angle 0.000000`

I think this behavior is not correct. Of course, for an angle of 0 the axis is arbitrary, but it should not be [0,0,0]

teodron commented 1 week ago

The [0,0,0] is indeed not a unit vector. The axis is thus undetermined. Perhaps it would be best to indicate this situation through nan values instead of zeros and leave it up to the user of the library to indicate how they wish to proceed further. Another option would be through a special policy, e.g. offering a class property of the Quaernion class that users can override to their wishes, a property that could be named UndeterminedAxis or similar. By default, that could return nans instead of zeros, but the whole idea would be to have users configure the behavior. I also treated such an edge case a while ago by simply choosing 1,0,0 as an axis with no other consequence on my algorithm, but that is not a mathematically generally correct or without consequences. Thus, users should know that by default they will encounters nan values, but they can choose to tune the behavior if they really need a specific workaround and do not want to handle nans explicitly.

jodyxha commented 1 week ago

Yes, I have done a similar workaround, but it did not seem very clean :). I also think the returning of nan values would be a good option.

On Wed, Jun 26, 2024 at 8:36 AM Teodor Cioaca @.***> wrote:

The [0,0,0] is indeed not a unit vector. The axis is thus undetermined. Perhaps it would be best to indicate this situation through nan values instead of zeros and leave it up to the user of the library to indicate how they wish to proceed further. Another option would be through a special policy, e.g. offering a class property of the Quaernion class that users can override to their wishes, a property that could be named UndeterminedAxis or similar. By default, that could return nans instead of zeros, but the whole idea would be to have users configure the behavior. I also treated such an edge case a while ago by simply choosing 1,0,0 as an axis with no other consequence on my algorithm, but that is not a mathematically generally correct or without consequences. Thus, users should know that by default they will encounters nan values, but they can choose to tune the behavior if they really need a specific workaround and do not want to handle nans explicitly.

— Reply to this email directly, view it on GitHub https://github.com/KieranWynn/pyquaternion/issues/86#issuecomment-2190916488, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFZAB7JPQDEBH2U2R4D3HHDZJJON7AVCNFSM6AAAAABJ3LBE6GVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOJQHEYTMNBYHA . You are receiving this because you authored the thread.Message ID: @.***>