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