IterationController.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) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
5 
6 /* NOTE The class IterationController has been adapted from the iteration
7  * class of the GMM++ and ITL libraries.
8  */
9 
10 //=======================================================================
11 // Copyright (C) 1997-2001
12 // Authors: Andrew Lumsdaine <lums@osl.iu.edu>
13 // Lie-Quan Lee <llee@osl.iu.edu>
14 //
15 // This file is part of the Iterative Template Library
16 //
17 // You should have received a copy of the License Agreement for the
18 // Iterative Template Library along with the software; see the
19 // file LICENSE.
20 //
21 // Permission to modify the code and to distribute modified code is
22 // granted, provided the text of this NOTICE is retained, a notice that
23 // the code was modified is included with the above COPYRIGHT NOTICE and
24 // with the COPYRIGHT NOTICE in the LICENSE file, and that the LICENSE
25 // file is distributed with the modified code.
26 //
27 // LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.
28 // By way of example, but not limitation, Licensor MAKES NO
29 // REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY
30 // PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS
31 // OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS
32 // OR OTHER RIGHTS.
33 //=======================================================================
34 
35 //========================================================================
36 //
37 // Copyright (C) 2002-2007 Yves Renard
38 //
39 // This file is a part of GETFEM++
40 //
41 // Getfem++ is free software; you can redistribute it and/or modify
42 // it under the terms of the GNU Lesser General Public License as
43 // published by the Free Software Foundation; version 2.1 of the License.
44 //
45 // This program is distributed in the hope that it will be useful,
46 // but WITHOUT ANY WARRANTY; without even the implied warranty of
47 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48 // GNU Lesser General Public License for more details.
49 // You should have received a copy of the GNU Lesser General Public
50 // License along with this program; if not, write to the Free Software
51 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
52 // USA.
53 //
54 //========================================================================
55 
56 #include "../../../../Eigen/src/Core/util/NonMPL2.h"
57 
58 #ifndef EIGEN_ITERATION_CONTROLLER_H
59 #define EIGEN_ITERATION_CONTROLLER_H
60 
61 namespace Eigen {
62 
72 {
73  protected :
74  double m_rhsn;
75  size_t m_maxiter;
76  int m_noise;
77  double m_resmax;
79  size_t m_nit;
80  double m_res;
81  bool m_written;
83  public :
84 
85  void init()
86  {
87  m_nit = 0; m_res = 0.0; m_written = false;
88  m_resminreach = 1E50; m_resadd = 0.0;
89  m_callback = 0;
90  }
91 
92  IterationController(double r = 1.0E-8, int noi = 0, size_t mit = size_t(-1))
93  : m_rhsn(1.0), m_maxiter(mit), m_noise(noi), m_resmax(r) { init(); }
94 
95  void operator ++(int) { m_nit++; m_written = false; m_resadd += m_res; }
96  void operator ++() { (*this)++; }
97 
98  bool first() { return m_nit == 0; }
99 
100  /* get/set the "noisyness" (verbosity) of the solvers */
101  int noiseLevel() const { return m_noise; }
102  void setNoiseLevel(int n) { m_noise = n; }
103  void reduceNoiseLevel() { if (m_noise > 0) m_noise--; }
104 
105  double maxResidual() const { return m_resmax; }
106  void setMaxResidual(double r) { m_resmax = r; }
107 
108  double residual() const { return m_res; }
109 
110  /* change the user-definable callback, called after each iteration */
111  void setCallback(void (*t)(const IterationController&))
112  {
113  m_callback = t;
114  }
115 
116  size_t iteration() const { return m_nit; }
117  void setIteration(size_t i) { m_nit = i; }
118 
119  size_t maxIterarions() const { return m_maxiter; }
120  void setMaxIterations(size_t i) { m_maxiter = i; }
121 
122  double rhsNorm() const { return m_rhsn; }
123  void setRhsNorm(double r) { m_rhsn = r; }
124 
125  bool converged() const { return m_res <= m_rhsn * m_resmax; }
126  bool converged(double nr)
127  {
128  using std::abs;
129  m_res = abs(nr);
131  return converged();
132  }
133  template<typename VectorType> bool converged(const VectorType &v)
134  { return converged(v.squaredNorm()); }
135 
136  bool finished(double nr)
137  {
138  if (m_callback) m_callback(*this);
139  if (m_noise > 0 && !m_written)
140  {
141  converged(nr);
142  m_written = true;
143  }
144  return (m_nit >= m_maxiter || converged(nr));
145  }
146  template <typename VectorType>
148  { return finished(double(v.squaredNorm())); }
149 
150 };
151 
152 } // end namespace Eigen
153 
154 #endif // EIGEN_ITERATION_CONTROLLER_H
Eigen::IterationController::m_res
double m_res
last computed residual
Definition: IterationController.h:80
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Eigen::IterationController::setIteration
void setIteration(size_t i)
Definition: IterationController.h:117
Eigen::IterationController::m_written
bool m_written
Definition: IterationController.h:81
Eigen::IterationController::finished
bool finished(const MatrixBase< VectorType > &v)
Definition: IterationController.h:147
Eigen::IterationController::first
bool first()
Definition: IterationController.h:98
Eigen::IterationController::maxResidual
double maxResidual() const
Definition: IterationController.h:105
Eigen::IterationController
Controls the iterations of the iterative solvers.
Definition: IterationController.h:71
Eigen::IterationController::m_resmax
double m_resmax
maximum residual
Definition: IterationController.h:77
Eigen::IterationController::m_rhsn
double m_rhsn
Right hand side norm.
Definition: IterationController.h:74
Eigen::IterationController::setMaxIterations
void setMaxIterations(size_t i)
Definition: IterationController.h:120
Eigen::IterationController::setMaxResidual
void setMaxResidual(double r)
Definition: IterationController.h:106
Eigen::IterationController::setCallback
void setCallback(void(*t)(const IterationController &))
Definition: IterationController.h:111
n
int n
Definition: BiCGSTAB_simple.cpp:1
Eigen::IterationController::iteration
size_t iteration() const
Definition: IterationController.h:116
Eigen::IterationController::setRhsNorm
void setRhsNorm(double r)
Definition: IterationController.h:123
Eigen::IterationController::m_resminreach
double m_resminreach
Definition: IterationController.h:78
Eigen::IterationController::reduceNoiseLevel
void reduceNoiseLevel()
Definition: IterationController.h:103
Eigen::IterationController::converged
bool converged(const VectorType &v)
Definition: IterationController.h:133
Eigen::IterationController::noiseLevel
int noiseLevel() const
Definition: IterationController.h:101
Eigen::IterationController::m_noise
int m_noise
if noise > 0 iterations are printed
Definition: IterationController.h:76
Eigen::IterationController::m_maxiter
size_t m_maxiter
Max. number of iterations.
Definition: IterationController.h:75
Eigen::IterationController::m_nit
size_t m_nit
iteration number
Definition: IterationController.h:79
Eigen::IterationController::maxIterarions
size_t maxIterarions() const
Definition: IterationController.h:119
Eigen::IterationController::finished
bool finished(double nr)
Definition: IterationController.h:136
Eigen::IterationController::converged
bool converged() const
Definition: IterationController.h:125
Eigen::IterationController::converged
bool converged(double nr)
Definition: IterationController.h:126
E
DiscreteKey E(5, 2)
Eigen::IterationController::rhsNorm
double rhsNorm() const
Definition: IterationController.h:122
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
min
#define min(a, b)
Definition: datatypes.h:19
Eigen::IterationController::residual
double residual() const
Definition: IterationController.h:108
abs
#define abs(x)
Definition: datatypes.h:17
VectorType
Definition: FFTW.cpp:65
Eigen::MatrixBase
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
Eigen::IterationController::m_callback
void(* m_callback)(const IterationController &)
Definition: IterationController.h:82
Eigen::IterationController::IterationController
IterationController(double r=1.0E-8, int noi=0, size_t mit=size_t(-1))
Definition: IterationController.h:92
Eigen::IterationController::init
void init()
Definition: IterationController.h:85
align_3::t
Point2 t(10, 10)
Eigen::IterationController::operator++
void operator++()
Definition: IterationController.h:96
Eigen::IterationController::setNoiseLevel
void setNoiseLevel(int n)
Definition: IterationController.h:102
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Eigen::IterationController::m_resadd
double m_resadd
Definition: IterationController.h:78


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:02:33