ALPSCore reference
transformer.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 <alps/alea/core.hpp>
9 #include <alps/alea/computed.hpp>
10 
11 #include <alps/alea/mean.hpp>
13 #include <alps/alea/convert.hpp>
14 
15 // Forward declarations
16 
17 namespace alps { namespace alea {
18  template <typename T> struct linear_transformer;
19  template <typename T> struct scalar_unary_transformer;
20  template <typename T> struct scalar_binary_transformer;
21 }}
22 
23 // Actual declarations
24 
25 namespace alps { namespace alea {
26 
27 template <typename T>
29 {
30  return scalar_unary_transformer<T>(fn);
31 }
32 
33 template <typename T>
35 {
37 }
38 
42 template <typename T>
43 struct linear_transformer
44  : public transformer<T>
45 {
46 public:
47  template <typename Derived>
48  linear_transformer(const Eigen::MatrixBase<Derived> &mat)
49  : mat_(mat)
50  { }
51 
52  size_t in_size() const { return mat_.rows(); }
53 
54  size_t out_size() const { return mat_.cols(); }
55 
56  column<T> operator() (const column<T> &in) const
57  {
58  // TODO figure this out
59  return mat_ * typename eigen<T>::col(in);
60  }
61 
62  bool is_linear() const { return true; }
63 
64 private:
65  typename eigen<T>::matrix mat_;
66 };
67 
68 
69 template <typename T>
71  : public transformer<T>
72 {
73 public:
74  scalar_unary_transformer(const std::function<T(T)> &fn) : fn_(fn) { }
75 
76  size_t in_size() const { return 1; }
77 
78  size_t out_size() const { return 1; }
79 
80  column<T> operator() (const column<T> &in) const
81  {
82  if (in.size() != in_size())
83  throw size_mismatch();
84 
85  column<T> ret(1);
86  ret(0) = fn_(in(0));
87  return ret;
88  }
89 
90 private:
91  std::function<T(T)> fn_;
92 };
93 
94 template <typename T>
96  : public transformer<T>
97 {
98 public:
99  scalar_binary_transformer(const std::function<T(T,T)> &fn) : fn_(fn) { }
100 
101  size_t in_size() const { return 2; }
102 
103  size_t out_size() const { return 1; }
104 
105  column<T> operator() (const column<T> &in) const
106  {
107  if (in.size() != in_size())
108  throw size_mismatch();
109 
110  column<T> ret(1);
111  ret(0) = fn_(in(0), in(1));
112  return ret;
113  }
114 
115 private:
116  std::function<T(T,T)> fn_;
117 };
118 
119 }} /* namespace alps::alea */
scalar_unary_transformer(const std::function< T(T)> &fn)
Definition: transformer.hpp:74
size_t size() const
Definition: core.hpp:220
scalar_unary_transformer< T > make_transformer(std::function< T(T)> fn)
Definition: transformer.hpp:28
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > matrix
Definition: util.hpp:49
linear_transformer(const Eigen::MatrixBase< Derived > &mat)
Definition: transformer.hpp:48
scalar_binary_transformer(const std::function< T(T, T)> &fn)
Definition: transformer.hpp:99
Eigen::Matrix< T, Eigen::Dynamic, 1 > col
Definition: util.hpp:41