ALPSCore reference
convert.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 
12 
13 // Forward
14 
15 namespace alps { namespace alea {
16  template <typename Result> struct joiner;
17 
18  template <typename T> class mean_dat;
19  template <typename T, typename Str> class var_data;
20  template <typename T, typename Str> class cov_data;
21  template <typename T> class batch_data;
22 }}
23 
24 // Actual
25 
26 namespace alps { namespace alea {
27 
42 template <typename R1, typename R2,
43  typename Result=typename internal::joined<R1, R2>::result_type>
44 Result join(const R1 &first, const R2 &second)
45 {
46  return joiner<Result>()(first, second);
47 }
48 
50 template <typename Result>
51 struct joiner;
52 
53 template <typename T>
54 struct joiner<mean_result<T> >
55 {
56  template <typename R1, typename R2>
57  mean_result<T> operator()(const R1 &first, const R2 &second)
58  {
59  if (first.store().count() != second.store().count())
60  throw weight_mismatch(); // TODO
61 
62  mean_result<T> res(mean_data<T>(first.size() + second.size()));
63  res.store().data().topRows(first.size()) = first.store().data();
64  res.store().data().bottomRows(second.size()) = second.store().data();
65  res.store().count() = first.store().count();
66  return res;
67  }
68 };
69 
70 template <typename T>
71 struct joiner<var_result<T> >
72 {
73  template <typename R1, typename R2>
74  var_result<T> operator()(const R1 &first, const R2 &second)
75  {
76  if (first.store().count() != second.store().count())
77  throw weight_mismatch();
78  if (first.store().count2() != second.store().count2())
79  throw weight_mismatch();
80 
81  var_result<T> res(var_data<T>(first.size() + second.size()));
82  res.store().data().topRows(first.size()) = first.store().data();
83  res.store().data().bottomRows(second.size()) = second.store().data();
84  res.store().data2().topRows(first.size()) = first.store().data2();
85  res.store().data2().bottomRows(second.size()) = second.store().data2();
86  res.store().count() = first.store().count();
87  res.store().count2() = first.store().count2();
88  return res;
89  }
90 };
91 
92 template <typename T>
93 struct joiner<cov_result<T> >
94 {
95  template <typename R1, typename R2>
96  cov_result<T> operator()(const R1 &first, const R2 &second)
97  {
98  if (first.store().count() != second.store().count())
99  throw weight_mismatch();
100  if (first.store().count2() != second.store().count2())
101  throw weight_mismatch();
102 
103  cov_result<T> res(cov_data<T>(first.size() + second.size()));
104  res.store().data().topRows(first.size()) = first.store().data();
105  res.store().data().bottomRows(second.size()) = second.store().data();
106 
107  // ignore cross correlation
108  res.store().data2().topLeftCorner(first.size(), first.size())
109  = first.store().data2();
110  res.store().data2().bottomRightCorner(second.size(), second.size())
111  = second.store().data2();
112  res.store().count() = first.store().count();
113  return res;
114  }
115 };
116 
117 template <typename T>
119 {
120  template <typename R1, typename R2>
121  autocorr_result<T> operator()(const R1 &first, const R2 &second)
122  {
123  if (first.count() != second.count())
124  throw weight_mismatch();
125  if (first.nlevel() != second.nlevel())
126  throw size_mismatch();
127 
128  // granularities are checked on the individual levels
129  autocorr_result<T> res(first.nlevel());
130  for (size_t l = 0; l != first.nlevel(); ++l)
131  res.level(l) = join(first.level(l), second.level(l));
132  return res;
133  }
134 };
135 
136 template <typename T>
137 struct joiner<batch_result<T> >
138 {
139  template <typename R1, typename R2>
140  batch_result<T> operator()(const R1 &first, const R2 &second)
141  {
142  if (first.store().count() != second.store().count())
143  throw weight_mismatch();
144  if (first.store().num_batches() != second.store().num_batches())
145  throw size_mismatch();
146 
147  batch_result<T> res(batch_data<T>(first.size() + second.size(),
148  first.store().num_batches()));
149 
150  res.store().batch().topRows(first.size()) = first.store().batch();
151  res.store().batch().bottomRows(second.size()) = second.store().batch();
152  res.store().count() = first.store().count();
153  return res;
154  }
155 };
156 
157 }}
batch_result< T > operator()(const R1 &first, const R2 &second)
Definition: convert.hpp:140
const level_result_type & level(size_t i) const
Definition: autocorr.hpp:202
const var_data< T, Strategy > & store() const
Definition: variance.hpp:252
const mean_data< T > & store() const
Definition: mean.hpp:190
Result join(const R1 &first, const R2 &second)
Definition: convert.hpp:44
cov_result< T > operator()(const R1 &first, const R2 &second)
Definition: convert.hpp:96
var_result< T > operator()(const R1 &first, const R2 &second)
Definition: convert.hpp:74
mean_result< T > operator()(const R1 &first, const R2 &second)
Definition: convert.hpp:57
autocorr_result< T > operator()(const R1 &first, const R2 &second)
Definition: convert.hpp:121
const batch_data< T > & store() const
Definition: batch.hpp:216
const cov_data< T, Strategy > & store() const
Definition: covariance.hpp:259