Scipy的隐式重启Arnoldi方法中的迭代次数

huangapple go评论140阅读模式
英文:

Iteration count in Scipy's Implicitly Restarted Arnoldi Method

问题

  1. 我试图获得在找到矩阵的`k`个最小特征值之前需要的迭代次数尽管Scipy可以很容易地找到特征值但我没有找到一种可以轻松获取所需迭代次数的方法
  2. 在如下的代码块中我认为将变量*i*中的计数器添加进去会解决我的问题但这不可能是真的因为*i*可能大于maxiter这是一个明显的矛盾
  3. ```python
  4. import scipy.sparse.linalg._eigen.arpack as bark
  5. #%%
  6. def ct(v):
  7. return np.conj(v.T)
  8. def eigsb(A, k=6, M=None, sigma=None, which='LM', v0=None,
  9. ncv=None, maxiter=None, tol=0, return_eigenvectors=True,
  10. Minv=None, OPinv=None, OPpart=None):
  11. n = A.shape[0]
  12. mode = 1
  13. M_matvec = None
  14. Minv_matvec = None
  15. matvec = bark._aslinearoperator_with_dtype(A).matvec
  16. params = bark._UnsymmetricArpackParams(n, k, A.dtype.char, matvec, mode,
  17. M_matvec, Minv_matvec, sigma,
  18. ncv, v0, maxiter, which, tol)
  19. i=0
  20. while not params.converged:
  21. params.iterate()
  22. i=i+1
  23. return i,params.extract(return_eigenvectors)
  24. ###制作Hermitian矩阵
  25. d = 50
  26. mat = np.random.rand(d,d)+ 1j * (np.random.rand(d,d))
  27. mat = mat - (np.random.rand(d,d)+ 1j * (np.random.rand(d,d)))
  28. mat = mat + ct(mat)
  29. ###
  30. i,(vals, vecs) = eigsb(mat, k=1, which='SR', ncv=6, maxiter=50)

是否有其他方法我应该尝试?或者是否有关于计数i和我没有看到的真正迭代值之间的转换?

  1. <details>
  2. <summary>英文:</summary>
  3. I&#39;m trying to get a number of iterations needed until convergence for finding the `k` smallest eigenvalues of a matrix. While scipy can find the eigenvalues without much difficulty, there&#39;s no way that I see to easily get out the number of iterations needed.
  4. In the code block as shown, my thought was that the addition of the counter in variable *i* would solve my problem, but this can&#39;t be the case as *i* can be greater than maxiter, a clear contradiction.

import scipy.sparse.linalg._eigen.arpack as bark
#%%
def ct(v):
return np.conj(v.T)

def eigsb(A, k=6, M=None, sigma=None, which='LM', v0=None,
ncv=None, maxiter=None, tol=0, return_eigenvectors=True,
Minv=None, OPinv=None, OPpart=None):
n = A.shape[0]
mode = 1
M_matvec = None
Minv_matvec = None
matvec = bark._aslinearoperator_with_dtype(A).matvec
params = bark._UnsymmetricArpackParams(n, k, A.dtype.char, matvec, mode,
M_matvec, Minv_matvec, sigma,
ncv, v0, maxiter, which, tol)

  1. i=0
  2. while not params.converged:
  3. params.iterate()
  4. i=i+1
  5. return i,params.extract(return_eigenvectors)

###Make Herimtian Matrix
d = 50
mat = np.random.rand(d,d)+ 1j * (np.random.rand(d,d))
mat = mat - (np.random.rand(d,d)+ 1j * (np.random.rand(d,d)))
mat = mat + ct(mat)

i,(vals, vecs) = eigsb(mat, k=1, which='SR', ncv=6, maxiter=50)

  1. Is there a different way I should be going about this? Or is there some conversion between the count *i* and the true iteration value I&#39;m not seeing?
  2. </details>
  3. # 答案1
  4. **得分**: 1
  5. tol在这里起到了作用。我尝试将tol设置为非零正值,无论dmaxiter的值如何,它都会迭代相同的次数。尝试将tol设置为非零正值并检查。
  6. 参考链接:
  7. https://sisl.readthedocs.io/en/v0.9.2/_modules/scipy/sparse/linalg/eigen/arpack/arpack.html
  8. ```python
  9. class IterInv(LinearOperator):
  10. """
  11. IterInv:
  12. 辅助类,使用迭代方法重复解决M*x=b。
  13. """
  14. def __init__(self, M, ifunc=gmres, tol=0):
  15. if tol <= 0:
  16. # 当tol=0时,ARPACK使用由LAPACK的_LAMCH函数计算的机器精度。
  17. # 我们应该匹配这个值。
  18. tol = 2 * np.finfo(M.dtype).eps
  19. tol: float,可选
  20. 特征值的相对精度(停止准则)
  21. 默认值0表示机器精度。
英文:

It appears to me that tol is playing a role here. I tried setting non-zero positive value for tol and it iterates exactly same number of times regardless of value of d or maxiter. Try setting tol as non-zero positive and check.

Reference :
https://sisl.readthedocs.io/en/v0.9.2/_modules/scipy/sparse/linalg/eigen/arpack/arpack.html

  1. class IterInv(LinearOperator):
  2. &quot;&quot;&quot;
  3. IterInv:
  4. helper class to repeatedly solve M*x=b
  5. using an iterative method.
  6. &quot;&quot;&quot;
  7. def __init__(self, M, ifunc=gmres, tol=0):
  8. if tol &lt;= 0:
  9. # when tol=0, ARPACK uses machine tolerance as calculated
  10. # by LAPACK&#39;s _LAMCH function. We should match this
  11. tol = 2 * np.finfo(M.dtype).eps
  12. tol : float, optional
  13. Relative accuracy for eigenvalues (stopping criterion)
  14. The default value of 0 implies machine precision.

huangapple
  • 本文由 发表于 2023年3月9日 13:34:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75680773.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定