moble / quaternion

Add built-in support for quaternions to numpy
MIT License
610 stars 85 forks source link

dual quaternion support request #88

Closed qixuxiang closed 6 years ago

qixuxiang commented 6 years ago

Hi, Thanks for your awesome library share! Recently I found your awesome code share, and I want ot use dual quaternion. Would you please add dual quaternion support or show how to use your library to implement a simple dual quaternion? Thanks!

moble commented 6 years ago

It shouldn't be too hard to basically copy the code for quaternion and make some changes. I know that @igilitschenski has already done some work to add dual quaternions to this module. It looks like it's still a work in progress, but his branch would be a good starting point, and I'm sure he'd appreciate improvements to the code. Once it's in a usable state, I'd also be happy to merge it into this version.

Just so we have this in a public place, here's what I said (via email) to Igor when he was starting on his alterations.


Like numpy itself, the core of this module is actually written in C, even though it's then used just from python. Most of the python code in the module is just there to add a few features. So I would suggest starting off by just duplicating the C parts of the code to make sure that works, and then gradually add any python things you want.

Off the top of my head, here are some changes that would need to be made:

1) Copy quaternion.h, quaternion.c, and numpy_quaternion.c to dual_quaternion.h, dual_quaternion.c, and numpy_dual_quaternion.c 2) Replace every occurrence of "quaternion" with "dual_quaternion" in those files (keeping capitalization consistent, since that matters) 3) Add four new variables to the struct in dual_quaternion.h. Maybe (s, t, u, v)? 4) Change dual_quaternion_descr->elsize in numpy_dual_quaternion.c to be 8*8. 5) Add your new files to setup.py, along with quaternion.h, etc. 6) Add from .numpy_dual_quaternion import dual_quaternion to __init__.py.

See if you can successfully run python setup.py install. If so, see if you can import quaternion and do quaternion.dual_quaternion() without getting a segfault.

Assuming that works, you could then start to go through and make the functions actually work as they should for dual quaternions. Start with dual_quaternion_add, dual_quaternion_subtract, etc. Then you can run some basic tests to make sure they give the right answers. Most of this will probably just be adding your four new variables to the functions and changing some signs. There will also be some logic in functions like exp and log that would need to be rethought. You might want to just comment out a lot of stuff. If there are functions you know need to change, but want to keep around, you could just replace their contents with something like

PyErr_SetNone(PyExc_NotImplementedError);
return NULL;
igilitschenski commented 6 years ago

While I never got around implementing it myself, I asked this year my summer student, @beeshi, to help out on this. A first PR (that I still have to look through before I open a PR to this codebase) can be found at igilitschenski/quaternion#1.

qixuxiang commented 6 years ago

Thanks anyway, I have already solve my problem in another way.