00001 // This file is part of Eigen, a lightweight C++ template library 00002 // for linear algebra. 00003 // 00004 // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr> 00005 00006 /* NOTE The class IterationController has been adapted from the iteration 00007 * class of the GMM++ and ITL libraries. 00008 */ 00009 00010 //======================================================================= 00011 // Copyright (C) 1997-2001 00012 // Authors: Andrew Lumsdaine <lums@osl.iu.edu> 00013 // Lie-Quan Lee <llee@osl.iu.edu> 00014 // 00015 // This file is part of the Iterative Template Library 00016 // 00017 // You should have received a copy of the License Agreement for the 00018 // Iterative Template Library along with the software; see the 00019 // file LICENSE. 00020 // 00021 // Permission to modify the code and to distribute modified code is 00022 // granted, provided the text of this NOTICE is retained, a notice that 00023 // the code was modified is included with the above COPYRIGHT NOTICE and 00024 // with the COPYRIGHT NOTICE in the LICENSE file, and that the LICENSE 00025 // file is distributed with the modified code. 00026 // 00027 // LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. 00028 // By way of example, but not limitation, Licensor MAKES NO 00029 // REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY 00030 // PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS 00031 // OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS 00032 // OR OTHER RIGHTS. 00033 //======================================================================= 00034 00035 //======================================================================== 00036 // 00037 // Copyright (C) 2002-2007 Yves Renard 00038 // 00039 // This file is a part of GETFEM++ 00040 // 00041 // Getfem++ is free software; you can redistribute it and/or modify 00042 // it under the terms of the GNU Lesser General Public License as 00043 // published by the Free Software Foundation; version 2.1 of the License. 00044 // 00045 // This program is distributed in the hope that it will be useful, 00046 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00047 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00048 // GNU Lesser General Public License for more details. 00049 // You should have received a copy of the GNU Lesser General Public 00050 // License along with this program; if not, write to the Free Software 00051 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 00052 // USA. 00053 // 00054 //======================================================================== 00055 00056 #include "../../../../Eigen/src/Core/util/NonMPL2.h" 00057 00058 #ifndef EIGEN_ITERATION_CONTROLLER_H 00059 #define EIGEN_ITERATION_CONTROLLER_H 00060 00061 namespace Eigen { 00062 00071 class IterationController 00072 { 00073 protected : 00074 double m_rhsn; 00075 size_t m_maxiter; 00076 int m_noise; 00077 double m_resmax; 00078 double m_resminreach, m_resadd; 00079 size_t m_nit; 00080 double m_res; 00081 bool m_written; 00082 void (*m_callback)(const IterationController&); 00083 public : 00084 00085 void init() 00086 { 00087 m_nit = 0; m_res = 0.0; m_written = false; 00088 m_resminreach = 1E50; m_resadd = 0.0; 00089 m_callback = 0; 00090 } 00091 00092 IterationController(double r = 1.0E-8, int noi = 0, size_t mit = size_t(-1)) 00093 : m_rhsn(1.0), m_maxiter(mit), m_noise(noi), m_resmax(r) { init(); } 00094 00095 void operator ++(int) { m_nit++; m_written = false; m_resadd += m_res; } 00096 void operator ++() { (*this)++; } 00097 00098 bool first() { return m_nit == 0; } 00099 00100 /* get/set the "noisyness" (verbosity) of the solvers */ 00101 int noiseLevel() const { return m_noise; } 00102 void setNoiseLevel(int n) { m_noise = n; } 00103 void reduceNoiseLevel() { if (m_noise > 0) m_noise--; } 00104 00105 double maxResidual() const { return m_resmax; } 00106 void setMaxResidual(double r) { m_resmax = r; } 00107 00108 double residual() const { return m_res; } 00109 00110 /* change the user-definable callback, called after each iteration */ 00111 void setCallback(void (*t)(const IterationController&)) 00112 { 00113 m_callback = t; 00114 } 00115 00116 size_t iteration() const { return m_nit; } 00117 void setIteration(size_t i) { m_nit = i; } 00118 00119 size_t maxIterarions() const { return m_maxiter; } 00120 void setMaxIterations(size_t i) { m_maxiter = i; } 00121 00122 double rhsNorm() const { return m_rhsn; } 00123 void setRhsNorm(double r) { m_rhsn = r; } 00124 00125 bool converged() const { return m_res <= m_rhsn * m_resmax; } 00126 bool converged(double nr) 00127 { 00128 using std::abs; 00129 m_res = abs(nr); 00130 m_resminreach = (std::min)(m_resminreach, m_res); 00131 return converged(); 00132 } 00133 template<typename VectorType> bool converged(const VectorType &v) 00134 { return converged(v.squaredNorm()); } 00135 00136 bool finished(double nr) 00137 { 00138 if (m_callback) m_callback(*this); 00139 if (m_noise > 0 && !m_written) 00140 { 00141 converged(nr); 00142 m_written = true; 00143 } 00144 return (m_nit >= m_maxiter || converged(nr)); 00145 } 00146 template <typename VectorType> 00147 bool finished(const MatrixBase<VectorType> &v) 00148 { return finished(double(v.squaredNorm())); } 00149 00150 }; 00151 00152 } // end namespace Eigen 00153 00154 #endif // EIGEN_ITERATION_CONTROLLER_H