MCKL
Monte Carlo Kernel Library
const_math.hpp
Go to the documentation of this file.
1 //============================================================================
2 // MCKL/include/mckl/internal/const_math.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_INTERNAL_CONST_MATH_HPP
33 #define MCKL_INTERNAL_CONST_MATH_HPP
34 
35 #include <mckl/internal/config.h>
37 #include <mckl/core/memory.hpp>
38 #include <algorithm>
39 #include <array>
40 #include <limits>
41 #include <type_traits>
42 
43 namespace mckl {
44 
45 namespace internal {
46 
48 {
49  public:
50  StirlingMatrix2(std::size_t n, std::size_t m)
51  : ncol_(m + 1), data_((n + 1) * (m + 1))
52  {
53  std::fill(data_.begin(), data_.end(), 0);
54  get(0, 0) = 1;
55  for (std::size_t j = 1; j <= m; ++j) {
56  get(j, j) = 1;
57  for (std::size_t i = j + 1; i <= n; ++i) {
58  get(i, j) = j * get(i - 1, j) + get(i - 1, j - 1);
59  }
60  }
61  }
62 
63  double operator()(std::size_t i, std::size_t j) const
64  {
65  return data_[i * ncol_ + j];
66  }
67 
68  private:
69  std::size_t ncol_;
70  Vector<double> data_;
71 
72  double &get(std::size_t i, std::size_t j) { return data_[i * ncol_ + j]; }
73 }; // class StirlingMatrix2
74 
75 template <unsigned long long U,
76  int N = std::numeric_limits<unsigned long long>::digits - 1>
77 class Log2L
78 {
79  static constexpr unsigned long long M = 1ULL << N;
80 
81  public:
82  static constexpr int value = M <= U ? N : Log2L<U, N - 1>::value;
83 }; // class Log2L
84 
85 template <unsigned long long U>
86 class Log2L<U, 0>
87 {
88  public:
89  static constexpr int value = 0;
90 }; // class Log2L
91 
92 template <int N>
93 class Log2L<0, N>
94 {
95  public:
96  static constexpr int value = N + 1;
97 }; // class Log2L
98 
99 template <typename UIntType, UIntType U>
100 class Log2 : public Log2L<U, std::numeric_limits<UIntType>::digits - 1>
101 {
102 }; // class Log2
103 
104 template <unsigned long long B, unsigned N, bool = N % 2 == 0>
105 class PowL
106 {
107  static constexpr unsigned long long b = PowL<B, N / 2>::value;
108 
109  public:
110  static constexpr unsigned long long value = b * b;
111 }; // class PowL
112 
113 template <unsigned long long B, unsigned N>
114 class PowL<B, N, false>
115 {
116  public:
117  static constexpr unsigned long long value = B * PowL<B, N - 1>::value;
118 }; // class PowL
119 
120 template <unsigned long long B>
121 class PowL<B, 0, true>
122 {
123  public:
124  static constexpr unsigned long long value = 1;
125 }; // class PowL
126 
127 template <typename UIntType, unsigned long long B, unsigned N>
128 class Pow
129 {
130  public:
131  static constexpr UIntType value = static_cast<UIntType>(PowL<B, N>::value);
132 }; // class Pow
133 
134 template <int P,
135  int Q = (std::numeric_limits<unsigned long long>::digits <
136  std::numeric_limits<long double>::digits ?
137  std::numeric_limits<unsigned long long>::digits :
138  std::numeric_limits<long double>::digits) -
139  1,
140  bool = (Q < P)>
141 class Pow2L
142 {
143  public:
144  static constexpr long double value =
145  static_cast<long double>(1ULL << Q) * Pow2L<P - Q>::value;
146 }; // class Pow2L
147 
148 template <int P, int Q>
150 {
151  public:
152  static constexpr long double value = static_cast<long double>(1ULL << P);
153 }; // class Pow2L
154 
155 template <typename RealType, int P, bool = P >= 0>
156 class Pow2Impl
157 {
158  public:
159  static constexpr RealType value = static_cast<RealType>(Pow2L<P>::value);
160 }; // class Pow2
161 
162 template <typename RealType, int P>
163 class Pow2Impl<RealType, P, false>
164 {
165  public:
166  static constexpr RealType value =
167  static_cast<RealType>(1.0L / Pow2L<-P>::value);
168 }; // class Pow2
169 
170 template <typename RealType, int P>
171 using Pow2 = Pow2Impl<RealType, P>;
172 
173 template <typename UIntType, unsigned N>
175 {
176  public:
177  static constexpr UIntType value = N * Factorial<UIntType, N - 1>::value;
178 }; // class Factorial
179 
180 template <typename UIntType>
181 class Factorial<UIntType, 1>
182 {
183  public:
184  static constexpr UIntType value = 1;
185 }; // class Factorial
186 
187 template <typename UIntType>
188 class Factorial<UIntType, 0>
189 {
190  public:
191  static constexpr UIntType value = 1;
192 }; // class Factorial
193 
194 } // namespace internal
195 
196 } // namespace mckl
197 
198 #endif // MCKL_INTERNAL_CONST_MATH_HPP
StirlingMatrix2(std::size_t n, std::size_t m)
Definition: const_math.hpp:50
std::vector< T, Alloc > Vector
std::vector with Allocator as the default allocator
Definition: memory.hpp:435
double operator()(std::size_t i, std::size_t j) const
Definition: const_math.hpp:63
Definition: mcmc.hpp:40
Pow2Impl< RealType, P > Pow2
Definition: const_math.hpp:171