【问题标题】:How to quickly determine if a matrix is a permutation matrix如何快速判断一个矩阵是否为置换矩阵
【发布时间】:2015-03-06 09:35:05
【问题描述】:

如何快速判断一个逻辑方阵是否为置换矩阵?例如,

不是置换矩阵,因为第 3 行有 2 个条目 1。

PS:permutation matrix 是一个二元方阵,每行和每列都有一个条目 1,其他地方为 0。

我定义了一个逻辑矩阵,比如

numpy.array([(0,1,0,0), (0,0,1,0), (0,1,1,0), (1,0,0,1)])

这是我的源代码:

#!/usr/bin/env python
import numpy as np

### two test cases
M1 = np.array([
    (0, 1, 0, 0),
    (0, 0, 1, 0),
    (0, 1, 1, 0),
    (1, 0, 0, 1)]);

M2 = np.array([
    (0, 1, 0, 0),
    (0, 0, 1, 0),
    (1, 0, 0, 0),
    (0, 0, 0, 1)]);

### fuction 
def is_perm_matrix(M) :
    for sumRow in np.sum(M, axis=1) :
        if sumRow != 1 :
            return False
    for sumCol in np.sum(M, axis=0) :
        if sumCol != 1 :
            return False
    return True

### print the result
print is_perm_matrix(M1) #False
print is_perm_matrix(M2) #True

有没有更好的实现方式?

【问题讨论】:

  • 你的矩阵是如何定义的?到目前为止你做了什么?
  • 你说的快是什么意思,是指大O还是别的什么意思?
  • 您是否将矩阵存储为列表列表?你能保证矩阵只包含整数 0 和 1,或者代码需要检查吗?您可以发布一些代码来向我们展示您的尝试吗?
  • @QiankunSU “方阵逻辑矩阵”不是 Python 类型。
  • 彼得伍德所说的。此外,说它是一个逻辑矩阵意味着它的元素是布尔值,即TrueFalse,但您的示例矩阵包含整数

标签: python numpy matrix linear-algebra


【解决方案1】:

这是一个简单的非 numpy 解决方案,它假定矩阵是一个列表列表,并且它只包含整数 0 或 1。如果矩阵包含布尔值,它也能正常工作。

def is_perm_matrix(m):
    #Check rows
    if all(sum(row) == 1 for row in m):
        #Check columns
        return all(sum(col) == 1 for col in zip(*m))
    return False

m1 = [
    [0, 1, 0],
    [1, 0, 0],
    [0, 0, 1],
]

m2 = [
    [0, 1, 0],
    [1, 0, 0],
    [0, 1, 1],
]

m3 = [
    [0, 1, 0],
    [1, 0, 0],
    [1, 0, 0],
]

m4 = [
    [True, False, False],
    [False, True, False],
    [True, False, False],
]

print is_perm_matrix(m1)
print is_perm_matrix(m2)
print is_perm_matrix(m3)
print is_perm_matrix(m4)

输出

True
False
False
False

【讨论】:

    【解决方案2】:

    这个呢:

    def is_permuation_matrix(x):
        x = np.asanyarray(x)
        return (x.ndim == 2 and x.shape[0] == x.shape[1] and
                (x.sum(axis=0) == 1).all() and 
                (x.sum(axis=1) == 1).all() and
                ((x == 1) | (x == 0)).all())
    

    快速测试:

    In [37]: is_permuation_matrix(np.eye(3))
    Out[37]: True
    
    In [38]: is_permuation_matrix([[0,1],[2,0]])
    Out[38]: False
    
    In [39]: is_permuation_matrix([[0,1],[1,0]])
    Out[39]: True
    
    In [41]: is_permuation_matrix([[0,1,0],[0,0,1],[1,0,0]])
    Out[41]: True
    
    In [42]: is_permuation_matrix([[0,1,0],[0,0,1],[1,0,1]])
    Out[42]: False
    
    In [43]: is_permuation_matrix([[0,1,0],[0,0,1]])
    Out[43]: False
    

    【讨论】:

    • 您可以使用not numpy.any(x & ~1) 进行检查。
    【解决方案3】:

    一种方法是调用np.sum 并传递一个轴参数,这应该会生成一个全为1 的数组,如果不是那么你就没有置换矩阵:

    In [56]:
    
    a = np.array([[0,1,0,0],[0,0,1,0],[0,1,1,0],[1,0,0,1]])
    a
    Out[56]:
    array([[0, 1, 0, 0],
           [0, 0, 1, 0],
           [0, 1, 1, 0],
           [1, 0, 0, 1]])
    
    In [57]:
    
    np.all(np.sum(a,axis=0) == np.ones((1,4)), True)
    Out[57]:
    array([False], dtype=bool)
    
    In [58]:
    
    np.all(np.sum(a,axis=1) == np.ones((1,4)), True)
    Out[58]:
    array([False], dtype=bool)
    
    In [60]:
    
    np.sum(a, axis=1) == np.ones([1,4])
    Out[60]:
    array([[ True,  True, False, False]], dtype=bool)
    In [59]:
    
    np.sum(a, axis=0) == np.ones([1,4])
    Out[59]:
    array([[ True, False, False,  True]], dtype=bool)
    
    In [61]:
    
    np.sum(a,axis=0)
    Out[61]:
    array([1, 2, 2, 1])
    In [62]:
    
    np.sum(a,axis=1)
    Out[62]:
    array([1, 1, 2, 2])
    

    【讨论】:

      猜你喜欢
      • 2018-01-11
      • 1970-01-01
      • 1970-01-01
      • 2022-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-18
      相关资源
      最近更新 更多