SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IterativeSolverIterator.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2013 Soumyajit De
8  */
9 
10 #ifndef ITERATIVE_SOLVER_ITERATOR_H_
11 #define ITERATIVE_SOLVER_ITERATOR_H_
12 
13 #include <shogun/lib/config.h>
14 
15 #ifdef HAVE_EIGEN3
17 
18 using namespace Eigen;
19 
20 namespace shogun
21 {
22 
27 typedef struct _IterInfo
28 {
31 
34 } IterInfo;
35 
44 template<class T> class IterativeSolverIterator
45 {
46 
47 typedef Matrix<T, Dynamic, 1> VectorXt;
48 
49 public:
60  IterativeSolverIterator(const VectorXt& b,
61  index_t max_iteration_limit=1000,
62  float64_t relative_tolerence=1E-5,
63  float64_t absolute_tolerence=1E-5)
64  : m_max_iteration_limit(max_iteration_limit),
65  m_tolerence(absolute_tolerence+relative_tolerence*b.norm()),
66  m_success(false)
67  {
68  m_iter_info.residual_norm=std::numeric_limits<float64_t>::max();
69  m_iter_info.iteration_count=0;
70  }
71 
73  void begin(const VectorXt& residual)
74  {
75  m_iter_info.residual_norm=residual.norm();
76  m_iter_info.iteration_count=0;
77  }
78 
80  const bool end(const VectorXt& residual)
81  {
82  m_iter_info.residual_norm=residual.norm();
83 
84  m_success=m_iter_info.residual_norm < m_tolerence;
85  return m_success || m_iter_info.iteration_count >= m_max_iteration_limit;
86  }
87 
89  const IterInfo get_iter_info() const
90  {
91  return m_iter_info;
92  }
93 
95  const bool succeeded(const VectorXt& residual)
96  {
97  m_iter_info.residual_norm=residual.norm();
98 
99  m_success=m_iter_info.residual_norm < m_tolerence;
100  return m_success;
101  }
102 
104  void operator++()
105  {
106  m_iter_info.iteration_count++;
107  }
108 
109 private:
111  IterInfo m_iter_info;
112 
114  const index_t m_max_iteration_limit;
115 
117  const float64_t m_tolerence;
118 
120  bool m_success;
121 };
122 
123 }
124 
125 #endif // HAVE_EIGEN3
126 #endif // ITERATIVE_SOLVER_ITERATOR_H_

SHOGUN Machine Learning Toolbox - Documentation