Option to normalize rotation matrix

This commit is contained in:
Philip Eisenlohr 2023-01-16 23:53:49 +00:00 committed by Sharan Roongta
parent bdf5faf40a
commit 5edd001d4c
2 changed files with 11 additions and 4 deletions

View File

@ -902,7 +902,8 @@ class Rotation:
return Rotation(Rotation._om2qu(om))
@staticmethod
def from_matrix(R: np.ndarray) -> 'Rotation':
def from_matrix(R: np.ndarray,
normalize: bool = False) -> 'Rotation':
"""
Initialize from rotation matrix.
@ -910,13 +911,17 @@ class Rotation:
----------
R : numpy.ndarray, shape (...,3,3)
Rotation matrix with det(R) = 1 and R.T R = I.
normalize : bool, optional
Rescales rotation matrix to unit determinant. Defaults to False.
Returns
-------
new : damask.Rotation
"""
return Rotation.from_basis(R)
return Rotation.from_basis(np.array(R,dtype=float) * (np.linalg.det(R)**(-1./3.))[...,np.newaxis,np.newaxis]
if normalize else
R)
@staticmethod
def from_parallel(a: np.ndarray,

View File

@ -774,9 +774,11 @@ class TestRotation:
).all()
def test_matrix(self,multidim_rotations):
@pytest.mark.parametrize('normalize',[True,False])
def test_matrix(self,multidim_rotations,normalize):
m = multidim_rotations
o = Rotation.from_matrix(m.as_matrix())
o = Rotation.from_matrix(m.as_matrix()*(0.9 if normalize else 1.0),
normalize=normalize)
f = Rotation(np.where(np.isclose(m.as_quaternion()[...,0],0.0,atol=atol)[...,np.newaxis],~o,o))
assert np.logical_or(m.isclose(o,atol=atol),
m.isclose(f,atol=atol)