Results 1 to 7 of 7

Thread: accumulate variant

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default accumulate variant

    Hello,
    could anyone explain me how do this below? I need to return the number of elements summed in way to do the mean. Is there anybody, please?
    Qt Code:
    1. class Value {
    2. public:
    3. double _value;
    4. bool _missed;
    5. bool getMissed() const { return _missed; }
    6. //other methods....
    7. };
    8. class Other {
    9. vector<Value> _values;
    10. static double sum_values(const double& accum, const Value& right) {
    11. if ( right.getMissed() ) return accum;
    12. return accum + right._value;
    13. }
    14. void computeMean() {
    15. double mean = std::accumulate( _values.begin(), _values.end(), 0.0,
    16. Other::sum_values );
    17. }
    18. };
    To copy to clipboard, switch view to plain text mode 
    Regards

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: accumulate variant

    The code is basically correct (assuming that's just part of it).
    (If it is supposed to be complete: Other is all private, the constructors are missing etc.)


    When I add that missing stuff (hint: do not forget to initialize _missed) the variable mean is assigned a meaningful value (namely the sum of my Values).

    What is the problem you're having?

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: accumulate variant

    I need sum all "valid" values (exactly what it do) BUT I need to compute the "MEAN"; so I need to know how many "values" I've summed; how know how many they?
    Regards

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: accumulate variant

    ok... you can do that by either:
    * counting those not _missed (and thus iterating twice over your vector)
    * or, by using a function object that counts both values and missed
    (* or you could use globals, which is very bad imho)

    try to replace your function by

    Qt Code:
    1. class MeanAccumulator
    2. {
    3. int counted_;
    4. double sum_;
    5. public:
    6. MeanAccumulator() : counted_(0), sum_(0) {}
    7. void operator() (const Value &right)
    8. {
    9. if ( right.getMissed() ) return;
    10. ++counted_;
    11. sum_ += right._value;
    12. }
    13. double mean() const {return counted_ ? sum_/counted_ : 0; }
    14. };
    15.  
    16. void computeMean() {
    17. MeanAccumulator res = std::for_each( _values.begin(), _values.end(),
    18. MeanAccumulator());
    19. std::cerr << "sum="<<res.mean()<<std::endl;
    20. }
    To copy to clipboard, switch view to plain text mode 

    Note that the function object passed into for_each does NOT get modified.
    The resulting function object (with the updated state) is returned by for_each.

    (I assumed you do not insist on using accumulate.)

    HTH

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: accumulate variant

    Hello,

    I was looking something that permit me to do this with accumulate....I don't know how "algorithms" works, so I was thinking that there was some trick with bind1st or other......your for_each avoid me to write a for( ; .......
    Regards

  6. #6
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: accumulate variant

    Hello,
    See this:
    Qt Code:
    1. class Value {
    2.  
    3. public:
    4. double _value;
    5. bool _missed;
    6. ...................
    7. };
    8.  
    9. class Feature {
    10. static std::vector<Value, std::allocator<Value> >&
    11. sum_vec(std::vector<Value, std::allocator<Value> >& v, const Value& right) {
    12. if ( !right.getMissed() ) {
    13. v.push_back(right);
    14. }
    15. return v;
    16. }
    17. ......................................
    18. };
    19. //main.cpp
    20. std::vector<Value, std::allocator<Value> > vVal;
    21. vVal = std::accumulate( _values.begin(), _values.end(), vVal, Feature::sum_vec ); //1
    22. _mean = std::accumulate( vVal.begin(), vVal.end() ) / vVal.size(); //2
    To copy to clipboard, switch view to plain text mode 

    //1 return vVal that is a new vector that contains only the values that must be summed; (and it is ok)
    //2 doesn't compile, because seems vVal.begin() and vVal.end() are not good; Could anyone correct this, please?

    However: this isn't the solution that I wanted; I must call 2 accumulate and push_back into a new vector;
    I hope this can be a point to reach my aim

    Regards,
    Regards

  7. #7
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: accumulate variant

    Why don't you like the for_each version?

    as an alternative (if you for some reason insist on using accumulate...):
    you can use a structure (not just a double) to hold both the sum and the number of items counted...

    something like the following (untested and uncompiled)
    Qt Code:
    1. struct mean_helper
    2. {
    3. mean_helper() : count(0), sum(0) {}
    4. int count;
    5. double sum;
    6. };
    7.  
    8. static mean_helper sum_and_count(mean_helper h, const Value &right)
    9. {
    10. if (! right.getMissed())
    11. {
    12. ++h.count;
    13. h.sum += right.value;
    14. }
    15. return h;
    16. }
    17.  
    18. mean_helper helper = std::accumulate( _values.begin(), _values.end(), mean_helper(), sum_and_count );
    19. _mean = helper.sum / helper.count;
    To copy to clipboard, switch view to plain text mode 

    HTH

  8. The following user says thank you to caduel for this useful post:

    mickey (24th August 2008)

Similar Threads

  1. Replies: 8
    Last Post: 16th July 2008, 15:05
  2. Automating Excel 97
    By Aki-Matti in forum Qt Programming
    Replies: 0
    Last Post: 14th December 2007, 07:28
  3. pixmap from variant
    By kernel_panic in forum Qt Programming
    Replies: 1
    Last Post: 30th January 2007, 11:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.