From 5edd001d4c54a99db4fd21037e5b29c0b63e2b9d Mon Sep 17 00:00:00 2001 From: Philip Eisenlohr Date: Mon, 16 Jan 2023 23:53:49 +0000 Subject: [PATCH] Option to normalize rotation matrix --- python/damask/_rotation.py | 9 +++++++-- python/tests/test_Rotation.py | 6 ++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/python/damask/_rotation.py b/python/damask/_rotation.py index 6c5c143aa..f986a50b4 100644 --- a/python/damask/_rotation.py +++ b/python/damask/_rotation.py @@ -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, diff --git a/python/tests/test_Rotation.py b/python/tests/test_Rotation.py index 3cce0fcf1..056af2a93 100644 --- a/python/tests/test_Rotation.py +++ b/python/tests/test_Rotation.py @@ -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)