std::max_element real value complex sequence

M

ma740988

Trying to determine how to get the max element from a complex sequence.
Given:

int main()
{

//typedef std::complex < double > C
typedef std::vector < std::complex < double > > complex_vec;
typedef complex_vec::const_iterator cvect_it;
complex_vec x_vec;

x_vec.push_back ( std::complex < double > ( 1., 2. ) );
x_vec.push_back ( std::complex < double > ( 3., 4. ) );
x_vec.push_back ( std::complex < double > ( 0, 0 ) );
x_vec.push_back ( std::complex < double > ( 4., 8. ) );
x_vec.push_back ( std::complex < double > ( 0, 0 ) );

cvect_it beg = x_vec.begin();
cvect_it end = x_vec.end();

// cvect_it iter = std::max_element ( beg, end ); // wont work
obviously ..
// std::cout << *iter << std::endl;

}

I'm perusing both forms of max_element here and I'm in a quandry how to
do this. My current solution involves copying the real values to
another vector then utilizing max_element to determine the max but I
suspect a more viable solution exists. Ideas?

Thanks in advance
 
V

Victor Bazarov

ma740988 said:
Trying to determine how to get the max element from a complex
sequence.
[..noting of importance..]

I'm perusing both forms of max_element here and I'm in a quandry how
to do this. My current solution involves copying the real values to
another vector then utilizing max_element to determine the max but I
suspect a more viable solution exists. Ideas?

How do you define "max element" for a sequence of complex numbers?
IOW, how do compare two complex numbers and tell which one is "greater"?

V
 
M

ma740988

How do you define "max element" for a sequence of complex numbers?
IOW, how do compare two complex numbers and tell which one is "greater"?

I tend to think too hard when it comes to predicates. The solution is
real simple..

bool real_part ( const std::complex<double> & lhs ,
const std::complex<double> & rhs)
{
return lhs.real() < rhs.real();
}
cvect_it iter = std::max_element ( beg, end , real_part );
Found what I was looking for thanks.
 
M

mlimber

ma740988 said:
I tend to think too hard when it comes to predicates. The solution is
real simple..

bool real_part ( const std::complex<double> & lhs ,
const std::complex<double> & rhs)
{
return lhs.real() < rhs.real();
}
cvect_it iter = std::max_element ( beg, end , real_part );
Found what I was looking for thanks.

That may be fine for your application, but be aware, as Victor implied,
such a test is not general. Someone else might want to compare the
imaginary parts or their magnitudes or angles. Any could be valid
depending on the application.

Cheers! --M
 
V

Victor Bazarov

ma740988 said:
[..]
bool real_part ( const std::complex<double> & lhs ,
const std::complex<double> & rhs)
{
return lhs.real() < rhs.real();
}

A nit: naming. I'd probably call this function 'real_less', to
express the fact that it uses the less-than (<) operator. The
name you used does not explain what the nature of the comparison
is. Variation: 'real_ascending'.

V
 
M

ma740988

mlimber wrote:

|| Any could be valid depending on the application.

Indeed and - for what I'm doing - computing the Power Spectrial Density
( PSD ) of a sequence 'x' using a periodgram, the imaginaries for the
the corresponding vector of frequences is irrelevant
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top