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