473,503 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

maxima & minima

Hello group

Is there a library which can help in doing the task below or do I need
to write something up?

I need to get the index of the relative “local” maximas and minimas in a
sequence or real numbers. That is when the derivative is zero and there
is a change in direction.

Thanks
Jul 23 '05 #1
9 6966
* Baloff:

Is there a library which can help in doing the task below or do I need
to write something up?
It's almost guaranteed to be less work to write your own code than
to find, install and figure out a library.

I need to get the index of the relative “local” maximas and minimas in a
sequence or real numbers.
The indices, surely?

That is when the derivative is zero and there
is a change in direction.


Uhm, a sequence doesn't have a derivate, but taking that as a figure of
speech: just loop over the numbers and note every change and zero in the
first difference.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2

"Baloff" <vd****@bi.edu.gr> wrote in message
news:42********@news.iprimus.com.au...
Hello group

Is there a library which can help in doing the task below or do I need to
write something up?

I need to get the index of the relative “local” maximas and minimas in a
sequence or real numbers. That is when the derivative is zero and there
is a change in direction.

Thanks


You can iterate over the sequence of values and calculate the derivate
easily using the finite difference formula. Then just observe a change in
the sign of two consecutive derivates and you have you minimum or maximum.
However, if you want to classify them you'll have to calculate the 2nd
derivate too and in this case I'd suggest to calculate the derivate not
using a linear connection of two points but rather a parabola fitted with
three points of your sequence.

Cheers
Chris
Jul 23 '05 #3
"Baloff" <vd****@bi.edu.gr> wrote in message
news:42********@news.iprimus.com.au
Hello group

Is there a library which can help in doing the task below or do I need
to write something up?

I need to get the index of the relative “local” maximas and minimas
in a sequence or real numbers. That is when the derivative is zero
and there is a change in direction.

Thanks


Just for fun, I have written some quick code to solve your problem. It seems
to work, but I suggest you test it more thoroughly than I have.

It defines a template class Optima. The template parameter is a pointer or
iterator of the type needed to traverse the container storing the real
numbers. You declare an instance of this class and supply to its constructor
two arguments:

1. A pointer/iterator to the first real number to be tested.
2. A pointer/iterator to one past the last real number to be tested or a
count of the number of objects to be tested. The second alternative only
works with pointers/random access iterators.

The constructor creates two vectors of pointers/iterators. The members of
the first vector point to the elements that are minima. The members of the
second vector point to the elements that are maxima. References to these two
vectors can be retrieved from an Optima object with the member functions
MinIndices() and MaxIndices().

The class and a sample use are given below. It is assumed that the real
numbers are stored as doubles.

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <vector>
#include <functional>
using namespace std;

enum OptType {MIN, MAX};

template<typename Iterator>
class Optima
{
public:
Optima(Iterator a_start, int size) : start(a_start),
finish(a_start+size)
{
FindAllOptima();
}
Optima(Iterator a_start, Iterator a_finish)
: start(a_start), finish(a_finish)
{
FindAllOptima();
}

const vector<Iterator> & MinIndices()
{
return optIndex[MIN];
}
const vector<Iterator> & MaxIndices()
{
return optIndex[MAX];
}
private:
Iterator it, itnext, start, finish;
vector<Iterator> optIndex[2];
void FindAllOptima();
void FindFirstOptima();
template<OptType ot, typename Comparison>
void FindNextOptima();
void FindNextMin() { FindNextOptima<MIN, less<double> > ();}
void FindNextMax() { FindNextOptima<MAX, greater<double> > ();}
};

/////////// Use of class (member definitions come later) ///////////

const int size = 200;
double g_array[size];
typedef double * ItType;

int main()
{
for(int i=0; i<size; ++i)
g_array[i] = rand();

ItType itstart = g_array;
ItType itfinish = g_array+size;
Optima<ItType> opt(itstart, itfinish);

// print out the results;
const vector<ItType>& maxIndices = opt.MaxIndices();
const vector<ItType>& minIndices = opt.MinIndices();

size_t imax=0, imin=0;

for(ItType it = itstart; it != itfinish; ++it)
{
// first the number
cout << setw(7) << *it;
// now show if it's a local max
if(it == maxIndices[imax])
{
cout << " Max";
if(imax < maxIndices.size()-1)
++imax;
}
// now show if it's a local min
if(it == minIndices[imin])
{
cout << " Min";
if(imin < minIndices.size()-1)
++imin;
}
cout << endl;
}
}

