matrixfree_cg.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <Eigen/Core>
3 #include <Eigen/Dense>
4 #include <Eigen/IterativeLinearSolvers>
5 #include <unsupported/Eigen/IterativeSolvers>
6 
9 
10 namespace Eigen {
11 namespace internal {
12  // MatrixReplacement looks-like a SparseMatrix, so let's inherits its traits:
13  template<>
14  struct traits<MatrixReplacement> : public Eigen::internal::traits<Eigen::SparseMatrix<double> >
15  {};
16 }
17 }
18 
19 // Example of a matrix-free wrapper from a user type to Eigen's compatible type
20 // For the sake of simplicity, this example simply wrap a Eigen::SparseMatrix.
21 class MatrixReplacement : public Eigen::EigenBase<MatrixReplacement> {
22 public:
23  // Required typedefs, constants, and method:
24  typedef double Scalar;
25  typedef double RealScalar;
26  typedef int StorageIndex;
27  enum {
30  IsRowMajor = false
31  };
32 
33  Index rows() const { return mp_mat->rows(); }
34  Index cols() const { return mp_mat->cols(); }
35 
36  template<typename Rhs>
39  }
40 
41  // Custom API:
43 
45  mp_mat = &mat;
46  }
47  const SparseMatrix<double> my_matrix() const { return *mp_mat; }
48 
49 private:
51 };
52 
53 
54 // Implementation of MatrixReplacement * Eigen::DenseVector though a specialization of internal::generic_product_impl:
55 namespace Eigen {
56 namespace internal {
57 
58  template<typename Rhs>
59  struct generic_product_impl<MatrixReplacement, Rhs, SparseShape, DenseShape, GemvProduct> // GEMV stands for matrix-vector
60  : generic_product_impl_base<MatrixReplacement,Rhs,generic_product_impl<MatrixReplacement,Rhs> >
61  {
63 
64  template<typename Dest>
65  static void scaleAndAddTo(Dest& dst, const MatrixReplacement& lhs, const Rhs& rhs, const Scalar& alpha)
66  {
67  // This method should implement "dst += alpha * lhs * rhs" inplace,
68  // however, for iterative solvers, alpha is always equal to 1, so let's not bother about it.
69  assert(alpha==Scalar(1) && "scaling is not implemented");
71 
72  // Here we could simply call dst.noalias() += lhs.my_matrix() * rhs,
73  // but let's do something fancier (and less efficient):
74  for(Index i=0; i<lhs.cols(); ++i)
75  dst += rhs(i) * lhs.my_matrix().col(i);
76  }
77  };
78 
79 }
80 }
81 
82 int main()
83 {
84  int n = 10;
85  Eigen::SparseMatrix<double> S = Eigen::MatrixXd::Random(n,n).sparseView(0.5,1);
86  S = S.transpose()*S;
87 
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  {
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  {
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  {
125  minres.compute(A);
126  x = minres.solve(b);
127  std::cout << "MINRES: #iterations: " << minres.iterations() << ", estimated error: " << minres.error() << std::endl;
128  }
129 }
Eigen::SparseMatrix::cols
Index cols() const
Definition: SparseMatrix.h:140
Eigen::GMRES
A GMRES solver for sparse square problems.
Definition: GMRES.h:221
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Eigen::SparseMatrix
A versatible sparse matrix representation.
Definition: SparseMatrix.h:96
Eigen::DGMRES
A Restarted GMRES with deflation. This class implements a modification of the GMRES solver for sparse...
Definition: DGMRES.h:19
Eigen::GemvProduct
@ GemvProduct
Definition: Constants.h:500
alpha
RealScalar alpha
Definition: level1_cplx_impl.h:147
Eigen::EigenBase< MatrixReplacement >::Index
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:39
Eigen::DenseShape
Definition: Constants.h:528
MatrixReplacement::mp_mat
const SparseMatrix< double > * mp_mat
Definition: matrixfree_cg.cpp:50
b
Scalar * b
Definition: benchVecAdd.cpp:17
Eigen::EigenBase
Definition: EigenBase.h:29
Eigen::internal::generic_product_impl< MatrixReplacement, Rhs, SparseShape, DenseShape, GemvProduct >::scaleAndAddTo
static void scaleAndAddTo(Dest &dst, const MatrixReplacement &lhs, const Rhs &rhs, const Scalar &alpha)
Definition: matrixfree_cg.cpp:65
x
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition: gnuplot_common_settings.hh:12
MatrixReplacement::RealScalar
double RealScalar
Definition: matrixfree_cg.cpp:25
MatrixReplacement::Scalar
double Scalar
Definition: matrixfree_cg.cpp:24
Eigen::internal::generic_product_impl_base
Definition: ProductEvaluators.h:343
MatrixReplacement::MatrixReplacement
MatrixReplacement()
Definition: matrixfree_cg.cpp:42
mat
MatrixXf mat
Definition: Tutorial_AdvancedInitialization_CommaTemporary.cpp:1
EIGEN_ONLY_USED_FOR_DEBUG
#define EIGEN_ONLY_USED_FOR_DEBUG(x)
Definition: Macros.h:1049
A
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:48
n
int n
Definition: BiCGSTAB_simple.cpp:1
Eigen::internal::generic_product_impl
Definition: ProductEvaluators.h:86
MatrixReplacement::cols
Index cols() const
Definition: matrixfree_cg.cpp:34
MatrixReplacement::StorageIndex
int StorageIndex
Definition: matrixfree_cg.cpp:26
MatrixReplacement::attachMyMatrix
void attachMyMatrix(const SparseMatrix< double > &mat)
Definition: matrixfree_cg.cpp:44
A
Definition: test_numpy_dtypes.cpp:298
Eigen::MINRES
A minimal residual solver for sparse symmetric problems.
Definition: MINRES.h:143
Eigen::Dynamic
const int Dynamic
Definition: Constants.h:22
Eigen::SparseShape
Definition: Constants.h:537
MatrixReplacement::MaxColsAtCompileTime
@ MaxColsAtCompileTime
Definition: matrixfree_cg.cpp:29
Eigen::ConjugateGradient
A conjugate gradient solver for sparse (or dense) self-adjoint problems.
Definition: ConjugateGradient.h:97
Eigen::Product
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:71
Eigen::internal::gmres
bool gmres(const MatrixType &mat, const Rhs &rhs, Dest &x, const Preconditioner &precond, Index &iters, const Index &restart, typename Dest::RealScalar &tol_error)
Definition: GMRES.h:56
main
int main()
Definition: matrixfree_cg.cpp:82
Eigen::BiCGSTAB
A bi conjugate gradient stabilized solver for sparse square problems.
Definition: BiCGSTAB.h:113
Eigen::internal::traits
Definition: ForwardDeclarations.h:17
MatrixReplacement::ColsAtCompileTime
@ ColsAtCompileTime
Definition: matrixfree_cg.cpp:28
Eigen::internal::Rhs
@ Rhs
Definition: TensorContractionMapper.h:18
Eigen::internal::minres
EIGEN_DONT_INLINE void minres(const MatrixType &mat, const Rhs &rhs, Dest &x, const Preconditioner &precond, Index &iters, typename Dest::RealScalar &tol_error)
Definition: MINRES.h:32
Eigen::internal::generic_product_impl< MatrixReplacement, Rhs, SparseShape, DenseShape, GemvProduct >::Scalar
Product< MatrixReplacement, Rhs >::Scalar Scalar
Definition: matrixfree_cg.cpp:62
MatrixReplacement::my_matrix
const SparseMatrix< double > my_matrix() const
Definition: matrixfree_cg.cpp:47
MatrixReplacement::operator*
Eigen::Product< MatrixReplacement, Rhs, Eigen::AliasFreeProduct > operator*(const Eigen::MatrixBase< Rhs > &x) const
Definition: matrixfree_cg.cpp:37
MatrixReplacement::IsRowMajor
@ IsRowMajor
Definition: matrixfree_cg.cpp:30
internal
Definition: BandTriangularSolver.h:13
Eigen::MatrixBase
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
Eigen::SparseMatrix::rows
Index rows() const
Definition: SparseMatrix.h:138
MatrixReplacement::rows
Index rows() const
Definition: matrixfree_cg.cpp:33
MatrixReplacement
Definition: matrixfree_cg.cpp:21
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
S
DiscreteKey S(1, 2)
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74


gtsam
Author(s):
autogenerated on Tue Jun 25 2024 03:01:39