32 #ifndef MCKL_CORE_WEIGHT_HPP 33 #define MCKL_CORE_WEIGHT_HPP 64 double ess()
const {
return ess_; }
67 const double *
data()
const {
return data_.data(); }
70 template <
typename OutputIter>
71 OutputIter
read(OutputIter first)
const 73 return std::copy(data_.begin(), data_.end(), first);
79 std::fill(data_.begin(), data_.end(), 1.0 /
size());
80 ess_ =
static_cast<double>(
size());
84 template <
typename InputIter>
85 void set(InputIter first)
87 std::copy_n(first,
size(), data_.begin());
96 template <
typename InputIter>
100 std::copy_n(first,
size(), data_.begin());
104 template <
typename InputIter>
114 void mul(
const double *first)
121 void mul(
double *first) {
mul(const_cast<const double *>(first)); }
124 template <
typename InputIter>
127 std::copy_n(first,
size(), data_.begin());
132 template <
typename InputIter>
135 log(
size(), data_.data(), data_.data());
145 log(
size(), data_.data(), data_.data());
146 add(
size(), first, data_.data(), data_.data());
155 template <
typename RNGType>
160 return dist(rng, data_.begin(), data_.end(),
true);
165 return w1.ess_ == w2.ess_ && w1.data_ == w2.data_;
182 template <
typename InputIter>
183 double max_element(
size_type n, InputIter first)
186 typename std::iterator_traits<InputIter>::value_type;
188 value_type v = -const_inf<value_type>();
189 for (
size_type i = 0; i != n; ++i, ++first) {
195 return static_cast<double>(v);
198 void normalize(
bool use_log)
200 double *w = data_.data();
203 const double lmax = use_log ? max_element(
size(), w) : 0;
207 for (
size_type i = 0; i != m; ++i, w += k) {
208 normalize(k, w, accw, essw, use_log, lmax);
210 normalize(l, w, accw, essw, use_log, lmax);
212 ess_ = accw * accw / essw;
215 void normalize(
size_type n,
double *w,
double &accw,
double &essw,
216 bool use_log,
double lmax)
218 internal::size_check<MCKL_BLAS_INT>(n,
"Weight::normalize");
224 accw = std::accumulate(w, w + n, accw);
226 internal::cblas_ddot(static_cast<MCKL_BLAS_INT>(n), w, 1, w, 1);
236 #endif // MCKL_CORE_WEIGHT_HPP const double * data() const
Pointer to data of the normalized weight.
void set_log(InputIter first)
Set .
friend bool operator!=(const Weight &w1, const Weight &w2)
void mul(std::size_t n, const float *a, const float *b, float *y)
typename WeightTypeTrait< T >::type WeightType
double ess() const
Return the ESS of the particle system.
void mul(InputIter first)
Set .
void set_exact(double ess, InputIter first)
Set exact values of ESS and normalized weights.
void shrink_to_fit()
Shrink to fit.
std::vector< T, Alloc > Vector
std::vector with Allocator as the default allocator
#define MCKL_DEFINE_TYPE_DISPATCH_TRAIT(Outer, Inner, Default)
void log(std::size_t n, const float *a, float *y)
size_type size() const
Size of this Weight object.
void mul(const double *first)
Set .
void sub(std::size_t n, const float *a, const float *b, float *y)
size_type draw(RNGType &rng) const
Draw integer index in the range according to the weights.
friend bool operator==(const Weight &w1, const Weight &w2)
OutputIter read(OutputIter first) const
Read all normalized weights to an output iterator.
void add_log(InputIter first)
Set .
void mul(double *first)
Set .
void resize(size_type N)
Resize the Weight object.
void reserve(size_type N)
Reserve space.
void exp(std::size_t n, const float *a, float *y)
Draw a single sample given weights.
friend bool is_equal(const Weight &w1, const Weight &w2)
void add_log(double *first)
Set .
void add_log(const double *first)
Set .
void add(std::size_t n, const float *a, const float *b, float *y)