473,407 Members | 2,598 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,407 software developers and data experts.

std::max_element real value complex sequence

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

May 25 '06 #1
5 3136
ma740988 wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 25 '06 #2

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.

May 25 '06 #3
ma740988 wrote:
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.


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

May 25 '06 #4
ma740988 wrote:
[..]
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 25 '06 #5

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

May 25 '06 #6

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

Similar topics

1
by: Siemel Naran | last post by:
Hi. To find the maximum element in a container using the predicate form, do we supply a less predicate (one whose operator()(lhs, rhs) returns true if lhs < rhs), or a more predicate (one whose...
2
by: Marcin Kalicinski | last post by:
Hi, I found out that default constructor of std::complex class initializes the value to (0,0). I wonder why is it so? Because of this, the code below is about two times slower than it could be...
4
by: Steven T. Hatton | last post by:
This code works for dividing each element of a boost::array<> by a value of its element type: template <typename T, size_t S> inline boost::array<T, S>& operator/=( boost::array<T, S>& lhs,...
4
by: zender | last post by:
Hi, Is it possible to a value separately to the real or imaginary part of a complex number? Any help appreciated, Charlie The following code shows that .real() and .imag() are not lvalues:
6
by: bluekite2000 | last post by:
I want to accomplish the following void foo(const T &v1, const T &v2) { if(v1&v2 is of type double) double alpha=1.0; else if (v1&v2 is of type complex double) complex double alpha=(1.0,1.0);...
2
by: PengYu.UT | last post by:
Hi, In FFTW (http://www.fftw.org/), it defines the funciton fftw_malloc to allocate memory properly aligned. However, I only want to use new to allocate memory for std::complex<double>. Can...
4
by: Fredy Halter | last post by:
the following code is not working: std::complex<long doublex(1.,1.); std::complex<long doubleresult(0.,0.); result = 1./x; std::cout << "x = " << x << std::endl; std::cout << "r = " <<...
7
by: huili80 | last post by:
Should complex<T>::real() and imag() return a value or a refernce? What does the standard say about this? I just realized that MSVC2008's implementation returns a value, but in GCC reference is...
4
by: Peng Yu | last post by:
Hi, I'm wondering how to get the cubic root for a complex number? It seems that cbrt does not work complex numbers. Thanks, Peng #include <complex> #include <iostream>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.