我正在尝试使用reverse_cuthill_mckee来减少稀疏、对称、复杂矩阵的带宽,从而提高其条件数。但是当我在更简单的矩阵上进行测试时,似乎reverse_cuthill_mckee可能会将0放在矩阵的对角线上。这将导致矩阵求逆的不稳定。 使用reverse_cuthill_mckee时有没有办法避免对角线上出现零? 这是在对角线上产生零的示例片段。
import numpy as np
import pandas as pd
import scipy
import scipy.sparse as sp
from scipy.sparse.csgraph import reverse_cuthill_mckee
graph = np.array([
[5, 1, 0, 0, 1],
[1, 6, 2, 0, 0],
[0, 2, 4, 0, 5],
[0, 0, 0, 7, 6],
[1, 0, 5, 6, 1]
])
graph_csc = sp.csc_matrix(graph)
cmindx=reverse_cuthill_mckee(graph_csc, symmetric_mode=True)
rm_graph_csc=graph_csc[cmindx]
看的时候
rm_graph_csc.toarray()
我们得到
array([[1, 6, 2, 0, 0],
[0, 2, 4, 0, 5],
[5, 1, 0, 0, 1],
[1, 0, 5, 6, 1],
[0, 0, 0, 7, 6]], dtype=int32)
Python 索引中的元素 (2,2) 或常规索引符号中的 (3,3) 为 0。是否可以避免这种情况?
根据文档
cmindx
是行和列的排列数组。生成的数组应保持对称。
In [145]: cmindx
Out[145]: array([1, 2, 0, 4, 3])
In [146]: graph_csc[cmindx[:,None],cmindx]
Out[146]:
<5x5 sparse matrix of type '<class 'numpy.intc'>'
with 15 stored elements in Compressed Sparse Column format>
In [147]: _.A
Out[147]:
array([[6, 2, 1, 0, 0],
[2, 4, 0, 5, 0],
[1, 0, 5, 1, 0],
[0, 5, 1, 1, 6],
[0, 0, 0, 6, 7]], dtype=int32)