python - 如何测试一个矩阵是否是旋转矩阵?

标签 python numpy rotational-matrices

我有一个任务来检查矩阵是否是旋转矩阵,我编写代码如下:

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 = Idet(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/

相关文章:

python - 重定向到另一个域的 Django View

python - 权重需要多少字节的内存?

python - 如何对动态数据框进行OLS回归并估计斜率系数?

python - 如何在机器学习模型中使用 train.csv 、 test.csv 和 ground_truth.csv ? (交叉验证/Python)

computer-vision - 如何计算旋转矩阵之间的差异?

python - 将旋转矩阵正确转换为 Mayavi/Vtk 的(俯仰、滚动、偏航)

java - 矩形棱柱的 3D 旋转

python - Python 的 unittest 模块如何检测测试用例?

python - 将 Python 'print' 输出重定向到 Logger

python - 神经网络训练平台中的梯度下降