Serving the Quantitative Finance Community

 
User avatar
sam
Topic Author
Posts: 2
Joined: December 5th, 2001, 12:04 pm

complex C++ class

October 31st, 2005, 11:47 am

Hi,Has anyone here used the complex C++ class before Header <complex>? I am trying to do something VERY straightforward but there doesnt seem to be any way to do it!Basically, I define a complex, and then want to re-asign its real and imaginary parts:complex<double> A(1,1); // assign real and im parts to 1.//Now I want to change the real and imaginary parts to 2, 2A.real() = 2; //error//The only way I can make it work is A = complex<double> (2,2);//but I am guessing that this is much less efficient because you are basically recalling the complex constructor followed by the assignment operator! Does anyone know how to do this? It seems silly that you can not access the real and imaginary parts of the class via a function of the formdouble& complex<double>::real();Thanks,
 
User avatar
ldrage
Posts: 0
Joined: July 30th, 2004, 12:58 pm

complex C++ class

October 31st, 2005, 12:11 pm

QuoteBasically, I define a complex, and then want to re-asign its real and imaginary parts:complex<double> A(1,1); // assign real and im parts to 1.//Now I want to change the real and imaginary parts to 2, 2A.real() = 2; //errorThe correct syntax is A.real(2);lee
 
User avatar
sam
Topic Author
Posts: 2
Joined: December 5th, 2001, 12:04 pm

complex C++ class

October 31st, 2005, 12:14 pm

I knew I was missing something!Many Thanks Lee.Regards,
 
User avatar
MattF
Posts: 6
Joined: March 14th, 2003, 7:15 pm

complex C++ class

October 31st, 2005, 12:16 pm

QuoteOriginally posted by: samHi,Has anyone here used the complex C++ class before Header <complex>? I am trying to do something VERY straightforward but there doesnt seem to be any way to do it!Basically, I define a complex, and then want to re-asign its real and imaginary parts:complex<double> A(1,1); // assign real and im parts to 1.//Now I want to change the real and imaginary parts to 2, 2A.real() = 2; //error//The only way I can make it work is A = complex<double> (2,2);//but I am guessing that this is much less efficient because you are basically recalling the complex constructor followed by the assignment operator! Does anyone know how to do this? Thanks,From Josuttis "The C++ Standard Library" talking about the real() and imag() access functions: "Note that the return value is not a reference. Thus, you can't use these functions to modify the real or imaginary parts. To change only the real part or only the imaginary part you must assign a new complex number."So you'd have to create a new complex number with the value you wanted to set and the other value initialised from the current state.QuoteIt seems silly that you can not access the real and imaginary parts of the class via a function of the formdouble& complex<double>::real();I completely agree ...
 
User avatar
ldrage
Posts: 0
Joined: July 30th, 2004, 12:58 pm

complex C++ class

October 31st, 2005, 1:30 pm

I'm not a C++ expert so maybe I'm missing something but I looked at the overloads for real(..) in complex<_Ty> and there are two (at least in VC++): _Ty real() _Ty real(const _Ty &right)The attached code produced the output:<1,1><2,2>and so it looks like I'm using member functions correctly. Let me know if you disagree - I'm always keen to learn.
Last edited by ldrage on October 30th, 2005, 11:00 pm, edited 1 time in total.
 
User avatar
ckarakus
Posts: 1
Joined: October 28th, 2004, 8:05 am

complex C++ class

October 31st, 2005, 2:06 pm

QuoteOriginally posted by: ldrageI'm not a C++ expert so maybe I'm missing something but I looked at the overloads for real(..) in complex<_Ty> and there are two (at least in VC++): _Ty real() _Ty real(const _Ty &right)As you see, return is by value. You cannot assign to primitive values returned by a function.If it was _Ty& real()then you can write s.t likex.real() = 3
 
User avatar
ldrage
Posts: 0
Joined: July 30th, 2004, 12:58 pm

complex C++ class

October 31st, 2005, 2:57 pm

That's understood but my point was that you can instead use x.real(2.0) in the absence of Ty& real() and so there is no need to assign a new complex number.
 
User avatar
sam
Topic Author
Posts: 2
Joined: December 5th, 2001, 12:04 pm

complex C++ class

October 31st, 2005, 3:03 pm

A.real(2) does work and it sets the real part to 2. I glanced over the header file where the class and its members/methods are declared and there isnt an overloaded version of the A.real() function which returns by reference, hence what I was initially trying to do was failing...Thanks,
 
User avatar
Alan
Posts: 3050
Joined: December 19th, 2001, 4:01 am
Location: California
Contact:

complex C++ class

October 31st, 2005, 3:21 pm

A=2.0+2.0*I