在 Scipy 中求解 Over-determined 稀疏矩阵(从 Matlab 到 Python)

Solve Over-determined sparse matrix in Scipy (from Matlab to Python)

给定一个大的带状或三对角矩阵稀疏矩阵 A 和一个向量 f,我想求解 Z,其中 AZ = f。

有6条对角线,这里没有显示清楚。

A 的 M 行多于 N 列(仅多 1,M ~= N),因此它是 over-determined。这是 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 求解 Z 比 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 一样有效和快速 \ ?

这看起来很适合solve_banded

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