我有一个任务来检查矩阵是否是旋转矩阵,我编写代码如下:
import numpy as np
def isRotationMatrix(R):
# some code here
# return True or False
R = np.array([
[0, 0, 1],
[1, 0, 0],
[0, 1, 0],
])
print(isRotationMatrix(R)) # Should be True
R = np.array([
[-1, 0, 0],
[0, 1, 0],
[0, 0, 1],
])
print(isRotationMatrix(R)) # Should be False
我不知道如何实现函数isRotationMatrix
。
我的简单工具,它只适用于 3x3 矩阵:
def isRotationMatrix(R_3x3):
should_be_norm_one = np.allclose(np.linalg.norm(R_3x3, axis=0), np.ones(shape=3))
x = R_3x3[:, 0].ravel()
y = R_3x3[:, 1].ravel()
z = R_3x3[:, 2].ravel()
should_be_perpendicular = \
np.allclose(np.cross(x, y), z) \
and np.allclose(np.cross(y, z), x) \
and np.allclose(np.cross(z, x), y)
return should_be_perpendicular and should_be_norm_one
最佳答案
我正在使用this旋转矩阵的定义。旋转矩阵应满足条件 M (M^T) = (M^T) M = I
和 det(M) = 1
。这里M^T
表示M
的转置,I
表示单位矩阵,det(M)
表示矩阵的行列式M
。
您可以使用以下Python代码来检查矩阵是否是旋转矩阵。
import numpy as np
''' I have chosen `M` as an example. Feel free to put in your own matrix.'''
M = np.array([[0,-1,0],[1,0,0],[0,0,1]])
def isRotationMatrix(M):
tag = False
I = np.identity(M.shape[0])
if np.all((np.matmul(M, M.T)) == I) and (np.linalg.det(M)==1): tag = True
return tag
if(isRotationMatrix(M)): print 'M is a rotation matrix.'
else: print 'M is not a rotation matrix.'
关于python - 如何测试一个矩阵是否是旋转矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53808503/