解决Scipy中超定的稀疏矩阵(从Matlab到Python) [英] Solve Over-determined sparse matrix in Scipy (from Matlab to Python)

查看:169
本文介绍了解决Scipy中超定的稀疏矩阵(从Matlab到Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个带状或三对角的大稀疏矩阵A(无论如何称呼)和一个向量f,我想求解Z,其中AZ = f.

Given a large sparse matrix A which are banded or tridiagonals (however it is called) and a vector f, I would like to solve for Z, where AZ = f.

有6条对角线,此处未清楚显示.

There are 6 diagonals, not clearly shown here.

A的M行多于N列(正好为1,M〜= N),因此它是超定的.这是Matlab的源代码,我想将其转换为等效的Scipy.

A has more M rows than N columns (just by 1, M ~= N), hence it is over-determined. Here is the source Matlab code, and I would like to convert it to its Scipy equivalent.

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解决了Z .sparse.linalg lsqr & lsmr 比Matlab慢很多,但也没有提供足够好的解决方案. A被创建为csr_matrix.

My attempt on Scipy gives me this, but solving Z with scipy.sparse.linalg lsqr & lsmr is a lot slower than Matlab \ as well as not giving a good enough solution. A is created as 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一样有效且快速?

Could anyone recommend a better alternative to solve for Z, that is as effective and fast as Matlab \ ?

推荐答案

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

Unfortunately, the interface for providing the banded matrix is a little complex. You could start by converting your sparse matrix to DIA format, and work from there.

这篇关于解决Scipy中超定的稀疏矩阵(从Matlab到Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