32 #ifndef VSMC_CORE_WEIGHT_HPP 33 #define VSMC_CORE_WEIGHT_HPP 43 inline double weight_ess(std::size_t N,
const double *first)
46 ::cblas_ddot(static_cast<VSMC_CBLAS_INT>(N), first, 1, first, 1);
53 mul(N, 1 / std::accumulate(first, first + N, 0.0), first, first);
60 double wmax = *(std::max_element(first, first + N));
61 for (std::size_t i = 0; i != N; ++i)
78 double ess()
const {
return ess_; }
80 const double *
data()
const {
return data_.data(); }
84 template <
typename OutputIter>
87 std::copy(data_.begin(), data_.end(), first);
90 template <
typename RandomIter>
101 std::fill(data_.begin(), data_.end(), 1.0 /
resample_size());
105 template <
typename InputIter>
106 void set(InputIter first)
108 std::copy_n(first,
size(), data_.begin());
112 template <
typename RandomIter>
113 void set(RandomIter first,
int stride)
120 template <
typename InputIter>
128 void mul(
const double *first)
134 void mul(
double *first) {
mul(const_cast<const double *>(first)); }
136 template <
typename RandomIter>
137 void mul(RandomIter first,
int stride)
144 template <
typename InputIter>
147 std::copy_n(first,
size(), data_.begin());
151 template <
typename RandomIter>
159 template <
typename InputIter>
162 log(
size(), data_.data(), data_.data());
170 log(
size(), data_.data(), data_.data());
171 add(
size(), data_.data(), first, data_.data());
177 template <
typename RandomIter>
180 log(
size(), data_.data(), data_.data());
186 template <
typename URNG>
189 return draw_(eng, data_.begin(), data_.end(),
true);
200 void post_set() { ess_ = normalize(
false); }
205 ess_ = normalize(
true);
208 double normalize(
bool use_log)
210 double *w = data_.data();
212 const std::size_t k = 1024;
213 const std::size_t m =
size() / k;
214 const std::size_t l =
size() % k;
215 for (std::size_t i = 0; i != m; ++i, w += k)
216 normalize_eval(k, w, accw, use_log);
217 normalize_eval(l, w, accw, use_log);
220 return 1 / cblas_ddot(static_cast<VSMC_CBLAS_INT>(
size()),
221 data_.data(), 1, data_.data(), 1);
224 void normalize_eval(std::size_t n,
double *w,
double &accw,
bool use_log)
228 accw = std::accumulate(w, w + n, accw);
252 double ess()
const {
return std::numeric_limits<double>::quiet_NaN(); }
256 const double *
data()
const {
return nullptr; }
258 template <
typename OutputIter>
263 template <
typename RandomIter>
272 template <
typename InputIter>
277 template <
typename RandomIter>
278 void set(RandomIter, int)
282 template <
typename InputIter>
287 template <
typename RandomIter>
292 template <
typename InputIter>
297 template <
typename RandomIter>
302 template <
typename InputIter>
307 template <
typename RandomIter>
312 template <
typename URNG>
325 #endif // VSMC_CORE_WEIGHT_HPP #define VSMC_DEFINE_TYPE_DISPATCH_TRAIT(Outer, Inner, Default)
typename std::conditional< std::is_scalar< T >::value, AlignedVector< T >, std::vector< T >>::type Vector
AlignedVector for scalar type and std::vector for others.
void mul(std::size_t n, const float *a, const float *b, float *y)
void add_log(RandomIter first, int stride)
void add_log(const double *first)
void read_weight(RandomIter first, int stride) const
void set_log(InputIter first)
void weight_normalize_log(std::size_t N, double *first)
Normalize logarithm weights such that the maximum is zero.
void set_log(RandomIter first, int stride)
void add_log(RandomIter, int)
void add_log(double *first)
size_type draw(URNG &) const
void mul(InputIter first)
void mul(RandomIter, int)
void set_log(RandomIter, int)
void read_resample_weight(double *) const
void read_weight(OutputIter) const
const double * resample_data() const
void exp(std::size_t n, const float *a, float *y)
size_type draw(URNG &eng) const
const double * resample_data() const
const double * data() const
void weight_normalize(std::size_t N, double *first)
Normalize weights such that the summation is one.
An empty weight set class.
typename WeightTypeTrait< T >::type WeightType
void mul(const double *first)
void add(std::size_t n, const float *a, const float *b, float *y)
void read_weight(RandomIter, int) const
void read_resample_weight(double *first) const
double weight_ess(std::size_t N, const double *first)
Compute the ess given normalized weights.
const double * data() const
size_type resample_size() const
size_type resample_size() const
void read_weight(OutputIter first) const
void log(std::size_t n, const float *a, float *y)
void mul(RandomIter first, int stride)
void add_log(InputIter first)