-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Native quaternion implementation (#166)
This PR removes the dependency on boost::math::quaternion in favor of a minimal native implementation, which includes simple arithmetic and comparison operators, and conj, abs, norm and pow function implementations. The Quat object is serializeable and provides a numpy buffer interface. Closes #143.
- Loading branch information
Showing
23 changed files
with
899 additions
and
354 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import numpy as np | ||
from . import Quat, G3VectorQuat, G3TimestreamQuat | ||
|
||
__all__ = [] | ||
|
||
quat_types = (Quat, G3VectorQuat, G3TimestreamQuat) | ||
|
||
|
||
def quat_ufunc(self, ufunc, method, *inputs, **kwargs): | ||
"""Numpy ufunc interface for vectorized quaternion methods.""" | ||
if ufunc.__name__ in ["isinf", "isnan", "isfinite"] and len(inputs) == 1: | ||
v = getattr(ufunc, method)(np.asarray(inputs[0]), **kwargs) | ||
if ufunc.__name__ == "isfinite": | ||
return np.all(v, axis=-1) | ||
return np.any(v, axis=-1) | ||
if ufunc.__name__.startswith("logical"): | ||
args = [] | ||
for arg in inputs: | ||
if isinstance(arg, quat_types): | ||
arg = np.any(np.asarray(arg), axis=-1) | ||
args.append(arg) | ||
return getattr(ufunc, method)(*args, **kwargs) | ||
if method != "__call__" or kwargs: | ||
return NotImplemented | ||
if len(inputs) == 1: | ||
if ufunc.__name__ == "absolute": | ||
return self.abs() | ||
if ufunc.__name__ == "negative": | ||
return self.__neg__() | ||
if ufunc.__name__ == "conjugate": | ||
return self.conj() | ||
if ufunc.__name__ == "reciprocal": | ||
return Quat(1, 0, 0, 0) / self | ||
if len(inputs) == 2 and np.isscalar(inputs[1]): | ||
if ufunc.__name__ == "power": | ||
return self.__pow__(inputs[1]) | ||
return NotImplemented | ||
|
||
|
||
Quat.__array_ufunc__ = quat_ufunc | ||
G3VectorQuat.__array_ufunc__ = quat_ufunc | ||
G3TimestreamQuat.__array_ufunc__ = quat_ufunc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.