给定一个大的稀疏矩阵A,它是带状或三角形(但是它被称为)和一个向量f,我想求Z,其中AZ = f。
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一样有效和快速吗?