gpt4 book ai didi

numpy - 计算 scipy LinearOperator : "gmres did not converge" 的特征值时出错

转载 作者:行者123 更新时间:2023-12-03 21:39:53 24 4
gpt4 key购买 nike

我试图用 Scipy 解决一个大的特征值问题,其中矩阵 A是密集的,但我可以计算它对向量的作用而无需组装 A明确地。因此,为了避免矩阵 A 变大时出现内存问题,我想使用稀疏求解器 scipy.sparse.linalg.eigsLinearOperator实现此操作。

申请eigs到一个显式的 numpy 数组 A工作正常。但是,如果我申请 eigsLinearOperator相反,迭代求解器无法收敛。即使 matvec 也是如此LinearOperator的方法只是矩阵向量与给定矩阵的乘法 A .

下面附上了一个说明失败的最小示例(我使用移位反转模式,因为我对最小的几个特征值感兴趣)。这将计算随机矩阵的特征值 A很好,但应用于 LinearOperator 时失败直接从 A 转换而来.我试图摆弄迭代求解器的参数( v0ncvmaxiter )但无济于事。

我错过了一些明显的东西吗?有没有办法使这项工作?任何建议将不胜感激。非常感谢!

编辑:我应该澄清我所说的“使这项工作”是什么意思(谢谢,迪特里希)。下面的示例使用随机矩阵进行说明。但是,在我的应用程序中,我知道特征值几乎是纯虚数(或者如果我将矩阵乘以 1j 则几乎是纯实数)。我对 10-20 个最小幅度的特征值感兴趣,但如果我指定 which='SM',该算法表现不佳(即,即使对于小矩阵尺寸也不会停止) .因此,我通过传递参数 sigma=0.0, which='LM' 来使用 shift-invert 模式.我很高兴尝试不同的方法,只要它允许我计算一堆最小幅度的特征值。

from scipy.sparse.linalg import eigs, LinearOperator, aslinearoperator
import numpy as np

# Set a seed for reproducibility
np.random.seed(0)

# Size of the matrix
N = 100

# Generate a random matrix of size N x N
# and compute its eigenvalues
A = np.random.random_sample((N, N))
eigvals = eigs(A, sigma=0.0, which='LM', return_eigenvectors=False)
print eigvals

# Convert the matrix to a LinearOperator
A_op = aslinearoperator(A)

# Try to solve the same eigenproblem again.
# This time it produces an error:
#
# ValueError: Error in inverting M: function gmres did not converge (info = 1000).
eigvals2 = eigs(A_op, sigma=0.0, which='LM', return_eigenvectors=False)

最佳答案

我试过运行你的代码,但没有通过 sigma eigs() 的参数它运行没有问题(阅读 eigs() docs 以了解其含义)。我在你的例子中没有看到它的好处。

关于numpy - 计算 scipy LinearOperator : "gmres did not converge" 的特征值时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24216273/

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