2017-10-05 142 views
1

我使用spilu預處理矩陣,但是,要將此預處理器傳入cg(內置共軛梯度法),需要使用LinearOperator函數,有人可以向我解釋參數matvec,以及爲什麼我需要使用它。下面是我現在的代碼Scipy線性代數LinearOperator函數用於共軛梯度

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

但是這不起作用,我得到錯誤TypeError:'SuperLU'對象不可調用。我玩過並試過

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

改爲。這似乎工作,但我想知道爲什麼matvec需要Ainv.solve而不是僅僅Ainv,並且餵給LinearOperator是否正確?

感謝您的時間

回答

0

,而不必與SciPy的這一部分很多經驗,一些意見:

  • 按照docs你沒有使用LinearOperator,但你可能會做
    • M : {sparse matrix, dense matrix, LinearOperator},所以你也可以使用顯式矩陣!
    • LinearOperator的想法/優勢:
      • Many iterative methods (e.g. 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 productsdocs
        • 根據不同的任務,有時甚至是免費的基質可用的方法可以是更有效的
  • The 工作方式您呈現的確是正確的(some other source doing it similarily,有的course-materials doing it like that
    • 不使用逆矩陣的概念,但使用solve()這裏沒有明確地形成逆(這可能是非常昂貴的)
      • 類似的想法是在BFGS-based優化算法很常見的,雖然維基可能不會在這裏給出很大的啓示
      • Source @ scicomp.stackexchange討論這個不碰SciPy的
      • 而且因爲我會假定spilu完全此過猶(返回一個解決法的對象)