///// Member function definitons ////////////////////////

template<typename Iterator>
void Optima<Iterator>::FindFirstOptima()
{
// first check for boundary min/max
if(*it < *itnext)
optIndex[MIN].push_back(it);
else if(*it > *itnext)
optIndex[MAX].push_back(it);
else // constant section initially
{
// get past the constant section
while(itnext != finish && *it == *itnext)
{
++it, ++itnext;
}

if(itnext == finish) // constant to the end
{
for(Iterator k = start; k != itnext; ++k)
{
optIndex[MIN].push_back(k);
optIndex[MAX].push_back(k);
}
}
else if(*it < *itnext) // constant then increasing => min
{
for(Iterator k = start; k != itnext; ++k)
{
optIndex[MIN].push_back(k);
}
}
else // constant then decreasing => max
{
for(Iterator k = start; k != itnext; ++k)
{
optIndex[MAX].push_back(k);
}
}
}
}

template<typename Iterator>
template<OptType ot, typename Comparison>
void Optima<Iterator>::FindNextOptima()
{
static Comparison comp;
// get past changing section
while(itnext != finish && comp(*itnext, *it))
{
++it, ++itnext;
}

if(itnext == finish) // changing section lasts to the end
optIndex[ot].push_back(it); // record boundary optimum
else // reach a constant section
{
// first optimum index
Iterator localStart = it;
// get to end of constant section
while(itnext != finish && *itnext == *it)
{
++it, ++itnext;
}
if(itnext == finish // constant to the end
|| comp(*it, *itnext)) // sign reversal in differences
{
for(Iterator k = localStart; k != itnext; ++k)
optIndex[ot].push_back(k);
}
// else differences revert to previous value so it's an inflection
}
}

template<typename Iterator>
void Optima<Iterator>::FindAllOptima()
{
it = start;
itnext = start;
++itnext;

FindFirstOptima();
while(itnext != finish)
{
if(*it < *itnext)
FindNextMax();
else
FindNextMin();
}
}
--
John Carson
Jul 23 '05 #4

"Baloff" <vd****@bi.edu.gr> wrote in message
news:42********@news.iprimus.com.au...
Hello group

Is there a library which can help in doing the task below or do I need to
write something up?

I need to get the index of the relative local maximas and minimas in a
sequence or real numbers. That is when the derivative is zero and there
is a change in direction.

Thanks


As Alf mentioned, there is no derivative of a sequence. You'd first need to
compute a formula for an equation which satisfies the sequence before you
could compute a derivative. And there are an infinite number of functions
which satisfy any given sequence of points, so there may be any number of
local maximas and minimas.

Besides, it's really redundant to state that the derivative must be zero,
because, given any continuous function of x which maps to the sequence, then
any time the y direction changes, at at least one location within the
sequence of three points used to detect a direction change, the derivative
must equal zero. Consider the values 4,2,3 (at x values of 1,2,3, let's
say). The function is tending downward from 4 to 2, then upward from 2 to
3. So, at at least one location, if the function is continuous, the curve
must have changed directions, and thus has a derivative of zero. But
there's no way of telling *where* any of those possible changes occurred.
However, since you are asked only to find the index, you can at least narrow
it down to "somewhere in the vicinity of x=2". It could actually be
arbitrarily close to x=1 or x=3, but since you have no way to tell without a
formula for the function, you might as well choose the "inflection" point,
or x=2.

As a general rule, then, simply check every set of three consecutive points,
and report the value of x for every x for which both its neighbors are
either greater or smaller:

for (int x = 1; x < (N-1); ++x)
if ((f[x-1] < f[x]) && (f[x] > f[x+1]))
ReportLocalMaxima(x,f[x]);
else if ((f[x-1] > f[x]) && (f[x] < f[x+1]))
ReportLocalMinima(x,f[x]);

-Howard

Jul 23 '05 #5
Howard wrote:


As a general rule, then, simply check every set of three consecutive points,
and report the value of x for every x for which both its neighbors are
either greater or smaller:


But take care to report a sequence such as

4, 2, 2, 3

as a local minimum!
In other words: just by looking at only 3 points in sequence, you will
miss some minima/maxima.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #6

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:42***************@gascad.at...
Howard wrote:


