STL multi-dimensional vector computations on rows and columns using iterators

Discussion in 'C++' started by Kamil, Oct 5, 2005.

  1. Kamil

    Kamil Guest

    I want to sum elements of a multi-dimensional vector, across each
    vector, or across each dimension... this is my old code:

    #define v_ldouble vector<ldouble>
    #define vv_ldouble vector<vector<ldouble> >
    ....
    void vect_sum_old(const vv_ldouble& x, v_ldouble& sum, const int dir)
    {
    int v,nV,d,nD;

    nV=x.size();
    nD=x[0].size();

    if(dir!=1)
    {
    sum = v_ldouble(nV,0.0);
    for(v=0; v<nV; v++)
    for(d=0; d<nD; d++)
    sum[v]+=x[v][d];
    }
    else
    {
    sum = v_ldouble(nD,0.0);
    for(d=0; d<nD; d++)
    for(v=0; v<nV; v++)
    sum[d]+=x[v][d];
    }
    }

    Yeah I know... excuse my brute-force engineering approach. I am new to
    STL -- I switched from C to C++ because STL vectors make my life
    easier. Now I want to utilize more of what STL/C++ has to offer. I can
    compute the sum of each row vector using iterators:

    vv_ldouble::iterator iter;
    v_ldouble::iterator iter1;
    vv_ldouble mdv = vv_ldouble(3,v_ldouble(4,0.0));
    v_ldouble sumrow = v_ldouble(mdv.size());
    v_ldouble sumcol = v_ldouble(mdv[0].size());
    ....
    //Sum each vector across its elements...
    for(iter = mdv.begin(), iter1=sumrow.begin(); iter!=mdv.end(); iter++,
    iter1++)
    *iter1 = accumulate((*iter).begin(), (*iter).end(), 0.0);

    How do I go about computing sum of each column using iterators
    (efficiently)?
     
    Kamil, Oct 5, 2005
    #1
    1. Advertising

  2. Kamil wrote:

    > I want to sum elements of a multi-dimensional vector, across each
    > vector, or across each dimension... this is my old code:
    >

    <snip>
    > Yeah I know... excuse my brute-force engineering approach. I am new to
    > STL -- I switched from C to C++ because STL vectors make my life
    > easier. Now I want to utilize more of what STL/C++ has to offer. I can
    > compute the sum of each row vector using iterators:
    >
    > vv_ldouble::iterator iter;
    > v_ldouble::iterator iter1;
    > vv_ldouble mdv = vv_ldouble(3,v_ldouble(4,0.0));
    > v_ldouble sumrow = v_ldouble(mdv.size());
    > v_ldouble sumcol = v_ldouble(mdv[0].size());
    > ...
    > //Sum each vector across its elements...
    > for(iter = mdv.begin(), iter1=sumrow.begin(); iter!=mdv.end(); iter++,
    > iter1++)
    > *iter1 = accumulate((*iter).begin(), (*iter).end(), 0.0);
    >
    > How do I go about computing sum of each column using iterators
    > (efficiently)?


    Summing the columns has the problem that the data is not stored in a
    suitable way for efficient operations on the columns.
    You can try it like this:

    class SumAt {
    size_t index;
    public:
    SumAt(size_t index_init): index(index_init) {}
    ldouble operator()(const v_ldouble& row, const ldouble& accumulator)
    {
    return accumulator + row.at(index);
    }
    };

    for (size_t counter=0; counter < sumcol.size(); count++)
    {
    transform(mdv.begin(), mdv.end(), /* 1st source range */
    sumcol.begin(), /* start 2nd source */
    sumcol.begin(), /* start destination */
    SumAt(counter));
    }

    If you interrupt this loop half-way through, then sumcol will contain
    the partial sum of each column for rows 1 to N, where N is the last row
    that was processed.

    Bart v Ingen Schenau

    PS. Posting your message once is sufficient. It can take sometimes up to
    a day for messages to become visible, so be patient.
    --
    a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
    c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    c.l.c++ FAQ: http://www.parashift.com/c -faq-lite/
     
    Bart van Ingen Schenau, Oct 5, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. helpful sql
    Replies:
    0
    Views:
    830
    helpful sql
    May 19, 2005
  2. Kamil
    Replies:
    0
    Views:
    339
    Kamil
    Oct 5, 2005
  3. Kamil
    Replies:
    0
    Views:
    312
    Kamil
    Oct 5, 2005
  4. Kamil
    Replies:
    0
    Views:
    348
    Kamil
    Oct 5, 2005
  5. Replies:
    8
    Views:
    1,967
    Csaba
    Feb 18, 2006
Loading...

Share This Page