Eigen学习笔记26:求解稀疏线性矩阵

本文介绍Eigen库中用于求解线性系统的多种稀疏矩阵求解器,包括直接求解器和迭代求解器。详细列出内置求解器及外部求解器库的特性,如求解器类型、适用矩阵种类、性能相关特性及许可信息。同时,提供了求解步骤的说明和性能评估的基准程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Eigen中,当系数矩阵是稀疏矩阵的时候,存在这几种方法用于求解线性系统。

Because of the special representation of this class of matrices, special care should be taken in order to get a good performance.

有关Eigen中稀疏矩阵的详细介绍,请参见稀疏矩阵操作

此页面列出了Eigen中可用的稀疏求解器。还介绍了所有这些线性求解器共有的主要步骤。

Depending on the properties of the matrix, the desired accuracy, the end-user is able to tune those steps in order to improve the performance of its code.

请注意,不需要深入了解这些步骤背后的内容: the last section presents a benchmark routine that can be easily used to get an insight on the performance of all the available solvers.

稀疏求解器列表

Eigen当前提供了应用很广泛的内置求解器,以及外部求解器库的包装器。下表中汇总了它们: They are summarized in the following tables:

内置直接求解器

求解器类型Matrix kindFeatures related to performanceLicense

注意:

SimplicialLLT
#include<Eigen/SparseCholesky>
Direct LLt factorizationSPDFill-in reducingLGPL

通常最好使用SimplicialLDLT

SimplicialLDLT
#include<Eigen/SparseCholesky>
Direct LDLt factorizationSPDFill-in reducingLGPL

建议用于非常稀疏且不太大的问题(例如2D泊松方程)

SparseLU
#include<Eigen/SparseLU>
LU factorizationSquareFill-in reducing, Leverage fast dense algebraMPL2

optimized for small and large problems with irregular patterns

SparseQR
#include<Eigen/SparseQR>
QR factorizationAny, rectangularFill-in reducingMPL2r建议用于最小二乘问题, has a basic rank-revealing feature

内置迭代求解器

Class求解器类型Matrix kindSupported preconditioners, [default]License

注意:

ConjugateGradient
#include<Eigen/IterativeLinearSolvers>
Classic iterative CGSPDIdentityPreconditioner, [DiagonalPreconditioner], IncompleteCholeskyMPL2

推荐用于大型对称问题(例如3D泊松方程)

LeastSquaresConjugateGradient
#include<Eigen/IterativeLinearSolvers>
CG for rectangular least-square problemRectangularIdentityPreconditioner, [LeastSquareDiagonalPreconditioner]MPL2

Solve for min |A'Ax-b|^2 without forming A'A

 

BiCGSTAB
#include<Eigen/IterativeLinearSolvers>
Iterative stabilized bi-conjugate gradientSquareIdentityPreconditioner, [DiagonalPreconditioner], IncompleteLUTMPL2加速收敛,  try it with the IncompleteLUT preconditioner.

外部求解器的包装器

模块求解器类型Matrix与性能相关的特征Dependencies,License

注意

PastixLLT
PastixLDLT
PastixLU
PaStiXSupportDirect LLt, LDLt, LU factorizationsSPD
SPD
Square
Fill-in reducing, Leverage fast dense algebra, MultithreadingRequires the PaStiX package, CeCILL-C针对棘手的问题和对称模式进行了优化
CholmodSupernodalLLTCholmodSupportDirect LLt factorizationSPDFill-in reducing, Leverage fast dense algebraRequires the SuiteSparse package, GPL 
UmfPackLUUmfPackSupportDirect LU factorizationSquareFill-in reducing, Leverage fast dense algebraRequires the SuiteSparse package, GPL 
SuperLUSuperLUSupportDirect LU factorizationSquareFill-in reducing, Leverage fast dense algebraRequires the SuperLU library, (BSD-like) 
SPQRSPQRSupportQR factorizationAny, rectangularfill-in reducing, multithreaded, fast dense algebrarequires the SuiteSparse package, GPL推荐用于线性最小二乘法问题, has a rank-revealing feature
PardisoLLT
PardisoLDLT
PardisoLU
PardisoSupportDirect LLt, LDLt, LU factorizationsSPD
SPD
Square
Fill-in reducing, Leverage fast dense algebra, MultithreadingRequires the Intel MKL package, Proprietaryoptimized for tough problems patterns, see also using MKL with Eigen

此处SPD表示对称正定。

稀疏求解器概念

#include <Eigen/RequiredModuleName>
// ...
SparseMatrix<double> A;
// fill A
VectorXd b, x;
// fill b
// solve Ax = b
SolverClassName<SparseMatrix<double> > solver;
solver.compute(A);
if(solver.info()!=Success) {
  // decomposition failed
  return;
}
x = solver.solve(b);
if(solver.info()!=Success) {
  // solving failed
  return;
}
// solve for another right hand side:
x1 = solver.solve(b1);

