Scipy共轭梯度中使用的线性代数LinearOperator函数
Scipy Linear algebra LinearOperator function utilised in Conjugate Gradient
我正在使用 spilu 对矩阵进行预处理,但是,要将此预条件子传递给 cg(内置共轭梯度法),必须使用 LinearOperator 函数,有人可以向我解释参数 matvec,以及为什么我需要使用它。下面是我当前的代码
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' object is not callable。我玩过并尝试过
Ainv=scla.LinearOperator(Ainv.shape,matvec=Ainv.solve)
相反。这似乎行得通,但我想知道为什么 matvec 需要 Ainv.solve 而不仅仅是 Ainv,并且提供 LinearOperator 是否正确?
感谢您的宝贵时间
由于对scipy的这一部分没有太多经验,一些评论:
- 根据 docs,您不必使用
LinearOperator
,但您可能会这样做
M : {sparse matrix, dense matrix, LinearOperator}
,所以你也可以使用显式矩阵!
LinearOperator
的idea/advantage:
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 products
docs
- 根据任务的不同,有时甚至可以使用更高效的无矩阵方法
- 您介绍的工作方法确实是正确的(some other source doing it similarily, and some course-materials doing it like that)
- 不使用逆矩阵的想法,而是使用
solve()
这里不是为了显式地形成逆矩阵(这可能会非常昂贵)
- 类似的想法在 BFGS-based 优化算法中很常见,尽管 wiki 在这里可能不会提供太多见解
- scipy has an extra LinearOperator for this 没有明确地形成逆! (虽然我认为它只用于统计/completing/finishing一些优化;但我用这个成功地构建了一些基于 LBFGS 的优化器)
- Source @ scicomp.stackexchange 在不触碰的情况下讨论这个 scipy
- 因此,我认为 spilu 也完全适用于此(返回具有求解方法的对象)
我正在使用 spilu 对矩阵进行预处理,但是,要将此预条件子传递给 cg(内置共轭梯度法),必须使用 LinearOperator 函数,有人可以向我解释参数 matvec,以及为什么我需要使用它。下面是我当前的代码
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' object is not callable。我玩过并尝试过
Ainv=scla.LinearOperator(Ainv.shape,matvec=Ainv.solve)
相反。这似乎行得通,但我想知道为什么 matvec 需要 Ainv.solve 而不仅仅是 Ainv,并且提供 LinearOperator 是否正确?
感谢您的宝贵时间
由于对scipy的这一部分没有太多经验,一些评论:
- 根据 docs,您不必使用
LinearOperator
,但您可能会这样做M : {sparse matrix, dense matrix, LinearOperator}
,所以你也可以使用显式矩阵!LinearOperator
的idea/advantage: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 products
docs- 根据任务的不同,有时甚至可以使用更高效的无矩阵方法
- 您介绍的工作方法确实是正确的(some other source doing it similarily, and some course-materials doing it like that)
- 不使用逆矩阵的想法,而是使用
solve()
这里不是为了显式地形成逆矩阵(这可能会非常昂贵)- 类似的想法在 BFGS-based 优化算法中很常见,尽管 wiki 在这里可能不会提供太多见解
- scipy has an extra LinearOperator for this 没有明确地形成逆! (虽然我认为它只用于统计/completing/finishing一些优化;但我用这个成功地构建了一些基于 LBFGS 的优化器)
- Source @ scicomp.stackexchange 在不触碰的情况下讨论这个 scipy
- 因此,我认为 spilu 也完全适用于此(返回具有求解方法的对象)
- 类似的想法在 BFGS-based 优化算法中很常见,尽管 wiki 在这里可能不会提供太多见解
- 不使用逆矩阵的想法,而是使用