enable array like slicing/iteration

This commit is contained in:
Martin Diehl 2020-06-30 12:16:47 +02:00
parent d06daec4cb
commit b8b34080fe
1 changed files with 18 additions and 1 deletions

View File

@ -52,6 +52,8 @@ class Rotation:
Unit quaternion that follows the conventions. Use .from_quaternion to perform a sanity check. Unit quaternion that follows the conventions. Use .from_quaternion to perform a sanity check.
""" """
if quaternion.shape[-1] != 4:
raise ValueError('Not a quaternion')
self.quaternion = quaternion.copy() self.quaternion = quaternion.copy()
@ -60,6 +62,7 @@ class Rotation:
return self.quaternion.shape[:-1] return self.quaternion.shape[:-1]
# ToDo: Check difference __copy__ vs __deepcopy__
def __copy__(self): def __copy__(self):
"""Copy.""" """Copy."""
return self.__class__(self.quaternion) return self.__class__(self.quaternion)
@ -70,7 +73,7 @@ class Rotation:
def __repr__(self): def __repr__(self):
"""Orientation displayed as unit quaternion, rotation matrix, and Bunge-Euler angles.""" """Orientation displayed as unit quaternion, rotation matrix, and Bunge-Euler angles."""
if self.quaternion.shape != (4,): if self.quaternion.shape != (4,):
return str(self.quaternion) # ToDo: could be nicer ... return 'Quaternions:\n'+str(self.quaternion) # ToDo: could be nicer ...
return '\n'.join([ return '\n'.join([
'Quaternion: (real={:.3f}, imag=<{:+.3f}, {:+.3f}, {:+.3f}>)'.format(*(self.quaternion)), 'Quaternion: (real={:.3f}, imag=<{:+.3f}, {:+.3f}, {:+.3f}>)'.format(*(self.quaternion)),
'Matrix:\n{}'.format(self.as_matrix()), 'Matrix:\n{}'.format(self.as_matrix()),
@ -78,6 +81,20 @@ class Rotation:
]) ])
def __len__(self):
return 0 if self.shape == () else len(self.shape)
def __getitem__(self,item):
if isinstance(item,tuple) and len(item) >= len(self):
raise IndexError('Too many indices')
return self.__class__(self.quaternion[item])
def __len__(self):
return 0 if self.shape == () else self.shape[0]
def __matmul__(self, other): def __matmul__(self, other):
""" """
Rotation of vector, second or fourth order tensor, or rotation object. Rotation of vector, second or fourth order tensor, or rotation object.