对于SPD求解器,第二个可选模板参数允许指定必须使用哪个三角形部分,例如:

#include <Eigen/IterativeLinearSolvers>
 
ConjugateGradient<SparseMatrix<double>, Eigen::Upper> solver;
x = solver.compute(A).solve(b);
 

在以上示例中,仅考虑输入矩阵A的上三角部分进行求解。The opposite triangle might either be empty or contain arbitrary values.

In the case where multiple problems with the same sparsity pattern have to be solved, then the "compute" step can be decomposed as follow:

SolverClassName<SparseMatrix<double> > solver;
solver.analyzePattern(A);   // for this step the numerical values of A are not used
solver.factorize(A);
x1 = solver.solve(b1);
x2 = solver.solve(b2);
...
A = ...;                    // modify the values of the nonzeros of A, the nonzeros pattern must stay unchanged
solver.factorize(A);
x1 = solver.solve(b1);
x2 = solver.solve(b2);
...
The compute() method is equivalent to calling both analyzePattern() and factorize().

每个求解器都提供一些特定功能,例如行列式,对因子的访问,迭代控制等。有关更多详细信息,请参见相应类的文档。

Finally, most of the iterative solvers, can also be used in a matrix-free context, see the following example .

计算步骤

在compute()函数中,通常对矩阵进行分解:LLT用于自伴矩阵,LDLT用于hermitian matrices,LU用于non hermitian matrices,QR用于rectangular matrices。这些是使用直接求解器的结果。对于这类求解器,计算步骤可进一步细分为analyzerPattern()和factorize()。

analyticsPattern()的目标是对矩阵的非零元素进行重新排序,以使the factorization step creates less fill-in此步骤仅利用矩阵的结构。Hence, the results of this step can be used for other linear systems where the matrix has the same structure.但是请注意,有时某些外部求解器(例如SuperLU)要求在此步骤中设置矩阵的值,例如,以平衡矩阵的行和列。在这种情况下,此步骤的结果不应与其他矩阵一起使用。

Eigen提供几种有限的方法来在此步骤中对矩阵进行重新排序,方法是内置的(COLAMD,AMD)或外部的(METIS)。这些方法在求解器的模板参数列表中设置:

DirectSolverClassName<SparseMatrix<double>, OrderingMethod<IndexType> > solver;
See the  OrderingMethods module for the list of available methods and the associated options.

In factorize(), the factors of the coefficient matrix are computed. 每当矩阵值更改时,都应调用此步骤。However, the structural pattern of the matrix should not change between multiple calls.

对于迭代求解器,计算步骤用于最终设置预处理器。 例如,使用ILUT预处理器,在此步骤中计算不完全因子L和U。请记住,基本上, the goal of the preconditioner is to speedup the convergence of an iterative method by solving a modified linear system where the coefficient matrix has more clustered eigenvalues. 对于实际问题,迭代求解器应始终与前置条件一起使用。在Eigen中,只需将预处理器作为模板参数添加到迭代求解器对象中,即可选择该预处理器。

IterativeSolverClassName<SparseMatrix<double>, PreconditionerName<SparseMatrix<double> > solver; 
The member function preconditioner() returns a read-write reference to the preconditioner to directly interact with it.
  有关可用方法的列表,请参见迭代求解器模块和每个类的文档。

求解步骤

solve()函数计算具有一个或多个右侧的线性系统的解。

X = solver.solve(B);
在此,B可以是向量或矩阵,其中各列形成不同的右侧。 The solve() function can be called several times as well, for instance when all the right hand sides are not available at once.
x1 = solver.solve(b1);
// Get the second right hand side b2
x2 = solver.solve(b2); 
//  ...
对于直接方法,解决方案以机器精度计算。有时,解决方案不必太精确。在这种情况下,迭代方法更合适,并且可以在使用setTolerance()的求解步骤之前设置所需的精度。有关所有可用功能,请参阅“ 迭代求解器”模块的文档。

基准程序

在大多数情况下,您所需要做的就是了解解决系统所需的时间,并希望有什么是最合适的解决器。在Eigen中,我们提供了可用于此目的的基准例程。It is very easy to use. In the build directory, navigate to bench/spbench and compile the routine by typing make spbenchsolver. 使用–help选项运行它以获取所有可用选项的列表。Basically, the matrices to test should be in MatrixMarket Coordinate format, and the routine returns the statistics from all available solvers in Eigen.

要以矩阵市场格式导出矩阵和右侧矢量,可以使用不受支持的SparseExtra模块:

#include <unsupported/Eigen/SparseExtra>
...
Eigen::saveMarket(A, "filename.mtx");
Eigen::saveMarket(A, "filename_SPD.mtx", Eigen::Symmetric); // if A is symmetric-positive-definite
Eigen::saveMarketVector(B, "filename_b.mtx");
下表提供了一些Eigen内置和外部求解器的XML统计信息示例。
 
