How to implement ILU precondioner in scipy?

强颜欢笑 提交于 2020-04-30 06:58:05

问题


For the iterative solvers in the scipy.sparse.linalg such as bicg, gmres, etc, there is an option to add the precondioner for the matrix A. However, the documentation is not very clear about what I should give as the preconditioner. If I use ilu = sp.sparse.linalg.spilu(A), ilu is not any matrices but an object that encompasses many things.

Someone asked about a similar question here for Python 2.7, but I doesn't work for me (Python 3.7, scipy version 1.1.0)

So my question is how to incorporate incomplete LU preconditioner for those iterative algorithms?


回答1:


As a preconditioner, bicg or gmres accept

  • sparse matrix
  • dense matrix
  • linear operator

In your case, the preconditioner comes from a factorization, thus it has to be passed as a linear operator.

Thus, you may want to explicitly define a linear operator from the ILU factorization you obtained via spilu. Something along the lines:

sA_iLU = sparse.linalg.spilu(sA)
M = sparse.linalg.LinearOperator((nrows,ncols), sA_iLU.solve)

Here, sA is a sparse matrix in a CSC format, and M will now be the preconditioner linear operator that you will supply to the iterative solver.


A complete example based on the question you mentioned:

import numpy as np
from scipy import sparse
from scipy.sparse import linalg

A = np.array([[ 0.4445,  0.4444, -0.2222],
              [ 0.4444,  0.4445, -0.2222],
              [-0.2222, -0.2222,  0.1112]])
sA = sparse.csc_matrix(A)

b = np.array([[ 0.6667], 
              [ 0.6667], 
              [-0.3332]])

sA_iLU = sparse.linalg.spilu(sA)
M = sparse.linalg.LinearOperator((3,3), sA_iLU.solve)

x = sparse.linalg.gmres(A,b,M=M)

print(x)

Notes:

  • I am actually using a dense matrix as an example, while it would make more sense to start from a representative sparse matrix in your case.
  • size of the linear operator M is hardcoded.
  • ILU has not been configured anyhow, but with the defaults.
  • this is pretty much what has been suggested in the comments to that aforementioned answer, however, I had to do small tweaks to make it Python3-compatible.


来源:https://stackoverflow.com/questions/58895934/how-to-implement-ilu-precondioner-in-scipy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!