ALPSCore reference
propagation.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  */
7 
8 #include <iostream>
9 
10 namespace alps { namespace alea {
11 
12 template <typename T>
13 typename eigen<T>::matrix jacobian(const transformer<T> &f, column<T> x, double dx)
14 {
15  size_t in_size = f.in_size();
16  size_t out_size = f.out_size();
17 
18  typename eigen<T>::matrix result(out_size, in_size);
19  for (size_t j = 0; j != in_size; ++j) {
20  x(j) += dx;
21  result.col(j) = f(x);
22  x(j) -= dx;
23  }
24  result.colwise() -= f(x);
25  result.array() /= dx;
26  return result;
27 }
28 
30  const transformer<double> &, column<double>, double);
31 template eigen< std::complex<double> >::matrix jacobian(
32  const transformer<std::complex<double> > &, column<std::complex<double> >,
33  double);
34 
35 
36 template <typename T>
38 {
39  // compute batch sums
40  if (tf.in_size() != in.size())
41  throw size_mismatch();
42 
43  batch_data<T> res(tf.out_size(), in.num_batches());
44  column<T> sum_batch = in.batch().rowwise().sum();
45  ptrdiff_t sum_count = in.count().sum();
46 
47  // compute leave-one-out statistics and transforms
48  column<T> leaveout(in.size());
49  for (size_t i = 0; i != in.num_batches(); ++i) {
50  leaveout = (sum_batch - in.batch().col(i))
51  / (sum_count - in.count()(i));
52  res.batch().col(i) = tf(leaveout);
53  }
54 
55  res.count() = in.count();
56 
57  // Since sum_count and res.count().array() are unsigned values,
58  // (res.count().array() - sum_count) would be an array with huge positive elements.
59  res.batch().array().rowwise() *=
60  -(sum_count - res.count().array()).template cast<T>();
61 
62  // compute transform of mean
63  sum_batch /= sum_count;
64  column<T> mean_result = tf(sum_batch);
65  res.batch().colwise() += mean_result * sum_count;
66 
67  return res;
68 }
69 
71  const transformer<double> &tf);
73  const batch_data<std::complex<double> > &in,
74  const transformer<std::complex<double> > &tf);
75 
76 }}
77 
eigen< T >::matrix jacobian(const transformer< T > &f, column< T > x, double dx)
Definition: propagation.cpp:13
batch_data< T > jackknife(const batch_data< T > &in, const transformer< T > &tf)
Definition: propagation.cpp:37
eigen< T >::matrix & batch()
Definition: batch.hpp:54
eigen< size_t >::row & count()
Definition: batch.hpp:59
virtual size_t in_size() const =0
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > matrix
Definition: util.hpp:49
virtual size_t out_size() const =0
size_t size() const
Definition: batch.hpp:52
traits< Acc >::result_type result(const Acc &acc)
Definition: util.hpp:53
size_t num_batches() const
Definition: batch.hpp:49
Eigen::Matrix< T, Eigen::Dynamic, 1 > col
Definition: util.hpp:41