ALPSCore reference
result.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1998-2018 ALPS Collaboration. See COPYRIGHT.TXT
3  * All rights reserved. Use is subject to license terms. See LICENSE.TXT
4  * For use in publications, see ACKNOWLEDGE.TXT
5  */
6 #include <alps/alea/result.hpp>
7 
8 #include <type_traits>
9 
10 #include <iostream> // FIXME
11 
12 namespace alps { namespace alea {
13 
14 struct valid_visitor
15 {
16  typedef bool result_type; // required by boost::apply_visitor
17 
18  template <typename Res>
19  bool operator() (const Res &r) const { return r.valid(); }
20 };
21 
22 struct size_visitor
23 {
24  typedef size_t result_type;
25 
26  template <typename Res>
27  size_t operator() (const Res &r) const { return r.size(); }
28 };
29 
30 struct count_visitor
31 {
32  typedef size_t result_type;
33 
34  template <typename Res>
35  size_t operator() (const Res &r) const { return r.count(); }
36 };
37 
38 template <typename T> // T = double or std::complex<double>
39 struct mean_visitor
40 {
41  typedef column<T> result_type;
42 
43  column<T> operator() (const mean_result<T> &r) const { return r.mean(); }
44  template <typename Str>
45  column<T> operator() (const var_result<T,Str> &r) const { return r.mean(); }
46  template <typename Str>
47  column<T> operator() (const cov_result<T,Str> &r) const { return r.mean(); }
48  column<T> operator() (const autocorr_result<T> &r) const { return r.mean(); }
49  column<T> operator() (const batch_result<T> &r) const { return r.mean(); }
50 
51  // default case
52  template <typename Res>
53  column<T> operator() (const Res &) const { throw estimate_type_mismatch(); }
54 };
55 
56 template <typename T, typename Str> // T = double or std::complex<double>
57 struct var_visitor
58 {
59  typedef column<typename bind<Str,T>::var_type> result_type;
60 
61  result_type operator() (const mean_result<T> &) const { throw estimate_unavailable(); }
62  result_type operator() (const var_result<T,Str> &r) const { return r.var(); }
63  result_type operator() (const cov_result<T,Str> &r) const { return r.var(); }
64  result_type operator() (const autocorr_result<T> &) const { throw estimate_type_mismatch(); }
65  result_type operator() (const batch_result<T> &r) const { return r.template var<Str>(); }
66 
67  // default case
68  template <typename Res>
69  result_type operator() (const Res &) const { throw estimate_type_mismatch(); }
70 };
71 
72 template <typename T> // T = double or std::complex<double>
73 struct var_visitor<T, circular_var>
74 {
75  typedef circular_var Str;
76  typedef column<typename bind<circular_var,T>::var_type> result_type;
77 
78  result_type operator() (const mean_result<T> &) const { throw estimate_unavailable(); }
79  result_type operator() (const var_result<T,Str> &r) const { return r.var(); }
80  result_type operator() (const cov_result<T,Str> &r) const { return r.var(); }
81  result_type operator() (const autocorr_result<T> &r) const { return r.var(); }
82  result_type operator() (const batch_result<T> &r) const { return r.var(); }
83 
84  // default case
85  template <typename Res>
86  result_type operator() (const Res &) const { throw estimate_type_mismatch(); }
87 };
88 
89 template <typename T, typename Str> // T = double or std::complex<double>
90 struct cov_visitor
91 {
92  typedef typename eigen<typename bind<Str,T>::cov_type>::matrix result_type;
93 
94  result_type operator() (const mean_result<T> &) const { throw estimate_unavailable(); }
95  result_type operator() (const var_result<T,Str> &) const { throw estimate_unavailable(); }
96  result_type operator() (const cov_result<T,Str> &r) const { return r.cov(); }
97  result_type operator() (const autocorr_result<T> &) const { throw estimate_unavailable(); }
98  result_type operator() (const batch_result<T> &r) const { return r.template cov<Str>(); }
99 
100  // default case
101  template <typename Res>
102  result_type operator() (const Res &) const { throw estimate_type_mismatch(); }
103 };
104 
105 struct serialize_visitor
106 {
107  typedef bool result_type;
108 
109  serialize_visitor(serializer &s, const std::string &key) : s_(s), key_(key) { }
110 
111  // default case
112  template <typename Res>
113  bool operator() (const Res &res) const
114  {
115  serialize(s_, key_, res);
116  return false; // the visitor mechanism does not allow void returns
117  }
118 
119 private:
120  serializer &s_;
121  const std::string &key_;
122 };
123 
124 bool result::valid() const
125 {
126  return boost::apply_visitor(valid_visitor(), res_);
127 }
128 
129 size_t result::size() const
130 {
131  return boost::apply_visitor(size_visitor(), res_);
132 }
133 
134 size_t result::count() const
135 {
136  return boost::apply_visitor(count_visitor(), res_);
137 }
138 
139 template <typename T>
141 {
142  return boost::apply_visitor(mean_visitor<T>(), res_);
143 }
144 
145 template column<double> result::mean<double>() const;
146 template column<std::complex<double> > result::mean<std::complex<double> >() const;
147 
148 template <typename T, typename Str>
150 {
151  return boost::apply_visitor(var_visitor<T,Str>(), res_);
152 }
153 
154 template column<double> result::var<double, circular_var>() const;
155 template column<double> result::var<std::complex<double>, circular_var>() const;
156 template column<complex_op<double> > result::var<std::complex<double>, elliptic_var>() const;
157 
158 template <typename T, typename Str>
160 {
161  return boost::apply_visitor(cov_visitor<T,Str>(), res_);
162 }
163 
164 template eigen<double>::matrix result::cov<double, circular_var>() const;
165 template eigen<std::complex<double> >::matrix result::cov<std::complex<double>, circular_var>() const;
166 template eigen<complex_op<double> >::matrix result::cov<std::complex<double>, elliptic_var>() const;
167 
168 void serialize(serializer &s, const std::string &key, const result &result)
169 {
170  boost::apply_visitor(serialize_visitor(s, key), result.res_);
171 }
172 
173 }}
bool valid() const
Definition: result.cpp:124
column< typename bind< Str, T >::var_type > var() const
Definition: result.cpp:149
F::result_type apply_visitor(F &visitor, dictionary::const_iterator it)
Const-access visitor to a value by an iterator.
Definition: dictionary.hpp:110
void serialize(serializer &, const std::string &, const autocorr_result< T > &)
Definition: autocorr.cpp:243
eigen< typename bind< Str, T >::cov_type >::matrix cov() const
Definition: result.cpp:159
size_t count() const
Definition: result.cpp:134
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > matrix
Definition: util.hpp:49
column< T > mean() const
Definition: result.cpp:140
size_t size() const
Definition: result.cpp:129
T r(T x, T y=T(), T z=T())