Connecting Tech Pros Worldwide Forums | Help | Site Map

STL - Vector - Normalization ?

Rakesh Kumar
Guest
 
Posts: n/a
#1: Jul 22 '05
Hi,
Is there a function that can help me to normalize the given vector
(belonging to C++ STL) of 'double's in the range 0.0 - 1.0

i.e . given a set of 'double's, each element 'ele' would be replaced by
elenew = (ele - min) / (max - min) , where max and min are the
maximum and minimum of the given vector.

Or, aliter - is there a way , I can specify a function (here, in
this case, I could specify my normalize function) to be applied to each
and every element of a vector and get a new vector consisting of the
value returned by the function.

--
Rakesh Kumar
** Remove nospamplz from my email address for my real email **

Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 22 '05

re: STL - Vector - Normalization ?


"Rakesh Kumar" <dreamzlion_nospamplz@yahoo.com> wrote...[color=blue]
> Hi,
> Is there a function that can help me to normalize the given vector
> (belonging to C++ STL) of 'double's in the range 0.0 - 1.0
>
> i.e . given a set of 'double's, each element 'ele' would be replaced by
> elenew = (ele - min) / (max - min) , where max and min are the
> maximum and minimum of the given vector.
>
> Or, aliter - is there a way , I can specify a function (here, in
> this case, I could specify my normalize function) to be applied to each
> and every element of a vector and get a new vector consisting of the
> value returned by the function.[/color]

Aw, come on... What happened to the programmer's pride? Couldn't
you write one if you just needed it?


David Harmon
Guest
 
Posts: n/a
#3: Jul 22 '05

re: STL - Vector - Normalization ?


On Wed, 21 Apr 2004 17:25:51 -0700 in comp.lang.c++, Rakesh Kumar
<dreamzlion_nospamplz@yahoo.com> wrote,
[color=blue]
>i.e . given a set of 'double's, each element 'ele' would be replaced by
> elenew = (ele - min) / (max - min) , where max and min are the
>maximum and minimum of the given vector.[/color]

See std::min_element<>, std::max_element<>, and std::transform<>.
Stroustrup section 18.9, 18.6.2.

Dietmar Kuehl
Guest
 
Posts: n/a
#4: Jul 22 '05

re: STL - Vector - Normalization ?


"Victor Bazarov" <v.Abazarov@comAcast.net> wrote:[color=blue]
> Aw, come on... What happened to the programmer's pride? Couldn't
> you write one if you just needed it?[/color]

Actually, I think it would make sense to have a quite extensive algorithms
library which would cover, amoung other things, also many of the typical
numerical algorithms.
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
puppet_sock@hotmail.com
Guest
 
Posts: n/a
#5: Jul 22 '05

re: STL - Vector - Normalization ?


dietmar_kuehl@yahoo.com (Dietmar Kuehl) wrote in message news:<5b15f8fd.0404220222.44ef2c85@posting.google. com>...[color=blue]
> "Victor Bazarov" <v.Abazarov@comAcast.net> wrote:[color=green]
> > Aw, come on... What happened to the programmer's pride? Couldn't
> > you write one if you just needed it?[/color]
>
> Actually, I think it would make sense to have a quite extensive algorithms
> library which would cover, amoung other things, also many of the typical
> numerical algorithms.[/color]

Indeed it would. But I'm not sure it would make sense for it to
be part of the standard language. The language is already pretty
large. I think it makes more sense to have math packages available.
As well, it makes sense for such things to be tweaked for special
purposes, tweaked for specific hardware, etc.

Hey, guess what? That's the situation now. So I can be lazy.
Socks
Siemel Naran
Guest
 
Posts: n/a
#6: Jul 22 '05

re: STL - Vector - Normalization ?


"David Harmon" <source@netcom.com> wrote in message[color=blue]
> On Wed, 21 Apr 2004 17:25:51 -0700 in comp.lang.c++, Rakesh Kumar[/color]
[color=blue][color=green]
> >i.e . given a set of 'double's, each element 'ele' would be replaced by
> > elenew = (ele - min) / (max - min) , where max and min are the
> >maximum and minimum of the given vector.[/color]
>
> See std::min_element<>, std::max_element<>, and std::transform<>.
> Stroustrup section 18.9, 18.6.2.[/color]

This would be an 2*O(N) algorithm to find the min element and then the max
element. Is there a standard algorithm to find the max and min in one pass?


Pete Becker
Guest
 
