Boost logo

Boost :

Subject: Re: [boost] [accumulators] Help understanding implementation of weighted accumulators
From: Eric Niebler (eniebler_at_[hidden])
Date: 2013-05-01 20:23:47


On 5/1/2013 1:13 PM, Gabriel Redner wrote:
> Hi folks,
>
> I am trying to implement a weighted accumulator, but I'm having
> trouble understanding a few things about the existing ones.
>
> Looking at the implementation of weighted_sum, I see (weighted_sum.hpp:37):
>
> template<typename Args>
> weighted_sum_impl(Args const &args)
> : weighted_sum_(
> args[parameter::keyword<Tag>::get() | Sample()]
> * numeric::one<Weight>::value
> )
> {
> }
>
> My questions are:
>
> - It's taking the first sample from the args and multiplying it by a
> weight of 1. Isn't this wrong - ignoring the real weight of this
> sample? Shouldn't this produce incorrect results?

It's not taking the first sample. It's (optionally) taking an element
representing zero. In most cases, a default-constructed Sample() is
satisfactory for a zero element, but in some cases it's not. Consider
what happens if your Sample type is std::vector<int>, and all your
samples are going to be 3-element vectors. Then your zero is a
std::vector<int> with 3 zeros. Any other starting value would cause all
future computation to fail.

And we multiply by one to make the type promotion come out right.

> - I've never understood anyways how there can be an initial argument
> during construction of the accumulator. I haven't found any
> explanation in the docs beyond "Maybe there is an initial value in the
> argument pack". Maybe? How?

I hope that clears it up.

-- 
Eric Niebler
Boost.org
http://www.boost.org

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk