gpt4 book ai didi

python sparse gmres 混淆了输入参数

转载 作者:太空宇宙 更新时间:2023-11-04 03:39:06 34 4
gpt4 key购买 nike

我有一个使用 scipy.sparse.linalg.gmres 求解稀疏线性系统的简单代码

W, S  = load_data()
M = normalize(W.T.astype('float64'),'l1')
S = normalize(S.astype('float64'),'l1')
rhs = S[cat_id,:].T
print M.shape
print rhs.shape
p = gmres(M, rhs)

函数 load_data 从 matlab 的 .mat 文件中加载两个稀疏矩阵并省略。输出令人惊讶:

(150495, 150495)
(150495, 1)
Traceback (most recent call last):
File "explain.py", line 54, in <module>
pr(1)
File "explain.py", line 42, in pr
p = gmres(M, rhs)
File "<string>", line 2, in gmres
File "/usr/lib/python2.7/dist-packages/scipy/sparse/linalg/isolve/iterative.py", line 85, in non_reentrant
return func(*a, **kw)
File "/usr/lib/python2.7/dist-packages/scipy/sparse/linalg/isolve/iterative.py", line 418, in gmres
A,M,x,b,postprocess = make_system(A,M,x0,b,xtype)
File "/usr/lib/python2.7/dist-packages/scipy/sparse/linalg/isolve/utils.py", line 78, in make_system
raise ValueError('A and b have incompatible dimensions')
ValueError: A and b have incompatible dimensions

但是我已经按照文档运行了gmres

A : {sparse matrix, dense matrix, LinearOperator}

The real or complex N-by-N matrix of the linear system.

b : {array, matrix}

Right hand side of the linear system. Has shape (N,) or (N,1).

我只是不明白这段代码有什么问题,希望有任何想法。

最佳答案

gmres 的参数b不能是稀疏矩阵;它可以是一个 numpy 数组或矩阵。尝试

p = gmres(M, rhs.A)

关于python sparse gmres 混淆了输入参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27465119/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com