在构造稀疏矩阵后,从稀疏到密集到稀疏再次降低密度

Converting from sparse to dense to sparse again decreases density after constructing sparse matrix

我正在使用 scipy 生成稀疏有限差分矩阵,最初从分块矩阵构造它,然后编辑对角线以考虑边界条件。生成的稀疏矩阵属于 BSR 类型。我发现,如果我使用 scipy.sparse.BSR_matrix 函数将矩阵转换为密集矩阵,然后再转换回稀疏矩阵,我得到的矩阵比以前更稀疏。这是我用来生成矩阵的代码:

size = (4,4)

xDiff = np.zeros((size[0]+1,size[0]))
ix,jx = np.indices(xDiff.shape)
xDiff[ix==jx] = 1
xDiff[ix==jx+1] = -1

yDiff = np.zeros((size[1]+1,size[1]))
iy,jy = np.indices(yDiff.shape)
yDiff[iy==jy] = 1
yDiff[iy==jy+1] = -1

Ax = sp.sparse.dia_matrix(-np.matmul(np.transpose(xDiff),xDiff))
Ay = sp.sparse.dia_matrix(-np.matmul(np.transpose(yDiff),yDiff))

lap = sp.sparse.kron(sp.sparse.eye(size[1]),Ax) + sp.sparse.kron(Ay,sp.sparse.eye(size[0]))

#set up boundary conditions
BC_diag = np.array([2]+[1]*(size[0]-2)+[2]+([1]+[0]*(size[0]-2)+[1])*(size[1]-2)+[2]+[1]*(size[0]-2)+[2])

lap += sp.sparse.diags(BC_diag)

如果我检查这个矩阵的稀疏性,我会看到以下内容:

lap
<16x16 sparse matrix of type '<class 'numpy.float64'>'
with 160 stored elements (blocksize = 4x4) in Block Sparse Row format>

但是,如果我将它转换为密集矩阵,然后再转换回相同的稀疏格式,我会看到一个更稀疏的矩阵:

sp.sparse.bsr_matrix(lap.todense())
<16x16 sparse matrix of type '<class 'numpy.float64'>'
with 64 stored elements (blocksize = 1x1) in Block Sparse Row format>

我怀疑发生这种情况的原因是因为我使用 sparse.kron 函数构建了矩阵,但我的问题是是否有一种方法可以在不首先转换为密集矩阵的情况下获得较小的稀疏矩阵,因为例如,如果我最终想模拟一个非常大的域。

[我被告知我的回答不正确。据我了解,原因是 Scipy 没有使用 Lapack 来创建矩阵,而是为此目的使用了自己的代码。有趣的。这些信息虽然出乎意料,但具有权威性。我会遵从的!

[我会留下答案以供参考,但不再断言答案是正确的。]

一般来说,对于稀疏矩阵这样复杂的数据结构,有两种情况:

  1. 构造函数提前知道结构的全部内容;或
  2. 该结构设计为逐渐构建,因此只有在结构完成后才能了解结构的全部内容。

复杂数据结构的经典案例就是二叉树的案例。你可以通过在二叉树完成后复制它来提高二叉树的效率。否则,树的标准红黑实现会留下一些搜索路径的长度是其他搜索路径的两倍——这通常没问题,但不是最优的。

现在,您可能知道所有这些,但我提到它是有原因的。 Scipy 取决于 Lapack。 Lapack 带来了几种不同的存储方案。其中两个是

  • 一般稀疏和
  • 带状

计划。 Scipy 似乎首先将矩阵存储为稀疏矩阵,其中显式存储了每个非零元素的索引;但是,在复制时,Scipy 注意到带状表示更合适——毕竟你的矩阵是带状的。

BSR 将数据存储在密集块中:

In [167]: lap.data.shape                                                        
Out[167]: (10, 4, 4)

在这种情况下,这些块有很多零。

In [168]: lap1 = lap.tocsr() 
In [170]: lap1                                                                  
Out[170]: 
<16x16 sparse matrix of type '<class 'numpy.float64'>'
    with 160 stored elements in Compressed Sparse Row format>
In [171]: lap1.data                                                             
Out[171]: 
array([-2.,  1.,  0.,  0.,  1.,  0.,  0.,  0.,  1., -3.,  1.,  0.,  0.,
        1.,  0.,  0.,  0.,  1., -3.,  1.,  0.,  0.,  1.,  0.,  0.,  0.,
        1., -2.,  0.,  0.,  0.,  1.,  1.,  0.,  0.,  0., -3.,  1.,  0.,
        0.,  1.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  1., -4.,  1.,  0., 
        ...
        0.,  0.,  1., -2.])

就地清理:

In [172]: lap1.eliminate_zeros()                                                
In [173]: lap1                                                                  
Out[173]: 
<16x16 sparse matrix of type '<class 'numpy.float64'>'
    with 64 stored elements in Compressed Sparse Row format>

如果我在使用kron时指定csr格式:

In [181]: lap2 = sparse.kron(np.eye(size[1]),Ax,format='csr') + sparse.kron(Ay,n
     ...: p.eye(size[0]), format='csr')                                         
In [182]: lap2                                                                  
Out[182]: 
<16x16 sparse matrix of type '<class 'numpy.float64'>'
    with 64 stored elements in Compressed Sparse Row format>