3

I'm following some thread (this, this and this) in order to calculate the rolling mean using std::vector as sample.

I've included headers of numeric::functional sublibrary in order to work with vectors

This code does not compile

#include <cstdlib>
#include <boost/accumulators/numeric/functional/vector.hpp>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/rolling_mean.hpp>
#include <boost/unordered_map.hpp>
#include <boost/smart_ptr.hpp>
#include <boost/assign/list_of.hpp>

using namespace std;
using namespace boost::accumulators;

typedef accumulator_set<vector<float>, tag::rolling_mean> RollingMeanAccumulator;
typedef boost::shared_ptr<RollingMeanAccumulator> RollingMeanAccumulatorPtr;

int main(int argc, char** argv) {

    RollingMeanAccumulatorPtr ptr = RollingMeanAccumulatorPtr(new RollingMeanAccumulator(vector<float>(2), tag::rolling_window::window_size = 4));
    const vector<float> v = boost::assign::list_of(2.0)(3.0);
    (*ptr)(v);
    rolling_mean(*(ptr));

    return 0;
}

It compiles instead if I comment this line

rolling_mean(*(ptr));

So it seems that there's a problem in retrieving value. How can I fix this?

I'm using boost 1.48.

EDIT:

@doctorlove noticed in compilation error that operator-= misses for std::Vector. I've opened boost/accumulators/numeric/functional/vector.hpp and I've seen that definition of this operator misses. So I've added it in my code changing it in this way (sorry if it's not well written):

#include <cstdlib>
#include <boost/accumulators/numeric/functional/vector.hpp>

namespace boost { namespace numeric { namespace operators {
template<typename Left>
    std::vector<Left> &
    operator -=(std::vector<Left> &left, std::vector<Left> const &right)
    {
        BOOST_ASSERT(left.size() == right.size());
        for(std::size_t i = 0, size = left.size(); i != size; ++i)
        {
            numeric::minus_assign(left[i], right[i]);
        }
        return left;
    }
}}}


#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/rolling_mean.hpp>
#include <boost/unordered_map.hpp>
#include <boost/smart_ptr.hpp>
#include <boost/assign/list_of.hpp>

using namespace std;
using namespace boost::accumulators;

typedef accumulator_set<vector<float>, tag::rolling_mean> RollingMeanAccumulator;
typedef boost::shared_ptr<RollingMeanAccumulator> RollingMeanAccumulatorPtr;

int main(int argc, char** argv) {

    RollingMeanAccumulatorPtr ptr = RollingMeanAccumulatorPtr(new RollingMeanAccumulator(vector<float>(2), tag::rolling_window::window_size = 4));
    const vector<float> v = boost::assign::list_of(2.0)(3.0);
    (*ptr)(v);
    rolling_mean(*(ptr));

    return 0;
}

I've taken the definition of operator+= in that header and adapted it.

I'm doing this way because in this post the user says that vector operators must be defined before accumulator defines.

But even in this case I'm not able to compile it.

Community
  • 1
  • 1
Jepessen
  • 11,744
  • 14
  • 82
  • 149
  • Not an answer, but if I specify `stat` in front of `tag@'rolling_mean' I get a clear error about `this->sum_ -= rolling_window_plus1(args).front();` i.e. it doesn't know how to do `-=` on a vector – doctorlove Sep 03 '14 at 10:57
  • 1
    Thanks for the bug report. I've filed the bug at https://svn.boost.org/trac/boost/ticket/10452. Patches welcome if you figure out what the problem is. – Eric Niebler Sep 03 '14 at 18:58
  • Ok thank you for the bug report. I've not reported before because I always think that when something does not work is my problem :) – Jepessen Sep 04 '14 at 07:58

0 Answers0