在Scipy中解决过度确定的稀疏矩阵(从Matlab到Python)

时间:2015-10-29 09:44:54

标签: python matlab scipy linear-algebra sparse-matrix

给定一个大的稀疏矩阵A,它是带状或三角形(但是它被称为)和一个向量f,我想求Z,其中AZ = f。

有6条对角线,这里没有清楚显示。 basically there are 6 diagonals, not clearly shown here

A比M列有更多的M行(只有1,M~ = N),因此它是过度确定的。这是源代码Matlab代码,我想将其转换为Scipy等效代码。

Matlab的

A = A(:,2:end); #less one column
f = f(:);

Z = A\f;
Z = [0;-Z];
Z = reshape(Z,H,W);
Z = Z - min(Z(:));

我对Scipy的尝试给了我这个,但用scipy.sparse.linalg lsqr& lsmr比Matlab \慢得多,并且没有提供足够好的解决方案。 A创建为csr_matrix。

的Python

A = A[:,1:]
f = f.flatten(1)

Z = la.lsqr(A, f, atol=1e-6, btol=1e-6)
#Z = la.lsmr(A, f)   # the other method i used
Z = Z[0]
Z = np.append([0], np.negative(Z))
Z = np.reshape(Z, (height, width), order='F').copy()
Z = Z - Z.flatten(1).min()

有人可以推荐一个更好的替代方案来解决Z,这与Matlab一样有效和快速吗?

1 个答案:

答案 0 :(得分:1)

这似乎是solve_banded的合适人选。

不幸的是,提供带状矩阵的界面有点复杂。您可以先将稀疏矩阵转换为DIA格式,然后从那里开始工作。