简体   繁体   English

共轭梯度中使用的Scipy线性代数LinearOperator函数

[英]Scipy Linear algebra LinearOperator function utilised in Conjugate Gradient

I am preconditioning a matrix using spilu, however, to pass this preconditioner into cg (the built in conjugate gradient method) it is necessary to use the LinearOperator function, can someone explain to me the parameter matvec, and why I need to use it. 我正在使用spilu预处理矩阵,但是,要将此预处理器传递到cg(内置共轭梯度方法)中,必须使用LinearOperator函数,有人可以向我解释参数matvec以及为什么需要使用它。 Below is my current code 下面是我当前的代码

Ainv=scla.spilu(A,drop_tol= 1e-7)
Ainv=scla.LinearOperator(Ainv.shape,matvec=Ainv)
scla.cg(A,b,maxiter=maxIterations, M = Ainv)

However this doesnt work and I am given the error TypeError: 'SuperLU' object is not callable. 但是,这不起作用,并且给我错误TypeError:'SuperLU'对象不可调用。 I have played around and tried 我玩了,尝试

Ainv=scla.LinearOperator(Ainv.shape,matvec=Ainv.solve)

instead. 代替。 This seems to work but I want to know why matvec needs Ainv.solve rather than just Ainv, and is it the right thing to feed LinearOperator? 这似乎可行,但是我想知道为什么matvec需要Ainv.solve而不是仅Ainv,并且喂LinearOperator是正确的做法吗?

Thanks for your time 谢谢你的时间

Without having much experience with this part of scipy, some comments: 在没有这部分scipy经验的情况下,一些评论:

  • According to the docs you don't have to use LinearOperator , but you might do 根据文档,您不必使用LinearOperator ,但是您可以使用
    • M : {sparse matrix, dense matrix, LinearOperator} , so you can use explicit matrices too! M : {sparse matrix, dense matrix, LinearOperator} ,因此您也可以使用显式矩阵!
    • The idea/advantage of the LinearOperator : LinearOperator的想法/优点:
      • Many iterative methods (eg cg, gmres) do not need to know the individual entries of a matrix to solve a linear system A*x=b. Such solvers only require the computation of matrix vector products Many iterative methods (eg cg, gmres) do not need to know the individual entries of a matrix to solve a linear system A*x=b. Such solvers only require the computation of matrix vector products docs Many iterative methods (eg cg, gmres) do not need to know the individual entries of a matrix to solve a linear system A*x=b. Such solvers only require the computation of matrix vector products docs
        • Depending on the task, sometimes even matrix-free approaches are available which can be much more efficient 根据任务的不同,有时甚至可以使用无矩阵的方法,这可能会更加高效
  • The working approach you presented is indeed the correct one ( some other source doing it similarily , and some course-materials doing it like that ) 您提出的工作方法确实是正确的方法( 其他类似的来源 ,某些课程材料也是如此
    • The idea of not using the inverse matrix, but using solve() here is not to form the inverse explicitly (which might be very costly) 不使用逆矩阵,而是 在此处 使用 solve() 的想法不是 显式地形成 逆矩阵 (这可能会非常昂贵)
      • A similar idea is very common in BFGS-based optimization algorithms although wiki might not give much insight here 基于BFGS的优化算法中,类似的想法非常普遍,尽管Wiki在这里可能无法提供太多见解
        • scipy has an extra LinearOperator for this not forming the inverse explicitly! scipy有一个额外的LinearOperator,因此不会显式地形成逆函数! (although i think it's only used for statistics / completing/finishing some optimization; but i successfully build some LBFGS-based optimizers with this one) (尽管我认为它仅用于统计/完成/完成一些优化;但是我使用此模型成功构建了一些基于LBFGS的优化器)
      • Source @ scicomp.stackexchange discussing this without touching scipy 来源@ scicomp.stackexchange在不接触scipy的情况下进行讨论
      • And because of that i would assume spilu is completely going for this too (returning an object with a solve-method) 因此,我认为spilu也完全可以做到这一点(使用Solved方法返回一个对象)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM