simpler (and scipy compatible)

This commit is contained in:
Martin Diehl 2021-01-13 09:35:42 +01:00
parent 2d6e6a2370
commit f4247e0f35
2 changed files with 7 additions and 23 deletions

View File

@ -253,24 +253,6 @@ class Rotation:
return self/other return self/other
def apply(self,other):
"""
Apply rotation to Rotation, vector, second order tensor, or fourth order tensor.
Parameters
----------
other : Rotation or numpy.ndarray of shape (...,3), (...,3,3), or (...,3,3,3,3)
Rotation, vector, or tensor on which to apply the rotation.
Returns
-------
rotated : Rotation or numpy.ndarray of shape (...,3), (...,3,3), or (...,3,3,3,3)
Composed rotation or rotated vector/tensor, i.e. transformed to frame defined by rotation.
"""
return self*other if isinstance(other,Rotation) else self@other
def __matmul__(self,other): def __matmul__(self,other):
""" """
Rotation of vector, second order tensor, or fourth order tensor. Rotation of vector, second order tensor, or fourth order tensor.
@ -286,9 +268,7 @@ class Rotation:
Rotated vector or tensor, i.e. transformed to frame defined by rotation. Rotated vector or tensor, i.e. transformed to frame defined by rotation.
""" """
if isinstance(other,Rotation): if isinstance(other,np.ndarray):
raise TypeError('Use "R1*R2", i.e. multiplication, to compose rotations "R1" and "R2"')
elif isinstance(other,np.ndarray):
if self.shape + (3,) == other.shape: if self.shape + (3,) == other.shape:
q_m = self.quaternion[...,0] q_m = self.quaternion[...,0]
p_m = self.quaternion[...,1:] p_m = self.quaternion[...,1:]
@ -308,9 +288,13 @@ class Rotation:
return np.einsum('...im,...jn,...ko,...lp,...mnop',R,R,R,R,other) return np.einsum('...im,...jn,...ko,...lp,...mnop',R,R,R,R,other)
else: else:
raise ValueError('Can only rotate vectors, 2nd order tensors, and 4th order tensors') raise ValueError('Can only rotate vectors, 2nd order tensors, and 4th order tensors')
elif isinstance(other,Rotation):
raise TypeError('Use "R1*R2", i.e. multiplication, to compose rotations "R1" and "R2"')
else: else:
raise TypeError(f'Cannot rotate {type(other)}') raise TypeError(f'Cannot rotate {type(other)}')
apply = __matmul__
def _standardize(self): def _standardize(self):
"""Standardize quaternion (ensure positive real hemisphere).""" """Standardize quaternion (ensure positive real hemisphere)."""

View File

@ -1021,10 +1021,10 @@ class TestRotation:
R = Rotation.from_random() R = Rotation.from_random()
assert R/R == R*R**(-1) == Rotation() assert R/R == R*R**(-1) == Rotation()
@pytest.mark.parametrize('item',[Rotation(),np.ones(3),np.ones((3,3)), np.ones((3,3,3,3))]) @pytest.mark.parametrize('item',[np.ones(3),np.ones((3,3)), np.ones((3,3,3,3))])
def test_apply(self,item): def test_apply(self,item):
r = Rotation.from_random() r = Rotation.from_random()
assert r.apply(item) == r*item if isinstance(item,Rotation) else (r.apply(item) == r@item).all() assert (r.apply(item) == r@item).all()
@pytest.mark.parametrize('angle',[10,20,30,40,50,60,70,80,90,100,120]) @pytest.mark.parametrize('angle',[10,20,30,40,50,60,70,80,90,100,120])
def test_average(self,angle): def test_average(self,angle):