As a general rule, then, simply check every set of three consecutive
points,
and report the value of x for every x for which both its neighbors are
either greater or smaller:


But take care to report a sequence such as

4, 2, 2, 3

as a local minimum!
In other words: just by looking at only 3 points in sequence, you will
miss some minima/maxima.

--
Karl Heinz Buchegger
kb******@gascad.at


Ooh! Good point! Blows my simple loop out of the water, huh? :-) But if
the above is allowed, then what would you report as the index of the
minimum? I think that question needs to be addressed to whoever made the
spec's in the first place. (A college instructor?)

-Howard

Jul 23 '05 #7
"Howard" <al*****@hotmail.com> wrote in message
news:X9*******************@bgtnsc04-news.ops.worldnet.att.net
"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:42***************@gascad.at...

But take care to report a sequence such as

4, 2, 2, 3

as a local minimum!
In other words: just by looking at only 3 points in sequence, you
will miss some minima/maxima.

--
Karl Heinz Buchegger
kb******@gascad.at


Ooh! Good point! Blows my simple loop out of the water, huh? :-) But if
the above is allowed, then what would you report as the index
of the minimum? I think that question needs to be addressed to
whoever made the spec's in the first place. (A college instructor?)


There is nothing to say that a local maximum or minimum must be a *strict*
local maximum/minimum even when a continuous function domain is being
considered. Functions can easily have horizontal sections in which you have
a continuum of optima, with all points interior to the horizontal section
having the same value as all points in a sufficiently small neighbourhood of
them. In the case of 4, 2, 2, 3, the indices of both '2's should be
recorded.

--
John Carson

Jul 23 '05 #8
"John Carson" <jc****************@netspace.net.au> wrote in message
news:d7**********@otis.netspace.net.au

The constructor creates two vectors of pointers/iterators. The
members of the first vector point to the elements that are minima.
The members of the second vector point to the elements that are
maxima.


I did this for generality. If you are working with C-style arrays or C++
vectors and want indices, then you can get them as follows.

If working with pointers, then you can get indices from the pointers by
simply subtracting the pointer to the zero element from the pointer to the
element of interest, e.g.,

int indexOfMax = ptrToMax - ptrToZeroElement;

If you are working with vectors from the standard library and the associated
iterators, say:

vector<double> vec(N);
vector<double>::iterator itr;

then if itr points to a maximum, the associated index is

int indexOfMax = itr - vec.begin();

--
John Carson

Jul 23 '05 #9
Baloff wrote:
Hello group

Is there a library which can help in doing the task below or do I need
to write something up?

I need to get the index of the relative “local” maximas and minimas in a
sequence or real numbers. That is when the derivative is zero and there
is a change in direction.

Thanks


Look at GNU Scientific Library.
Jul 23 '05 #10

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

Similar topics

1
11417
by: DrTebi | last post by:
Hello, I have the following problem: I used to "encode" my email address within links, in order to avoid (most) email spiders. So I had a link like this: <a...
0
2407
by: Thomas Scheffler | last post by:
Hi, I runned in trouble using XALAN for XSL-Transformation. The following snipplet show what I mean: <a href="http://blah.com/?test=test&amp;test2=test2">Test1&amp;</a> <a...
4
3203
by: johkar | last post by:
When the output method is set to xml, even though I have CDATA around my JavaScript, the operaters of && and < are converted to XML character entities which causes errors in my JavaScript. I know...
11
6381
by: Jeremy | last post by:
How can one stop a browser from converting &amp; to & ? We have a textarea in our system wehre a user can type in some html code and have it saved to the database. When the data is retireved...
14
5907
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
12
10059
by: InvalidLastName | last post by:
We have been used XslTransform. .NET 1.1, for transform XML document, Dataset with xsl to HTML. Some of these html contents contain javascript and links. For example: // javascript if (a &gt; b)...
7
4598
by: John Nagle | last post by:
I've been parsing existing HTML with BeautifulSoup, and occasionally hit content which has something like "Design & Advertising", that is, an "&" instead of an "&amp;". Is there some way I can get...
88
3662
by: santosh | last post by:
Hello all, In K&R2 one exercise asks the reader to compute and print the limits for the basic integer types. This is trivial for unsigned types. But is it possible for signed types without...
0
7273
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7322
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5572
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4667
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3161
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
374
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.