python求矩阵的秩_python – 计算所有子矩阵有多少个矩阵具有满秩

(现在是n = m // 2 1的部分解决方案,以及所请求的代码.)

令k:= m // 2 1

这有点等于问:“{-1,1}的m维向量的多少集合没有大小为min(k,n)的线性相关集合?”

对于这些矩阵,我们知道或可以假定:

>每个向量的第一个条目为1(如果不是,则将整数乘以-1).这使计数减少了2 ** m.

>列表中的所有向量是不同的(如果不是,具有两个相同向量的任何子矩阵具有非满秩).这消除了很多.选择(2 ** m,n)个不同向量的矩阵.

>向量列表按字典排序(排名不受排列影响).所以我们真的在考虑一组向量而不是列表.这减少了一个因子的数量! (因为我们需要明确).

有了这个,我们有一个解决方案n = 4,m = 8.只有八个不同的向量与属性的第一个条目是正的.来自8个不同载体的8个不同载体的组合(排序列表)只有一个.

array([[ 1, 1, 1, 1],

[ 1, 1, 1, -1],

[ 1, 1, -1, 1],

[ 1, 1, -1, -1],

[ 1, -1, 1, 1],

[ 1, -1, 1, -1],

[ 1, -1, -1, 1],

[ 1, -1, -1, -1]], dtype=int8)

该列表中的100个大小-4组合具有等级3.因此,具有属性的0个矩阵.

对于更一般的解决方案:

注意,存在具有第一坐标-1的2个(n-1)个矢量,并且选择(2 **(n-1),m)个矩阵进行检查.对于n = 8和m = 8,有128个向量和1.4297027e 12个矩阵.这可能有助于回答,“对于i = 1,…,k,有多少组合排名我?

或者,“什么样的矩阵(具有上述假设)少于满分?我认为答案是正确的,一个充分的条件是,“两列是彼此的倍数”.我有一种感觉,这是真的,我测试了所有4×4,5×5和6×6矩阵(必须拧紧测试)由于第一列被选择为均匀的,并且由于所有均匀矢量是彼此之间,具有除第一列之外的均匀柱的尺寸为k的任何子矩阵的等级都小于k.

这不是一个必要的条件.以下矩阵是单数(第一加四等于第三加二).

array([[ 1, 1, 1, 1, 1],

[ 1, 1, 1, 1, -1],

[ 1, 1, -1, -1, 1],

[ 1, 1, -1, -1, -1],

[ 1, -1, 1, -1, 1]], dtype=int8)

由于只有两个可能的值(-1和1),其中m> 2,k:= m // 2 1,n = k和第一列-1的所有mxn矩阵在每列中具有多数成员(即,最少k个成员是一样的).所以对于n = k,答案​​是0.

对于n <8,这里是生成向量的代码.

from numpy import unpackbits, arange, uint8, int8

#all distinct n-length vectors from -1,1 with first entry -1

def nvectors(n):

if n > 8:

raise ValueError #is that the right error?

return -1 + 2 * (

#explode binary numbers to arrays of 8 zeroes and ones

unpackbits(arange(2**(n-1),dtype=uint8)) #unpackbits only takes uint

.reshape((-1,8)) #unpackbits flattens, so we need to shape it to 8 bits

[:,-n:] #only take the last n bytes

.view(int8) #need signed

)

矩阵发生器

#generate all length-m matrices that are combinations of distinct n-vectors

def matrix_g(n,m):

return (array(mat) for mat in combinations(nvectors(n),m))

以下是检查长度maxrank的所有子矩阵是否具有满秩的函数.如果有的话小于maxrank,它会停止,而不是检查所有的组合.

rankof = np.linalg.matrix_rank

#all submatrices of at least half size have maxrank

#(we only need to check the maxrank-sized matrices)

def halfrank(matrix,maxrank):

return all(rankof(submatr) == maxrank for submatr in combinations(matrix,maxrank))

生成所有具有全等级的半矩阵的矩阵

def nicematrices(m,n):

maxrank = min(m // 2 1,n)

return(matr for matr in matrix_g(n,m)if halfrank(matr,maxrank))

把它们放在一起:

import numpy as np

from numpy import unpackbits, arange, uint8, int8, array

from itertools import combinations

#all distinct n-length vectors from -1,1 with first entry -1

def nvectors(n):

if n > 8:

raise ValueError #is that the right error?

if n==0:

return array([])

return -1 + 2 * (

#explode binary numbers to arrays of 8 zeroes and ones

unpackbits(arange(2**(n-1),dtype=uint8)) #unpackbits only takes uint

.reshape((-1,8)) #unpackbits flattens, so we need to shape it to 8 bits

[:,-n:] #only take the last n bytes

.view(int8) #need signed

)

#generate all length-m matrices that are combinations of distinct n-vectors

def matrix_g(n,m):

return (array(mat) for mat in combinations(nvectors(n),m))

rankof = np.linalg.matrix_rank

#all submatrices of at least half size have maxrank

#(we only need to check the maxrank-sized matrices)

def halfrank(matrix,maxrank):

return all(rankof(submatr) == maxrank for submatr in combinations(matrix,maxrank))

#generate all matrices that have all half-matrices with full rank

def nicematrices(m,n):

maxrank = min(m//2+1,n)

return (matr for matr in matrix_g(n,m) if halfrank(matr,maxrank))

#returns (number of nice matrices, number of all matrices)

def count_nicematrices(m,n):

from math import factorial

return (len(list(nicematrices(m,n)))*factorial(m)*2**m, 2**(m*n))

for i in range(0,6):

print (i, count_nicematrices(i,i))

count_nicematrices(5,5)对我来说大约需要15秒钟,绝大多数是由matrix_rank函数取代的.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值