matrixfree_cg.cpp File Reference

Go to the source code of this file.

Namespaces

 Eigen
 : InteropHeaders
 
 Eigen::internal
 

Functions

int main ()
 

Function Documentation

◆ main()

int main ( )

Definition at line 82 of file matrixfree_cg.cpp.

83 {
84  int n = 10;
86  S = S.transpose()*S;
87 
88  MatrixReplacement A;
89  A.attachMyMatrix(S);
90 
91  Eigen::VectorXd b(n), x;
92  b.setRandom();
93 
94  // Solve Ax = b using various iterative solver with matrix-free version:
95  {
97  cg.compute(A);
98  x = cg.solve(b);
99  std::cout << "CG: #iterations: " << cg.iterations() << ", estimated error: " << cg.error() << std::endl;
100  }
101 
102  {
104  bicg.compute(A);
105  x = bicg.solve(b);
106  std::cout << "BiCGSTAB: #iterations: " << bicg.iterations() << ", estimated error: " << bicg.error() << std::endl;
107  }
108 
109  {
110  Eigen::GMRES<MatrixReplacement, Eigen::IdentityPreconditioner> gmres;
111  gmres.compute(A);
112  x = gmres.solve(b);
113  std::cout << "GMRES: #iterations: " << gmres.iterations() << ", estimated error: " << gmres.error() << std::endl;
114  }
115 
116  {
117  Eigen::DGMRES<MatrixReplacement, Eigen::IdentityPreconditioner> gmres;
118  gmres.compute(A);
119  x = gmres.solve(b);
120  std::cout << "DGMRES: #iterations: " << gmres.iterations() << ", estimated error: " << gmres.error() << std::endl;
121  }
122 
123  {
124  Eigen::MINRES<MatrixReplacement, Eigen::Lower|Eigen::Upper, Eigen::IdentityPreconditioner> minres;
125  minres.compute(A);
126  x = minres.solve(b);
127  std::cout << "MINRES: #iterations: " << minres.iterations() << ", estimated error: " << minres.error() << std::endl;
128  }
129 }
Array< int, 3, 1 > b
int n
MatrixXcf A
A bi conjugate gradient stabilized solver for sparse square problems.
Definition: BiCGSTAB.h:161
A conjugate gradient solver for sparse (or dense) self-adjoint problems.
static const RandomReturnType Random()
Definition: Random.h:114
Derived & compute(const EigenBase< MatrixDerived > &A)
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:182
TransposeReturnType transpose()
const Solve< Derived, Rhs > solve(const MatrixBase< Rhs > &b) const