473,320 Members | 1,991 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,320 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 6298
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 - - - - - - - - -...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.