SigUtil  0.95
Utility modules for modern C++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
convergence.hpp
Go to the documentation of this file.
1 /*
2 Copyright© 2014 Akihiro Nishimura
3 
4 This software is released under the MIT License.
5 http://opensource.org/licenses/mit-license.php
6 */
7 
8 #ifndef SIG_UTIL_ERROR_CONVERGENCE_HPP
9 #define SIG_UTIL_ERROR_CONVERGENCE_HPP
10 
11 #include "../sigutil.hpp"
12 #include "../distance/norm.hpp"
13 #include "../helper/maybe.hpp"
14 #include "../helper/helper_modules.hpp"
15 
17 
18 namespace sig
19 {
21 
25 {
26  template <class T, class F>
27  double operator()(T const& value, T const& last_value, F const& norm_func) const
28  {
29  return norm_func(value, last_value);
30  }
31 };
32 
34 
36 
40 {
41  template <class T, class F>
42  double operator()(T const& value, T const& last_value, F const& norm_func) const
43  {
44  return norm_func(value, last_value) / norm_func(last_value);
45  }
46 };
47 
49 
50 
52 
65 {
66  const double epsilon_;
67  Maybe<double> last_value_;
68  bool conv_;
69 
70 public:
72 
75  ManageConvergenceSimple(double epsilon) : epsilon_(epsilon), last_value_(Nothing(0)), conv_(false) {}
76 
78 
83  bool update(double value){
84  conv_ = isJust(last_value_) && abs_delta(value, fromJust(last_value_)) < epsilon_;
85  last_value_ <<= value;
86 
87  return conv_;
88  }
89 
91  double get_value() const{ return isJust(last_value_) ? fromJust(last_value_) : -1; }
92 
93  bool is_convergence() const{ return conv_; }
94 };
95 
96 
98 
119 template <class T, class Criteria = RelativeError, class F = Norm<2>>
121 {
122  const double epsilon_;
123  const F norm_func_;
124  const Criteria criteria_;
125  Maybe<T> last_value_;
126  bool conv_;
127 
128 public:
130 
134  ManageConvergence(double epsilon, F norm_func = norm_L2) : epsilon_(epsilon), norm_func_(norm_func), criteria_(Criteria()), last_value_(Nothing(T())), conv_(false) {}
135 
137 
142  bool update(T value){
143  conv_ = isJust(last_value_) && criteria_(value, fromJust(last_value_), norm_func_) < epsilon_;
144  last_value_ <<= value;
145 
146  return conv_;
147  }
148 
149  bool is_convergence() const{ return conv_; }
150 };
151 
152 }
153 #endif
bool update(double value)
状態の更新と収束判定
Definition: convergence.hpp:83
ManageConvergence(double epsilon, F norm_func=norm_L2)
コンストラクタ
const Norm< 2 > norm_L2
L2ノルムを求める関数(関数オブジェクト)
Definition: norm.hpp:82
T abs_delta(T v1, T v2)
auto Nothing() -> typename impl::SameIf< T, void, typename boost::none_t, Maybe< T >>::type
値コンストラクタ
Definition: maybe.hpp:67
bool update(T value)
状態の更新とノルム計算、および収束判定
bool is_convergence() const
収束判定の管理を行うクラス
Definition: convergence.hpp:64
収束判定の計算と管理を行うクラス
誤差基準(絶対誤差)
Definition: convergence.hpp:24
double operator()(T const &value, T const &last_value, F const &norm_func) const
Definition: convergence.hpp:42
ManageConvergenceSimple(double epsilon)
コンストラクタ
Definition: convergence.hpp:75
bool isJust(Maybe< T > const &m)
Justであるか調べる関数.Maybe a -> Bool.
Definition: maybe.hpp:84
double operator()(T const &value, T const &last_value, F const &norm_func) const
Definition: convergence.hpp:27
boost::optional< T > Maybe
Definition: maybe.hpp:45
double get_value() const
前回の値の取得
Definition: convergence.hpp:91
const AbsoluteError absolute_error
Definition: convergence.hpp:33
T & fromJust(Maybe< T > &m)
Justから値を取り出す関数.Maybe a -> a.
Definition: maybe.hpp:113
誤差基準(相対誤差)
Definition: convergence.hpp:39
const RelativeError relative_error
Definition: convergence.hpp:48
Definition: array.hpp:15