Posts: n/a
#7: Jul 22 '05

re: STL - Vector - Normalization ?


Siemel Naran wrote:[color=blue]
>
> This would be an 2*O(N) algorithm to find the min element and then the max
> element.[/color]

2*O(N) == O(N).

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Rakesh Kumar
Guest
 
Posts: n/a
#8: Jul 22 '05

re: STL - Vector - Normalization ?


Thanks David for the pointers.

David Harmon wrote:[color=blue]
> See std::min_element<>, std::max_element<>, and std::transform<>.
> Stroustrup section 18.9, 18.6.2.
>[/color]

--
Rakesh Kumar
** Remove nospamplz from my email address for my real email **
Siemel Naran
Guest
 
Posts: n/a
#9: Jul 22 '05

re: STL - Vector - Normalization ?


"Pete Becker" <petebecker@acm.org> wrote in message
news:408802F2.24E74D3B@acm.org...[color=blue]
> Siemel Naran wrote:[/color]
[color=blue][color=green]
> > This would be an 2*O(N) algorithm to find the min element and then the[/color][/color]
max[color=blue][color=green]
> > element.[/color]
>
> 2*O(N) == O(N).[/color]

Sure, mathematically speaking. But what I'm trying to say is the original
algorithm performs 2 passes through the array, whereas we could get by with
just 1.


Victor Bazarov
Guest
 
Posts: n/a
#10: Jul 22 '05

re: STL - Vector - Normalization ?


"Siemel Naran" <SiemelNaran@REMOVE.att.net> wrote...[color=blue]
> "Pete Becker" <petebecker@acm.org> wrote in message
> news:408802F2.24E74D3B@acm.org...[color=green]
> > Siemel Naran wrote:[/color]
>[color=green][color=darkred]
> > > This would be an 2*O(N) algorithm to find the min element and then the[/color][/color]
> max[color=green][color=darkred]
> > > element.[/color]
> >
> > 2*O(N) == O(N).[/color]
>
> Sure, mathematically speaking. But what I'm trying to say is the original
> algorithm performs 2 passes through the array, whereas we could get by[/color]
with[color=blue]
> just 1.[/color]

But the whole point of complexity being the same is that while doing
your single path, you will have to do more on every iteration, which
basically negates the difference between one and two passes.

Victor


Siemel Naran
Guest
 
Posts: n/a
#11: Jul 22 '05

re: STL - Vector - Normalization ?


"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message news:gg0ic.5519[color=blue]
> "Siemel Naran" <SiemelNaran@REMOVE.att.net> wrote...[/color]
[color=blue][color=green]
> > Sure, mathematically speaking. But what I'm trying to say is the[/color][/color]
original[color=blue][color=green]
> > algorithm performs 2 passes through the array, whereas we could get by[/color]
> with[color=green]
> > just 1.[/color]
>
> But the whole point of complexity being the same is that while doing
> your single path, you will have to do more on every iteration, which
> basically negates the difference between one and two passes.[/color]

Sure, there's no time saved in the instructions of the body of the loop.
But there is the time saved in the loop itself. In the 2 pass algorithm for
(int i=0; i<N; i++) we do i++ and i<N a total of 2*N times. In mathematical
libraries, people are very concerned about performance.


Michiel Salters
Guest
 
Posts: n/a
#12: Jul 22 '05

re: STL - Vector - Normalization ?


"Siemel Naran" <SiemelNaran@REMOVE.att.net> wrote in message news:<sp2ic.12749$_o3.420355@bgtnsc05-news.ops.worldnet.att.net>...[color=blue]
> "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message news:gg0ic.5519[/color]
[ min_element followed by max_element ][color=blue][color=green]
> >
> > But the whole point of complexity being the same is that while doing
> > your single path, you will have to do more on every iteration, which
> > basically negates the difference between one and two passes.[/color]
>
> Sure, there's no time saved in the instructions of the body of the loop.[/color]

Actually, there is. After the first element, an element can be either the
minimum or the maximum, but not both. The first element usually is
treated as a special case anyway, so the normal case can actually use
an else:
iter = begin;
min = max = *iter; ++iter;
while( *iter != end )
if( *iter < min ) min = *iter;
else // This reduces the complexity per element.
if (*iter > max ) max = *iter;

Of course, with a random order an long sequences, the average
saving is almost 0, and if the optimizer recognizes the pattern
the net saving of the 'else' will be 0 anyway.

