32 #ifndef VSMC_CORE_WEIGHT_HPP 33 #define VSMC_CORE_WEIGHT_HPP 73 double ess()
const {
return ess_; }
76 const double *
data()
const {
return data_.data(); }
79 template <
typename OutputIter>
82 return std::copy(data_.begin(), data_.end(), first);
86 template <
typename RandomIter>
89 for (std::size_t i = 0; i !=
size(); ++i, first += stride)
98 std::fill(data_.begin(), data_.end(), 1.0 /
size());
99 ess_ =
static_cast<double>(
size());
103 template <
typename InputIter>
104 void set(InputIter first)
106 std::copy_n(first,
size(), data_.begin());
111 template <
typename RandomIter>
112 void set(RandomIter first,
int stride)
119 for (std::size_t i = 0; i !=
size(); ++i, first += stride)
125 template <
typename InputIter>
128 for (std::size_t i = 0; i !=
size(); ++i, ++first)
134 void mul(
const double *first)
141 void mul(
double *first) {
mul(const_cast<const double *>(first)); }
144 template <
typename RandomIter>
145 void mul(RandomIter first,
int stride)
152 for (std::size_t i = 0; i !=
size(); ++i, first += stride)
158 template <
typename InputIter>
161 std::copy_n(first,
size(), data_.begin());
166 template <
typename RandomIter>
174 for (std::size_t i = 0; i !=
size(); ++i, first += stride)
180 template <
typename InputIter>
183 log(
size(), data_.data(), data_.data());
184 for (std::size_t i = 0; i !=
size(); ++i)
192 log(
size(), data_.data(), data_.data());
193 add(
size(), first, data_.data(), data_.data());
201 template <
typename RandomIter>
209 log(
size(), data_.data(), data_.data());
210 for (std::size_t i = 0; i !=
size(); ++i, first += stride)
217 template <
typename RNGType>
220 return draw_(rng, data_.begin(), data_.end(),
true);
228 template <
typename InputIter>
229 double max_element(std::size_t n, InputIter first)
232 typename std::iterator_traits<InputIter>::value_type;
234 value_type v = -std::numeric_limits<value_type>::infinity();
235 for (std::size_t i = 0; i != n; ++i, ++first)
239 return static_cast<double>(v);
242 void normalize(
bool use_log)
244 double *w = data_.data();
247 const double lmax = use_log ? max_element(
size(), w) : 0;
249 const std::size_t m =
size() / k;
250 const std::size_t l =
size() % k;
251 for (std::size_t i = 0; i != m; ++i, w += k)
252 normalize(k, w, accw, essw, use_log, lmax);
253 normalize(l, w, accw, essw, use_log, lmax);
255 ess_ = accw * accw / essw;
258 void normalize(std::size_t n,
double *w,
double &accw,
double &essw,
259 bool use_log,
double lmax)
265 accw = std::accumulate(w, w + n, accw);
267 internal::cblas_ddot(static_cast<VSMC_BLAS_INT>(n), w, 1, w, 1);
295 double ess()
const {
return std::numeric_limits<double>::quiet_NaN(); }
297 const double *
data()
const {
return nullptr; }
299 template <
typename OutputIter>
304 template <
typename RandomIter>
311 template <
typename InputIter>
316 template <
typename RandomIter>
317 void set(RandomIter, int)
321 template <
typename InputIter>
326 template <
typename RandomIter>
331 template <
typename InputIter>
336 template <
typename RandomIter>
341 template <
typename InputIter>
346 template <
typename RandomIter>
351 template <
typename RNGType>
364 #endif // VSMC_CORE_WEIGHT_HPP #define VSMC_DEFINE_TYPE_DISPATCH_TRAIT(Outer, Inner, Default)
std::vector< T, Alloc > Vector
std::vector with Allocator as default allocator
double ess() const
Return the ESS of the particle system.
size_type draw(RNGType &) const
OutputIter read_weight(OutputIter first) const
Read all normalized weights to an output iterator.
void mul(std::size_t n, const float *a, const float *b, float *y)
void add_log(RandomIter first, int stride)
Set .
void add_log(const double *first)
Set .
void set_log(InputIter first)
Set .
size_type draw(RNGType &rng) const
Draw integer index in the range according to the weights.
void shrink_to_fit()
Shrink to fit.
void set_log(RandomIter first, int stride)
Set .
void add_log(RandomIter, int)
RandomIter read_weight(RandomIter first, int stride) const
Read all normalized weights to a random access iterator.
void add_log(double *first)
Set .
void mul(InputIter first)
Set .
void mul(RandomIter, int)
void set_log(RandomIter, int)
void read_weight(OutputIter) const
void exp(std::size_t n, const float *a, float *y)
const double * data() const
void resize(size_type N)
Resize the Weight object.
An empty weight set class.
void reserve(size_type N)
Reserve space.
size_type size() const
Size of this Weight object.
void sub(std::size_t n, const float *a, const float *b, float *y)
typename WeightTypeTrait< T >::type WeightType
void mul(const double *first)
Set .
void add(std::size_t n, const float *a, const float *b, float *y)
void read_weight(RandomIter, int) const
void mul(double *first)
Set .
const double * data() const
Pointer to data of the normalized weight.
void log(std::size_t n, const float *a, float *y)
void mul(RandomIter first, int stride)
Set .
void add_log(InputIter first)
Set .