如何在Scipy中实施ILU先导者? [英] How to implement ILU precondioner in scipy?
问题描述
对于scipy.sparse.linalg
中的迭代求解器,例如bicg
,gmres
等,可以选择为矩阵A
添加前置条件.但是,文档不是我很清楚我应该作为先决条件.如果我使用ilu = sp.sparse.linalg.spilu(A)
,则ilu
并不是任何矩阵,而是一个包含很多东西的对象.
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.
有人在此处询问了类似的问题,但我没有为我工作(Python 3.7,scipy版本1.1.0)
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)
所以我的问题是如何为这些迭代算法合并不完整的LU预条件器?
So my question is how to incorporate incomplete LU preconditioner for those iterative algorithms?
推荐答案
作为前提条件, gmres 接受
- 稀疏矩阵
- 致密矩阵
- 线性算子
在您的情况下,预处理器来自因式分解,因此必须作为线性运算符传递.
In your case, the preconditioner comes from a factorization, thus it has to be passed as a linear operator.
因此,您可能需要根据通过spilu
获得的ILU因式分解显式定义一个线性算子.大致情况:
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)
在这里,sA
是CSC格式的稀疏矩阵,并且M
现在将是要提供给迭代求解器的前置条件线性运算符.
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)
注意:
- 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.
这篇关于如何在Scipy中实施ILU先导者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!