Regards,
Michiel Salters
Jeff
Guest
 
Posts: n/a
#13: Jul 22 '05

re: STL - Vector - Normalization ?


If this is a very large vector, and this call is in the core loop of
your code, then maybe it's reasonable to worry about the 2-pass vs.
1-pass thing. Or more accurately, a 3-pass vs. 2-pass since the
normalizing the vectors will require another pass.

Here's a one pass solution to finding the max and min:

#include <algorithm>
#include <limits.h>
using namespace std;
struct MaxMin {
int max;
int min;
MaxMin() : max(MIN_INT), min(MAX_INT) {}
inline void operator()(int n) {
if (n < min) min = n;
if (n > max) max = n;
}
};

.....
MaxMin mm;
Vector<int> myvector;
// fill myvector
foreach(myvector.begin(), myvector.end(), mm);
// Now the max and min are stored in mm.max and mm.min.

But in any case, please measure the running time of this solution vs.
the running time of the one-pass solution. I suspect there will be so
little difference that the max_element() and min_element() approach
will be preferable because it requires fewer lines of code.

"Siemel Naran" <SiemelNaran@REMOVE.att.net> wrote in message news:<sp2ic.12749$_o3.420355@bgtnsc05-news.ops.worldnet.att.net>...[color=blue]
> "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message news:gg0ic.5519[color=green]
> > "Siemel Naran" <SiemelNaran@REMOVE.att.net> wrote...[/color]
>[color=green][color=darkred]
> > > Sure, mathematically speaking. But what I'm trying to say is the[/color][/color]
> original[color=green][color=darkred]
> > > algorithm performs 2 passes through the array, whereas we could get by[/color][/color]
> with[color=green][color=darkred]
> > > just 1.[/color]
> >
> > But the whole point of complexity being the same is that while doing
> > your single path, you will have to do more on every iteration, which
> > basically negates the difference between one and two passes.[/color]
>
> Sure, there's no time saved in the instructions of the body of the loop.
> But there is the time saved in the loop itself. In the 2 pass algorithm for
> (int i=0; i<N; i++) we do i++ and i<N a total of 2*N times. In mathematical
> libraries, people are very concerned about performance.[/color]
Siemel Naran
Guest
 
Posts: n/a
#14: Jul 22 '05

re: STL - Vector - Normalization ?


"Jeff" <surferjeff@gmail.com> wrote in message
news:781dd16f.0404230707.eea2968@posting.google.co m...[color=blue]
> "Siemel Naran" <SiemelNaran@REMOVE.att.net> wrote in message[/color]
news:<sp2ic.12749$_o3.420355@bgtnsc05-news.ops.worldnet.att.net>...[color=blue][color=green]
> > "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message[/color][/color]
news:gg0ic.5519
[color=blue][color=green][color=darkred]
> > > But the whole point of complexity being the same is that while doing
> > > your single path, you will have to do more on every iteration, which
> > > basically negates the difference between one and two passes.[/color]
> >
> > Sure, there's no time saved in the instructions of the body of the loop.
> > But there is the time saved in the loop itself. In the 2 pass algorithm[/color][/color]
for[color=blue][color=green]
> > (int i=0; i<N; i++) we do i++ and i<N a total of 2*N times. In[/color][/color]
mathematical[color=blue][color=green]
> > libraries, people are very concerned about performance.[/color][/color]
[color=blue]
> If this is a very large vector, and this call is in the core loop of
> your code, then maybe it's reasonable to worry about the 2-pass vs.
> 1-pass thing. Or more accurately, a 3-pass vs. 2-pass since the
> normalizing the vectors will require another pass.
>
> Here's a one pass solution to finding the max and min:
>
> #include <algorithm>
> #include <limits.h>
> using namespace std;
> struct MaxMin {
> int max;
> int min;
> MaxMin() : max(MIN_INT), min(MAX_INT) {}
> inline void operator()(int n) {
> if (n < min) min = n;
> if (n > max) max = n;
> }
> };
>
> ....
> MaxMin mm;
> Vector<int> myvector;
> // fill myvector
> foreach(myvector.begin(), myvector.end(), mm);
> // Now the max and min are stored in mm.max and mm.min.
>
> But in any case, please measure the running time of this solution vs.
> the running time of the one-pass solution. I suspect there will be so
> little difference that the max_element() and min_element() approach
> will be preferable because it requires fewer lines of code.[/color]


