Using Complex C++ Class Header <complex>

In summary: However, you cannot change the real or imaginary parts of the complex number after you have defined it.
  • #1
sam2
22
0
Hi,

Has anyone here used the complex C++ class before Header <complex>? I am trying to do something VERY straightforward but there doesn't 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 I am parts to 1.

//Now I want to change the real and imaginary parts to 2, 2

A.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 form

double& complex<double>::real();

Thanks,
 
Technology news on Phys.org
  • #2
sam2 said:
Hi,
Has anyone here used the complex C++ class before Header <complex>? I am trying to do something VERY straightforward but there doesn't 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 I am parts to 1.
//Now I want to change the real and imaginary parts to 2, 2
A.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 form
double& complex<double>::real();
Thanks,

A.real() and its counterpart for the imaginary portion should be private rather than public attributes. There should be a set and get method, or "+", "-", "/" and "*" should be overridden so you can add another complex number to it.
 
  • #3
To quote Microsoft's documentation:

This template function cannot be used to modify the real part of the complex number. To change the real part, a new complex number must be assigned the component value.


Now, to allay your fears about efficiency, if the implementation and compiler are any good, the expression:

a = complex<int>(5, imag(a));

should be just as efficient as if you could write

real(a) = 5;

.
 
  • #4
It seems silly that you can not access the real and imaginary parts of the class via a function of the form

Such a class is called an immutable class. They can be useful in certain circumstances and may help the compiler with optimisation.
 

FAQ: Using Complex C++ Class Header <complex>

What is the complex class in C++?

The complex class in C++ is a built-in class that allows for operations on complex numbers, which are numbers with both real and imaginary components. It is defined in the header file and is part of the standard library.

How do I declare a complex variable in C++?

To declare a complex variable in C++, you can use the syntax: std::complex variable_name; For example, std::complex cNum; will declare a complex variable named cNum with a data type of double.

What operations can be performed on complex numbers using the complex class?

The complex class in C++ allows for various operations on complex numbers, such as addition, subtraction, multiplication, division, and comparison. It also provides functions for calculating the real and imaginary parts, conjugate, magnitude, phase, and more.

Can I use the complex class with custom data types?

Yes, you can use the complex class with custom data types as long as they support the necessary operators and functions for complex numbers. You can also define your own overloaded operators and functions to work with the complex class.

How do I access the real and imaginary parts of a complex variable?

To access the real and imaginary parts of a complex variable, you can use the real() and imag() functions, respectively. For example, cNum.real() will return the real part of the complex variable cNum.

Similar threads

Replies
5
Views
2K
Replies
35
Views
3K
Replies
23
Views
2K
Replies
4
Views
2K
Replies
25
Views
2K
Replies
17
Views
2K
Replies
6
Views
10K
Back
Top