MatrixNNNZ UMFPACKSUPERLUPASTIX LUBiCGSTABBiCGSTAB+ILUTGMRES+ILUTLDLTCHOLMOD LDLTPASTIX LDLTLLTCHOLMOD SP LLTCHOLMOD LLTPASTIX LLTCG
vector_graphics1285572069Compute Time0.02545490.02156770.07018270.0001533880.01401070.01537090.01016010.009305020.0649689
Solve Time0.003378350.0009518260.004843730.03748860.00464450.008477540.0005418130.0002936960.00485376
Total Time0.02883330.02251950.07502650.0376420.01865520.02384840.01070190.009598710.0698227
Error(Iter)1.299e-162.04207e-164.83393e-153.94856e-11 (80)1.03861e-12 (3)5.81088e-14 (6)1.97578e-161.83927e-164.24115e-15
poisson_SPD19788308232Compute Time0.4250261.823780.6173670.0004789211.340011.334710.7964190.8575730.4730070.8148260.1847190.8615550.4705590.000458188
Solve Time0.02800530.01944020.02687470.2494370.05484440.09269910.008502040.00531710.02589320.008746030.005781550.005303610.02489420.239093
Total Time0.4530311.843220.6442410.2499161.394861.427410.8049210.8628910.49890.8235720.1905010.8668590.4954530.239551
Error(Iter)4.67146e-161.068e-151.3397e-156.29233e-11 (201)3.68527e-11 (6)3.3168e-15 (16)1.86376e-151.31518e-161.42593e-153.45361e-153.14575e-162.21723e-157.21058e-169.06435e-12 (261)
sherman2108023094Compute Time0.006317540.0150520.0247514-0.02144250.0217988
Solve Time0.0004784240.0003379980.0010291-0.002431520.00246152
Total Time0.006795970.015390.0257805-0.0238740.0242603
Error(Iter)1.83099e-158.19351e-152.625e-141.3678e+69 (1080)4.1911e-12 (7)5.0299e-13 (12)
bcsstk01_SPD48400Compute Time0.0001690790.000107890.0005725381.425e-069.1612e-058.3985e-055.6489e-057.0913e-050.0004682515.7389e-058.0212e-055.8394e-050.0004630171.333e-06
Solve Time1.2288e-051.1124e-050.0002863878.5896e-051.6381e-051.6984e-053.095e-064.115e-060.0003254383.504e-067.369e-063.454e-060.0002940956.0516e-05
Total Time0.0001813670.0001190140.0008589258.7321e-050.0001079930.0001009695.9584e-057.5028e-050.0007936896.0893e-058.7581e-056.1848e-050.0007571126.1849e-05
Error(Iter)1.03474e-162.23046e-162.01273e-164.87455e-07 (48)1.03553e-16 (2)3.55965e-16 (2)2.48189e-161.88808e-161.97976e-162.37248e-161.82701e-162.71474e-162.11322e-163.547e-09 (48)
sherman110003750Compute Time0.002288050.002092310.005282689.846e-060.001635220.001621550.0007892590.0008044950.00438269
Solve Time0.0002137889.7983e-050.0009388310.006298350.0003617640.000787944.3989e-052.5331e-050.000917166
Total Time0.002501840.002190290.006221510.00630820.001996980.002409490.0008332480.0008298260.00529986
Error(Iter)1.16839e-162.25968e-162.59116e-163.76779e-11 (248)4.13343e-11 (4)2.22347e-14 (10)2.05861e-161.83555e-161.02917e-15
young1c8414089Compute Time0.002358430.002172280.005680751.2735e-050.002648660.00258236
Solve Time0.0003295990.0001686340.000801180.05347380.001871930.00450211
Total Time0.002688030.002340910.006481930.05348650.004520590.00708447
Error(Iter)1.27029e-162.81321e-165.0492e-158.0507e-11 (706)3.00447e-12 (8)1.46532e-12 (16)
mhd1280b128022778Compute Time0.002348980.002070790.005709182.5976e-050.003025630.002980360.001445250.0009199220.00426444
Solve Time0.001033920.0002119110.001050.01104320.0006282870.003920890.0001383036.2446e-050.00097564
Total Time0.00338290.00228270.006759180.01106920.003653920.006901240.001583550.0009823680.00524008
Error(Iter)1.32953e-163.08646e-166.734e-168.83132e-11 (40)1.51153e-16 (1)6.08556e-16 (8)1.89264e-161.97477e-166.68126e-09
crashbasis1600001750416Compute Time3.20195.789215.75730.003835153.10063.09921
Solve Time0.2619150.1062250.4021411.490890.248880.443673
Total Time3.463815.8954216.15941.494733.349483.54288
Error(Iter)1.76348e-164.58395e-161.67982e-148.64144e-11 (61)8.5996e-12 (2)

6.04042e-14 (5)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值