python - 具有互换行和列的稀疏 cholesky 分解

标签 python sparse-matrix matrix-factorization

我正在使用 python 的 scikits.sparse.cholmod 来获取对称矩阵的 cholesky 分解。

我将 cholesky() 的结果与 matlab 的 chol() 进行了比较。结果有一些行和列互换的差异。我正在尝试遍历因式分解以获得特征值,但这种差异似乎有问题。

这是我的代码:

import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse import csc_matrix
from scikits.sparse.cholmod import cholesky

A = csr_matrix([[1,2,0,0], [0,0,3, 0], [4,0,5, 0], [0, 0, 1, 2]])
B = (A*A.T)
print "B: "
print B.todense()

for i in range(10):
    factor = cholesky(B.tocsc())
    l = factor.L()  #l is lower triangular
    B = (l.T*l)
    print l.todense()

第一次迭代的下三角矩阵为:

[[ 2.23606798  0.         0.          0.        ]
[ 0.          3.          0.          0.        ]
[ 0.          1.          2.          0.        ]
[ 1.78885438  5.          0.          3.57770876]]

而matlab的下三角矩阵为:

[2.2361        0         0         0
     0    3.0000         0         0
1.7889    5.0000    3.5777         0
     0    1.0000         0    2.0000]

matlab 结果是合理的,因为它产生了正确的特征值。我在选择 python 中的稀疏矩阵类型时做错了吗?

最佳答案

cholesky 算法使用的是减少填充的算法。因此,它设置了一个置换矩阵 P。所以LL'=PBP'

可以引用factor documentation获取更多信息。

如果你打印 P 你会得到:

>>> factor.P()
array([0, 1, 3, 2], dtype=int32)

这正是两个矩阵之间的差异。最后两行和两列的排列。

关于python - 具有互换行和列的稀疏 cholesky 分解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36179967/

相关文章:

python - 大数据矩阵分解推荐系统给出MemoryError

python - 在 Python 中实现 AES/ECB/PKCS5 填充

python - 使用 Python 在 Google 中搜索

python - 在 pandas 列上应用条件以创建 bool 索引数组

matlab - 根据给定数据定义矩阵

algorithm - Web 游戏的稀疏(伪)无限网格数据结构

python - 在 Python 中,如何打印出代表井字游戏的数字板,每个数字之间有一个 "|"?

python - 在 python/R 中使用稀疏矩阵的优缺点?

java - Cholesky 分解在 Java 中生成 NaN

python - 大型稀疏矩阵的快速非负矩阵分解