BasicPreconditioners.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2011-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_BASIC_PRECONDITIONERS_H
11 #define EIGEN_BASIC_PRECONDITIONERS_H
12 
13 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
37 template <typename Scalar_>
39 {
40  typedef Scalar_ Scalar;
42  public:
44  enum {
47  };
48 
50 
51  template<typename MatType>
52  explicit DiagonalPreconditioner(const MatType& mat) : m_invdiag(mat.cols())
53  {
54  compute(mat);
55  }
56 
57  EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT { return m_invdiag.size(); }
58  EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT { return m_invdiag.size(); }
59 
60  template<typename MatType>
62  {
63  return *this;
64  }
65 
66  template<typename MatType>
68  {
69  m_invdiag.resize(mat.cols());
70  for(int j=0; j<mat.outerSize(); ++j)
71  {
72  typename MatType::InnerIterator it(mat,j);
73  while(it && it.index()!=j) ++it;
74  if(it && it.index()==j && it.value()!=Scalar(0))
75  m_invdiag(j) = Scalar(1)/it.value();
76  else
77  m_invdiag(j) = Scalar(1);
78  }
79  m_isInitialized = true;
80  return *this;
81  }
82 
83  template<typename MatType>
85  {
86  return factorize(mat);
87  }
88 
90  template<typename Rhs, typename Dest>
91  void _solve_impl(const Rhs& b, Dest& x) const
92  {
93  x = m_invdiag.array() * b.array() ;
94  }
95 
96  template<typename Rhs> inline const Solve<DiagonalPreconditioner, Rhs>
97  solve(const MatrixBase<Rhs>& b) const
98  {
99  eigen_assert(m_isInitialized && "DiagonalPreconditioner is not initialized.");
100  eigen_assert(m_invdiag.size()==b.rows()
101  && "DiagonalPreconditioner::solve(): invalid number of rows of the right hand side matrix b");
102  return Solve<DiagonalPreconditioner, Rhs>(*this, b.derived());
103  }
104 
106 
107  protected:
110 };
111 
129 template <typename Scalar_>
131 {
132  typedef Scalar_ Scalar;
135  using Base::m_invdiag;
136  public:
137 
139 
140  template<typename MatType>
141  explicit LeastSquareDiagonalPreconditioner(const MatType& mat) : Base()
142  {
143  compute(mat);
144  }
145 
146  template<typename MatType>
148  {
149  return *this;
150  }
151 
152  template<typename MatType>
154  {
155  // Compute the inverse squared-norm of each column of mat
156  m_invdiag.resize(mat.cols());
157  if(MatType::IsRowMajor)
158  {
159  m_invdiag.setZero();
160  for(Index j=0; j<mat.outerSize(); ++j)
161  {
162  for(typename MatType::InnerIterator it(mat,j); it; ++it)
163  m_invdiag(it.index()) += numext::abs2(it.value());
164  }
165  for(Index j=0; j<mat.cols(); ++j)
168  }
169  else
170  {
171  for(Index j=0; j<mat.outerSize(); ++j)
172  {
173  RealScalar sum = mat.col(j).squaredNorm();
174  if(sum>RealScalar(0))
175  m_invdiag(j) = RealScalar(1)/sum;
176  else
177  m_invdiag(j) = RealScalar(1);
178  }
179  }
180  Base::m_isInitialized = true;
181  return *this;
182  }
183 
184  template<typename MatType>
186  {
187  return factorize(mat);
188  }
189 
191 
192  protected:
193 };
194 
203 {
204  public:
205 
207 
208  template<typename MatrixType>
209  explicit IdentityPreconditioner(const MatrixType& ) {}
210 
211  template<typename MatrixType>
213 
214  template<typename MatrixType>
215  IdentityPreconditioner& factorize(const MatrixType& ) { return *this; }
216 
217  template<typename MatrixType>
218  IdentityPreconditioner& compute(const MatrixType& ) { return *this; }
219 
220  template<typename Rhs>
221  inline const Rhs& solve(const Rhs& b) const { return b; }
222 
224 };
225 
226 } // end namespace Eigen
227 
228 #endif // EIGEN_BASIC_PRECONDITIONERS_H
Array< int, 3, 1 > b
RealReturnType real() const
#define EIGEN_NOEXCEPT
Definition: Macros.h:1260
#define EIGEN_CONSTEXPR
Definition: Macros.h:747
#define eigen_assert(x)
Definition: Macros.h:902
Matrix< float, 1, Dynamic > MatrixType
internal::traits< Derived >::StorageIndex StorageIndex
The type used to store indices.
Definition: DenseBase.h:58
A preconditioner based on the digonal entries.
DiagonalPreconditioner & factorize(const MatType &mat)
EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
DiagonalPreconditioner & analyzePattern(const MatType &)
void _solve_impl(const Rhs &b, Dest &x) const
DiagonalPreconditioner(const MatType &mat)
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
DiagonalPreconditioner & compute(const MatType &mat)
const Solve< DiagonalPreconditioner, Rhs > solve(const MatrixBase< Rhs > &b) const
Matrix< Scalar, Dynamic, 1 > Vector
A naive preconditioner which approximates any matrix as the identity matrix.
IdentityPreconditioner & factorize(const MatrixType &)
IdentityPreconditioner(const MatrixType &)
IdentityPreconditioner & analyzePattern(const MatrixType &)
const Rhs & solve(const Rhs &b) const
IdentityPreconditioner & compute(const MatrixType &)
Jacobi preconditioner for LeastSquaresConjugateGradient.
LeastSquareDiagonalPreconditioner & compute(const MatType &mat)
LeastSquareDiagonalPreconditioner & analyzePattern(const MatType &)
DiagonalPreconditioner< Scalar_ > Base
LeastSquareDiagonalPreconditioner & factorize(const MatType &mat)
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
Derived & setZero(Index size)
constexpr void resize(Index rows, Index cols)
Pseudo expression representing a solving operation.
Definition: Solve.h:65
ComputationInfo
Definition: Constants.h:444
@ Success
Definition: Constants.h:446
bool abs2(bool x)
: InteropHeaders
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
const int Dynamic
Definition: Constants.h:24
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:231
std::ptrdiff_t j