<commit_before>#ifndef VSMC_INTERNAL_WEIGHT_HPP
#define VSMC_INTERNAL_WEIGHT_HPP

#include <vsmc/internal/common.hpp>

namespace vsmc { namespace internal {

/// \brief Base weight set
class WeightBase
{
    public :

    /// The type of the size of weight and log weight vectors
    typedef VSMC_SIZE_TYPE size_type;

    /// The type of the weight and log weight vectors
    typedef Eigen::VectorXd weight_type;

    explicit WeightBase (size_type N) :
        size_(N), ess_(static_cast<double>(N)), weight_(N), log_weight_(N),
        ess_cached_(false), weight_cached_(false), log_weight_cached_(false),
        zconst_(0), inc_weight_(N) {}

    /// Size of the weight set
    size_type size () const
    {
        return size_;
    }

    /// Read only access to the weights
    const weight_type &weight () const
    {
        if (!weight_cached_) {
            weight_ = log_weight().array().exp();
            double sum = weight_.sum();
            weight_ *= 1 / sum;
            weight_cached_ = true;
        }

        return weight_;
    }

    /// Read only access to individua log weight
    double log_weight (size_type id) const
    {
        return log_weight()[id];
    }

    /// Read only access to the log weights
    const weight_type &log_weight () const
    {
        if (!log_weight_cached_) {
            double max_weight = log_weight_.maxCoeff();
            log_weight_ = log_weight_.array() - max_weight;
            log_weight_cached_ = true;
        }

        return log_weight_;
    }

    /// Set equal weights for all particles
    void set_equal_weight ()
    {
        ess_ = static_cast<double>(size_);
        weight_.setConstant(1.0 / size_);
        log_weight_.setConstant(0);

        ess_cached_ = true;
        weight_cached_ = true;
        log_weight_cached_ = true;
    }

    /// \brief Set the log weights with a pointer
    ///
    /// \param nw The position to start the reading, it shall be valid
    /// after increments of size() times.
    /// \param delta A multiplier appiled to the new log weights
    void set_log_weight (const double *nw, double delta = 1)
    {
        Eigen::Map<const weight_type> w(nw, size_);
        set_log_weight(w, delta);
    }

    /// \brief Set the log weights with a Eigen object
    ///
    /// \param nw An Eigen::DenseBase object. One dimension Array, Vector,
    /// RowVector are supported. The Scalar type also need to be \c double.
    /// Otherwise it will be a compile-time error
    /// \param delta A multiplier appiled to the new log weights
    template <typename D>
    void set_log_weight (const Eigen::DenseBase<D> &nw, double delta = 1)
    {
        log_weight_ = nw.head(size_);
        if (delta != 1)
            log_weight_ *= delta;
        set_log_weight();
    }

    /// \brief Add to the log weights with a pointer
    ///
    /// \param iw The position to start the reading, it shall be valid
    /// after increments of size() times.
    /// \param delta A multiplier appiled to the new incremental log weights
    /// \param add_zconst Whether this incremental weights shall contribute to
    /// the SMC normalizing constant estimate
    void add_log_weight (const double *iw, double delta = 1,
            bool add_zconst = true)
    {
        Eigen::Map<const weight_type> w(iw, size_);
        add_log_weight(w, delta, add_zconst);
    }

    /// \brief Add to the log weights with a weight_object object
    ///
    /// \param nw An Eigen::DenseBase object. One dimension Array, Vector,
    /// RowVector are supported. The Scalar type also need to be \c double.
    /// Otherwise it will be a compile-time error
    /// \param delta A multiplier appiled to the new incremental log weights
    /// \param add_zconst Whether this incremental weights shall contribute to<commit_msg>fix typo<commit_after>#ifndef VSMC_INTERNAL_WEIGHT_HPP
#define VSMC_INTERNAL_WEIGHT_HPP

#include <vsmc/internal/common.hpp>

namespace vsmc { namespace internal {

/// \brief Base weight set
class WeightBase
{
    public :

    /// The type of the size of weight and log weight vectors
    typedef VSMC_SIZE_TYPE size_type;

    /// The type of the weight and log weight vectors
    typedef Eigen::VectorXd weight_type;

    explicit WeightBase (size_type N) :
        size_(N), ess_(static_cast<double>(N)), weight_(N), log_weight_(N),
        ess_cached_(false), weight_cached_(false), log_weight_cached_(false),
        zconst_(0), inc_weight_(N) {}

    /// Size of the weight set
    size_type size () const
    {
        return size_;
    }

    /// Read only access to the weights
    const weight_type &weight () const
    {
        if (!weight_cached_) {
            weight_ = log_weight().array().exp();
            double sum = weight_.sum();
            weight_ *= 1 / sum;
            weight_cached_ = true;
        }

        return weight_;
    }

    /// Read only access to individua log weight
    double log_weight (size_type id) const
    {
        return log_weight()[id];
    }

    /// Read only access to the log weights
    const weight_type &log_weight () const
    {
        if (!log_weight_cached_) {
            double max_weight = log_weight_.maxCoeff();
            log_weight_ = log_weight_.array() - max_weight;
            log_weight_cached_ = true;
        }

        return log_weight_;
    }

    /// Set equal weights for all particles
    void set_equal_weight ()
    {
        ess_ = static_cast<double>(size_);
        weight_.setConstant(1.0 / size_);
        log_weight_.setConstant(0);

        ess_cached_ = true;
        weight_cached_ = true;
        log_weight_cached_ = true;
    }

    /// \brief Set the log weights with a pointer
    ///
    /// \param nw The position to start the reading, it shall be valid
    /// after increments of size() times.
    /// \param delta A multiplier appiled to the new log weights
    void set_log_weight (const double *nw, double delta = 1)
    {
        Eigen::Map<const weight_type> w(nw, size_);
        set_log_weight(w, delta);
    }

    /// \brief Set the log weights with a Eigen object
    ///
    /// \param nw An Eigen::DenseBase object. One dimension Array, Vector,
    /// RowVector are supported. The Scalar type also need to be \c double.
    /// Otherwise it will be a compile-time error
    /// \param delta A multiplier appiled to the new log weights
    template <typename D>
    void set_log_weight (const Eigen::DenseBase<D> &nw, double delta = 1)
    {
        log_weight_ = nw.head(size_);
        if (delta != 1)
            log_weight_ *= delta;
        set_log_weight();
    }

    /// \brief Add to the log weights with a pointer
    ///
    /// \param iw The position to start the reading, it shall be valid
    /// after increments of size() times.
    /// \param delta A multiplier appiled to the new incremental log weights
    /// \param add_zconst Whether this incremental weights shall contribute to
    /// the SMC normalizing constant estimate
    void add_log_weight (const double *iw, double delta = 1,
            bool add_zconst = true)
    {
        Eigen::Map<const weight_type> w(iw, size_);
        add_log_weight(w, delta, add_zconst);
    }

    /// \brief Add to the log weights with a weight_object object
    ///
    /// \param iw An Eigen::DenseBase object. One dimension Array, Vector,
    /// RowVector are supported. The Scalar type also need to be \c double.
    /// Otherwise it will be a compile-time error
    /// \param delta A multiplier appiled to the new incremental log weights
    /// \param add_zconst Whether this incremental weights shall contribute to
{'fraction_non_alphanumeric': 0.09423808898805618, 'fraction_numerical': 0.0023625147657172856, 'mean_word_length': 2.2425531914893617, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '224112', 'n_tokens_mistral': 2225, 'n_tokens_neox': 2144, 'n_words': 776}