MCKL
Monte Carlo Kernel Library
estimate_matrix.hpp
Go to the documentation of this file.
1 //============================================================================
2 // MCKL/include/mckl/core/estimate_matrix.hpp
3 //----------------------------------------------------------------------------
4 // MCKL: Monte Carlo Kernel Library
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013-2018, Yan Zhou
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //============================================================================
31 
32 #ifndef MCKL_CORE_ESTIMATE_MATRIX_HPP
33 #define MCKL_CORE_ESTIMATE_MATRIX_HPP
34 
35 #include <mckl/internal/common.hpp>
36 #include <mckl/core/matrix.hpp>
37 
38 namespace mckl {
39 
48 template <typename T>
49 class EstimateMatrix : public Matrix<T, RowMajor>
50 {
51  public:
59 
61 
63  std::size_t dim() const noexcept { return this->ncol(); }
64 
66  std::size_t num_iter() const noexcept { return this->nrow(); }
67 
69  void clear() { this->resize(0, dim()); }
70 
72  void reserve(std::size_t n)
73  {
75  }
76 
78  estimate_range estimate(size_type i) { return this->row_range(i); }
79 
82  {
83  return this->row_range(i);
84  }
85 
87  variable_range variable(size_type j) { return this->col_range(j); }
88 
91  {
92  return this->col_range(j);
93  }
94 
97  {
98  const size_type i = num_iter();
99  this->resize(i + 1, dim());
100 
101  return this->row_data(i);
102  }
103 
105  template <typename InputIter>
106  void insert_estimate(InputIter first)
107  {
108  this->push_back_row(first);
109  }
110 
121  template <typename InputIter>
122  void insert_estimate(size_type i, InputIter first)
123  {
124  const size_type t = num_iter();
125  if (i < t) {
126  std::copy_n(first, dim(), this->row_begin(i));
127  }
128  if (i == t) {
129  insert_estimate(first);
130  }
131  if (i > t) {
132  insert_estimate_dispatch(t, i, first, std::is_floating_point<T>());
133  }
134  }
135 
136  private:
138  using matrix_type::resize;
139 
140  template <typename InputIter>
141  void insert_estimate_dispatch(
142  size_type t, size_type i, InputIter first, std::true_type)
143  {
144  insert_estimate_dispatch(t, i, first, std::false_type());
145  if (i > t) {
146  std::fill_n(this->row_begin(t), (i - t) * dim(), -const_nan<T>());
147  }
148  }
149 
150  template <typename InputIter>
151  void insert_estimate_dispatch(
152  size_type t, size_type i, InputIter first, std::false_type)
153  {
154  this->resize(std::max(i + 1, t), dim());
155  std::copy_n(first, dim(), this->row_begin(i));
156  }
157 }; // class EstimateMatrix
158 
159 } // namespace mckl
160 
161 #endif // MCKL_CORE_ESTIMATE_MATRIX_HPP
std::size_t num_iter() const noexcept
The number of iterations stored in the estimate matrix.
void insert_estimate(size_type i, InputIter first)
Add a new estimate into the matrix given iteration number.
Estimate matrix for iterative Monte Carlo algorithms.
typename matrix_type::const_row_range const_estimate_range
void reserve(std::size_t n)
Reserve space for additional iterations.
size_type ncol() const
The number of columns.
Definition: matrix.hpp:488
void insert_estimate(InputIter first)
Add a new estimate to the bottom of the matrix.
void resize(size_type nrow, size_type ncol)
Resize the matrix.
Definition: matrix.hpp:506
void clear()
Clear the estimate matrix but preserve the dimension.
const_estimate_range estimate(size_type i) const
Range of an estimate.
variable_range variable(size_type j)
Range of a variable.
Matrix container.
Definition: matrix.hpp:49
Range< const_row_iterator > const_row_range
Definition: matrix.hpp:89
Range< const_col_iterator > const_col_range
Definition: matrix.hpp:94
pointer row_data(size_type i)
Pointer to the first element of a row.
Definition: matrix.hpp:443
void push_back_col(InputIter first)
Insert a new coloumn at the right.
Definition: matrix.hpp:543
estimate_range estimate(size_type i)
Range of an estimate.
Range< row_iterator > row_range
Definition: matrix.hpp:88
typename matrix_type::col_range variable_range
Range< col_iterator > col_range
Definition: matrix.hpp:93
typename matrix_type::value_type value_type
typename matrix_type::row_range estimate_range
row_iterator row_begin(size_type i)
Iterator to the beginning of a row.
Definition: matrix.hpp:211
T * insert_estimate()
Add space for a new estimate, return a pointer to the new row.
const_variable_range variable(size_type j) const
Range of a variable.
Definition: mcmc.hpp:40
void reserve(size_type n, size_type m)
Reserve storage space for the matrix.
Definition: matrix.hpp:497
size_type nrow() const
The number of rows.
Definition: matrix.hpp:485
EstimateMatrix(size_type dim)
void push_back_row(InputIter first)
Insert a new row at the bottom.
Definition: matrix.hpp:535
typename matrix_type::const_col_range const_variable_range
typename matrix_type::size_type size_type
std::size_t dim() const noexcept
The dimension of the estimator.