Merge branch 'from_basis-normalize' into 'development'

Option to normalize rotation matrix

See merge request damask/DAMASK!705
This commit is contained in:
Sharan Roongta 2023-01-16 23:53:49 +00:00
commit 56ff8c769b
2 changed files with 11 additions and 4 deletions

View File

@ -902,7 +902,8 @@ class Rotation:
return Rotation(Rotation._om2qu(om)) return Rotation(Rotation._om2qu(om))
@staticmethod @staticmethod
def from_matrix(R: np.ndarray) -> 'Rotation': def from_matrix(R: np.ndarray,
normalize: bool = False) -> 'Rotation':
""" """
Initialize from rotation matrix. Initialize from rotation matrix.
@ -910,13 +911,17 @@ class Rotation:
---------- ----------
R : numpy.ndarray, shape (...,3,3) R : numpy.ndarray, shape (...,3,3)
Rotation matrix with det(R) = 1 and R.T R = I. Rotation matrix with det(R) = 1 and R.T R = I.
normalize : bool, optional
Rescales rotation matrix to unit determinant. Defaults to False.
Returns Returns
------- -------
new : damask.Rotation 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 @staticmethod
def from_parallel(a: np.ndarray, def from_parallel(a: np.ndarray,

View File

@ -774,9 +774,11 @@ class TestRotation:
).all() ).all()
def test_matrix(self,multidim_rotations): @pytest.mark.parametrize('normalize',[True,False])
def test_matrix(self,multidim_rotations,normalize):
m = multidim_rotations 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)) 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), assert np.logical_or(m.isclose(o,atol=atol),
m.isclose(f,atol=atol) m.isclose(f,atol=atol)