首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何判断一个矩阵是否是旋转矩阵?

如何判断一个矩阵是否是旋转矩阵?
EN

Stack Overflow用户
提问于 2018-12-17 11:07:19
回答 2查看 5.5K关注 0票数 2

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

代码语言:javascript
复制
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矩阵:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-12-17 11:07:59

旋转矩阵是一个行列式,它的行列式应该是1 ( orthonormal matrix )。

我的工具:

代码语言:javascript
复制
import numpy as np


def isRotationMatrix(R):
    # square matrix test
    if R.ndim != 2 or R.shape[0] != R.shape[1]:
        return False
    should_be_identity = np.allclose(R.dot(R.T), np.identity(R.shape[0], np.float))
    should_be_one = np.allclose(np.linalg.det(R), 1)
    return should_be_identity and should_be_one


if __name__ == '__main__':
    R = np.array([
        [0, 0, 1],
        [1, 0, 0],
        [0, 1, 0],
    ])
    print(isRotationMatrix(R))  # True
    R = np.array([
        [-1, 0, 0],
        [0, 1, 0],
        [0, 0, 1],
    ])
    print(isRotationMatrix(R))  # True
    print(isRotationMatrix(np.zeros((3, 2))))  # False
票数 0
EN

Stack Overflow用户

发布于 2018-12-17 13:42:40

我使用的是旋转矩阵的this定义。旋转矩阵应满足条件M (M^T) = (M^T) M = Idet(M) = 1。这里M^T表示M的转置,I表示单位矩阵,det(M)表示矩阵M的行列式。

您可以使用以下python代码来检查矩阵是否为旋转矩阵。

代码语言:javascript
复制
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.'  
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53808503

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档