So I actually timed it. Sure, if the cost of operator<(const T&, const T&)
is expensive, then the time of the for loop (namely ++i and i<N), is small
in comparison to the body of the for loop which calls operator<(const T&,
const T&). But if T is int or some other small type, as is often the case
for numerical programs, then we expect the difference to be significant.

I wrote a program that takes a command line argument. If it is "one" it
calls the one-pass method, and for "two" it calls the two-pass method. The
program creates an array of 10 million integers, fills them with random
numbers, then calls either the one-pass or two-pass method to compute the
min and max. To test the functionality of the algorithm only, and not how
well the compiler optimizes STL algorithms, I encoded the logic of the
algorithms min_element etc in the function and did not use functors.

The one pass method took 190 to 195 clock cycles (I ran it about 5 times).
The two pass method to 310 to 370 clock cycles, with a larger standard
deviation for some reason. This is a significant difference.

My platform is Windows ME, 128 MB RAM, using Borland C++ Builder version 6,
AMD Athlon processor.


#if defined(__BORLANDC__)
#include <vcl.h>
#endif

#include <string>
#include <cstdlib> // rand
#include <ctime> // clock
#include <utility> // pair
#include <iostream>
#include <cassert>


typedef std::pair<int,int> PairInt;


PairInt action1(register const int * begin, register const int *const end)
{
assert(begin != end);
register int min = *begin;
register int max = min;
for (++begin; begin!=end; ++begin)
{
const int val = *begin;
if (val<min) min=val;
else if (val>max) max=val;
}
return PairInt(min, max);
}


PairInt action2(const int *const const_begin, register const int *const end)
{
assert(const_begin != end);
register int min = *const_begin;
register int max = min;
register const int * begin = NULL;
begin = const_begin;
for (++begin; begin!=end; ++begin)
{
register const int val = *begin;
if (val<min) min=val;
}
begin = const_begin;
for (++begin; begin!=end; ++begin)
{
register const int val = *begin;
if (val>max) max=val;
}
return PairInt(min, max);
}


int main(int argc, char * * argv)
{
if (argc == 2)
{
const int N = 10000000;
int * array = new int[N];
int *const begin = array;
int *const end = array+N;

for (int * iter = begin; iter != end; ++iter) *iter = std::rand();
const std::string which = argv[1];

typedef PairInt (* PtrAction)(const int *, const int *);
PtrAction ptr = NULL;
if (which == "one") ptr = &action1;
else if (which == "two") ptr = &action2;

if (ptr)
{
using std::clock_t;
using std::clock;
const clock_t clock_start = clock();
const PairInt pair = (*ptr)(begin, end);
const clock_t clock_end = clock();

std::cout
<< "min = " << pair.first << ", "
<< "max = " << pair.second << ", "
<< "time = " << clock_end - clock_start
<< '\n'
;
}

delete[] array;
}
}


Siemel Naran
Guest
 
Posts: n/a
#15: Jul 22 '05

re: STL - Vector - Normalization ?


"Siemel Naran" <SiemelNaran@REMOVE.att.net> wrote in message
news:V1lic.15816
[color=blue]
> I wrote a program that takes a command line argument. If it is "one" it
> calls the one-pass method, and for "two" it calls the two-pass method.[/color]
The[color=blue]
> program creates an array of 10 million integers, fills them with random
> numbers, then calls either the one-pass or two-pass method to compute the
> min and max. To test the functionality of the algorithm only, and not how
> well the compiler optimizes STL algorithms, I encoded the logic of the
> algorithms min_element etc in the function and did not use functors.
>
> The one pass method took 190 to 195 clock cycles (I ran it about 5 times).
> The two pass method to 310 to 370 clock cycles, with a larger standard
> deviation for some reason. This is a significant difference.[/color]

There is something else I ran into last week when I did the test, but didn't
think much of it. My initial test used an array of 100 million integers.
But this took a very long time, and I noticed my hard disk light was on a
lot. Which means my system ran out of memory (it has only 128MB RAM) and so
was swapping memory in and out.

This is significant because it means that if operator++ or operator* is
expensive, then a one-pass method is surely preferred. This could happen if
you're short on memory and the computer has to swap RAM with disk space, or
if operator++ or operator* is intrinsically expensive as with a database
select iterator or probably even std::deque, or on some platforms where
memory access is slower (but inline arithmetic operations are really fast)
which I think is the case for SPARC machines.


Closed Thread


Similar C / C++ bytes