ALPSCore reference
computed.hpp
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 #pragma once
7 
8 #include <cstddef>
9 #include <cassert>
10 #include <stdexcept>
11 #include <complex>
12 
13 #include <vector>
14 #include <array>
15 #include <Eigen/Dense>
16 
17 #include <alps/alea/core.hpp>
18 #include <alps/alea/util.hpp>
19 
20 // Forward declarations
21 
22 namespace alps { namespace alea {
23  template <typename T> class value_adapter;
24  template <typename T> class vector_adapter;
25  template <typename T, size_t N> class array_adapter;
26  template <typename T, typename Derived> class eigen_adapter;
27 }}
28 
29 // Actual declarations
30 
31 namespace alps { namespace alea {
32 
33 template <typename Derived>
35  const Eigen::DenseBase<Derived> &in)
36 {
38 }
39 
40 template <typename T>
41 vector_adapter<T> make_adapter(const std::vector<T> &v)
42 {
43  return vector_adapter<T>(v);
44 }
45 
46 template<typename T, size_t N>
47 array_adapter<T,N> make_array_adapter(const std::array<T, N> &a){
48  return array_adapter<T,N>(a);
49 }
50 
51 template <typename T>
52 class value_adapter
53  : public computed<T>
54 {
55 public:
56  typedef T value_type;
57 
58 public:
59  value_adapter(T in) : in_(in) { }
60 
61  size_t size() const { return 1; }
62 
63  void add_to(view<T> out) const
64  {
65  if (out.size() != 1)
66  throw size_mismatch();
67  out.data()[0] += in_;
68  }
69 
71 
72 private:
73  T in_;
74 };
75 
76 inline value_adapter<long> make_adapter(size_t v) // FIXME
77 {
78  return value_adapter<long>(v);
79 }
80 
82 {
83  return value_adapter<long>(v);
84 }
85 
87 {
88  return value_adapter<double>(v);
89 }
90 
91 
92 
93 template <typename T>
94 class vector_adapter
95  : public computed<T>
96 {
97 public:
98  typedef T value_type;
99 
100 public:
101  vector_adapter(const std::vector<T> &in) : in_(in) { }
102 
103  size_t size() const { return in_.size(); }
104 
105  void add_to(view<T> out) const
106  {
107  if (out.size() != in_.size())
108  throw size_mismatch();
109  for (size_t i = 0; i != in_.size(); ++i)
110  out.data()[i] += in_[i];
111  }
112 
114 
115 private:
116  const std::vector<T> &in_;
117 };
118 
119 template <typename T, size_t N> class array_adapter : public computed<T> {
120 public:
121  typedef T value_type;
122 
123 public:
124  array_adapter(const std::array<T, N> &in) : in_(in) {}
125 
126  size_t size() const { return in_.size(); }
127 
128  void add_to(view<T> out) const {
129  if (out.size() != in_.size())
130  throw size_mismatch();
131  for (size_t i = 0; i != in_.size(); ++i)
132  out.data()[i] += in_[i];
133  }
134 
136 
137 private:
138  const std::array<T, N> &in_;
139 };
140 
141 template <typename T, typename Derived>
142 class eigen_adapter
143  : public computed<T>
144 {
145 public:
146  typedef T value_type;
147 
148 public:
149  eigen_adapter(const Eigen::DenseBase<Derived> &in)
150  : in_(in)
151  {
152  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Eigen::DenseBase<Derived>);
153  static_assert(std::is_same<T, typename Derived::Scalar>::value,
154  "Type mismatch -- use explicit cast");
155  }
156 
157  size_t size() const { return in_.size(); }
158 
159  void add_to(view<T> out) const
160  {
161  if (out.size() != (size_t)in_.rows())
162  throw size_mismatch();
163 
164  typename eigen<T>::col_map out_map(out.data(), out.size());
165  out_map += in_;
166  }
167 
169 
170 private:
171  const Eigen::DenseBase<Derived> &in_;
172 };
173 
177 template <typename T, typename Parent>
179  : public computed<T>
180 {
181 public:
182  typedef T value_type;
183  typedef void (Parent::*adder_type)(view<T>) const;
184 
185 public:
186  computed_cmember(const Parent &parent, adder_type adder, size_t size)
187  : parent_(parent)
188  , adder_(adder)
189  , size_(size)
190  { }
191 
192  size_t size() const { return size_; }
193 
194  void add_to(view<T> out) const { (parent_.*adder_)(out); }
195 
196  void fast_add_to(view<T> out) { (parent_.*adder_)(out); }
197 
198  const Parent &parent() const { return parent_; }
199 
200  const adder_type &adder() const { return adder_; }
201 
203 
204 private:
205  const Parent &parent_;
206  adder_type adder_;
207  size_t size_;
208 };
209 
211 template<typename AccType>
212 typename std::enable_if<is_alea_acc<AccType>::value, AccType&>::type
213 operator<<(AccType& acc, const typename AccType::value_type& v){
214  return acc << value_adapter<typename AccType::value_type>(v);
215 }
216 
218 template<typename AccType, typename Derived>
219 typename std::enable_if<is_alea_acc<AccType>::value, AccType&>::type
220 operator<<(AccType& acc, const Eigen::DenseBase<Derived>& v){
221  return acc << eigen_adapter<typename AccType::value_type, Derived>(v);
222 }
223 
225 template<typename AccType>
226 typename std::enable_if<is_alea_acc<AccType>::value, AccType&>::type
227 operator<<(AccType& acc, const std::vector<typename AccType::value_type>& v){
228  return acc << vector_adapter<typename AccType::value_type>(v);
229 }
230 
232 template<typename AccType, size_t N>
233 typename std::enable_if<is_alea_acc<AccType>::value, AccType&>::type
234 operator<<(AccType& acc, const std::array<typename AccType::value_type, N>& v){
235  return acc << array_adapter<typename AccType::value_type, N>(v);
236 }
237 
238 }}
T * data()
Definition: core.hpp:68
computed_cmember(const Parent &parent, adder_type adder, size_t size)
Definition: computed.hpp:186
void add_to(view< T > out) const
Definition: computed.hpp:105
void add_to(view< T > out) const
Definition: computed.hpp:128
std::enable_if<!is_sequence< T >::value, std::size_t >::type size(T const &)
Definition: size.hpp:20
void add_to(view< T > out) const
Definition: computed.hpp:63
void fast_add_to(view< T > out)
Definition: computed.hpp:196
vector_adapter(const std::vector< T > &in)
Definition: computed.hpp:101
size_t size() const
Definition: computed.hpp:103
void add_to(view< T > out) const
Definition: computed.hpp:159
const Parent & parent() const
Definition: computed.hpp:198
size_t size() const
Definition: core.hpp:74
size_t size() const
Definition: computed.hpp:157
Eigen::Map< col, Eigen::Unaligned > col_map
Definition: util.hpp:42
array_adapter< T, N > make_array_adapter(const std::array< T, N > &a)
Definition: computed.hpp:47
size_t size() const
Definition: computed.hpp:126
const adder_type & adder() const
Definition: computed.hpp:200
array_adapter(const std::array< T, N > &in)
Definition: computed.hpp:124
std::ostream & operator<<(std::ostream &, const autocorr_result< T > &)
Definition: autocorr.cpp:293
size_t size() const
Definition: computed.hpp:61
eigen_adapter< typename Derived::Scalar, Derived > make_adapter(const Eigen::DenseBase< Derived > &in)
Definition: computed.hpp:34
void add_to(view< T > out) const
Definition: computed.hpp:194
eigen_adapter(const Eigen::DenseBase< Derived > &in)
Definition: computed.hpp:149