SHOGUN
v3.2.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 2013 Soumyajit De 00008 */ 00009 00010 #ifndef ITERATIVE_SOLVER_ITERATOR_H_ 00011 #define ITERATIVE_SOLVER_ITERATOR_H_ 00012 00013 #include <shogun/lib/config.h> 00014 00015 #ifdef HAVE_EIGEN3 00016 #include <shogun/mathematics/eigen3.h> 00017 00018 using namespace Eigen; 00019 00020 namespace shogun 00021 { 00022 00027 typedef struct _IterInfo 00028 { 00030 float64_t residual_norm; 00031 00033 index_t iteration_count; 00034 } IterInfo; 00035 00044 template<class T> class IterativeSolverIterator 00045 { 00046 00047 typedef Matrix<T, Dynamic, 1> VectorXt; 00048 00049 public: 00060 IterativeSolverIterator(const VectorXt& b, 00061 index_t max_iteration_limit=1000, 00062 float64_t relative_tolerence=1E-5, 00063 float64_t absolute_tolerence=1E-5) 00064 : m_max_iteration_limit(max_iteration_limit), 00065 m_tolerence(absolute_tolerence+relative_tolerence*b.norm()), 00066 m_success(false) 00067 { 00068 m_iter_info.residual_norm=std::numeric_limits<float64_t>::max(); 00069 m_iter_info.iteration_count=0; 00070 } 00071 00073 void begin(const VectorXt& residual) 00074 { 00075 m_iter_info.residual_norm=residual.norm(); 00076 m_iter_info.iteration_count=0; 00077 } 00078 00080 const bool end(const VectorXt& residual) 00081 { 00082 m_iter_info.residual_norm=residual.norm(); 00083 00084 m_success=m_iter_info.residual_norm < m_tolerence; 00085 return m_success || m_iter_info.iteration_count >= m_max_iteration_limit; 00086 } 00087 00089 const IterInfo get_iter_info() const 00090 { 00091 return m_iter_info; 00092 } 00093 00095 const bool succeeded(const VectorXt& residual) 00096 { 00097 m_iter_info.residual_norm=residual.norm(); 00098 00099 m_success=m_iter_info.residual_norm < m_tolerence; 00100 return m_success; 00101 } 00102 00104 void operator++() 00105 { 00106 m_iter_info.iteration_count++; 00107 } 00108 00109 private: 00111 IterInfo m_iter_info; 00112 00114 const index_t m_max_iteration_limit; 00115 00117 const float64_t m_tolerence; 00118 00120 bool m_success; 00121 }; 00122 00123 } 00124 00125 #endif // HAVE_EIGEN3 00126 #endif // ITERATIVE_SOLVER_ITERATOR_H_