题解 | 使用拉普拉斯展开式的 4x4 矩阵的行列式

使用拉普拉斯展开式的 4x4 矩阵的行列式

https://www.nowcoder.com/practice/5b3331a8000c427f81cbe2aef7af36cd

import numpy as np

def determinant_4x4(A) :
    A=np.array(A)
    if A.shape[0]==2:
        return A[0][0]*A[1][1]-A[0][1]*A[1][0]
    else:
        s=0
        for i in range(A.shape[0]):
            k=np.delete(np.arange(A.shape[0]),i)
            s+=(-1)**i*A[0][i]*determinant_4x4(A[1:A.shape[0],k])
        return s
        
# 主程序
if __name__ == "__main__":
    # 输入矩阵
    matrix_inputa = input()

    # 处理输入
    import ast
    matrix = ast.literal_eval(matrix_inputa)

    # 调用函数计算逆矩阵
    output = determinant_4x4(matrix)
    
    # 输出结果
    print(output)

全部评论

相关推荐

对空六翼:你真幸运,碰见这么好的人,不像我,秋招的时候被室友骗进cx了
实习好累,可以辞职全力准...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务