473,385 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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)?

Oct 5 '05 #1
1 6300
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/
Oct 5 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: OutsiderJustice | last post by:
Hi All, I can not find any information if PHP support multi-thread (Posix thread) or not at all, can someone give out some information? Is it supported? If yes, where's the info? If no, is it...
37
by: ajikoe | last post by:
Hello, Is anyone has experiance in running python code to run multi thread parallel in multi processor. Is it possible ? Can python manage which cpu shoud do every thread? Sincerely Yours,...
12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
6
by: Joe | last post by:
I have 2 multi-list boxes, 1 displays course categories based on a table called CATEGORIES. This table has 2 fields CATEGORY_ID, CATEGORY_NAME The other multi-list box displays courses based on...
4
by: mimmo | last post by:
Hi! I should convert the accented letters of a string in the correspondent letters not accented. But when I compile with -Wall it give me: warning: multi-character character constant Do the...
23
by: Kaz Kylheku | last post by:
I've been reading the recent cross-posted flamewar, and read Guido's article where he posits that embedding multi-line lambdas in expressions is an unsolvable puzzle. So for the last 15 minutes...
17
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, Wide character and multi-byte character are two popular encoding schemes on Windows. And wide character is using unicode encoding scheme. But each time I feel confused when...
1
by: mknoll217 | last post by:
I am recieving this error from my code: The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part identifier...
2
by: Aussie Rules | last post by:
Hi, I have a site that Iwant to either display my text in english or french, based on the users prefernces ? I am new to webforms, but I know in winforms, this is pretty easy with a resource...
2
by: Mirco Wahab | last post by:
After reading through some (open) Intel (CPU detection) C++ source (www.intel.com/cd/ids/developer/asmo-na/eng/276611.htm) I stumbled upon a sketchy use of multibyte characters - - - - - - - - -...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.