【问题标题】:how can I check if a matrix is square using sympy?如何使用 sympy 检查矩阵是否为正方形?
【发布时间】:2021-05-09 11:14:54
【问题描述】:

当输入是我使用的np.ndarray 类型时检查矩阵是否为正方形

if len(V) != len(V[0]):
        raise ValueError('V is not a square matrix')

但是当我想使用 sympy 时,len(V),例如对于 2x2 矩阵,返回 4 而不是 2。我尝试使用返回 (2,2) 的 v.shape,但我不知道如果输入矩阵与此输出不成正方形,如何实现 ValueError。

【问题讨论】:

    标签: python python-3.x matrix sympy


    【解决方案1】:

    形状函数应该做你想做的:

    from sympy import shape
    
    def isSquare(M):
       s = shape(M)
       return s[0] == s[1]
    

    也可以看看docs

    【讨论】:

    • 还有M.is_square
    猜你喜欢
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 2013-05-17
    • 2022-10-22
    • 2023-04-10
    • 2016-06-28
    • 2015-03-05
    • 1970-01-01
    相关资源
    最近更新 更多