Complex numbers in C++ | Set 1
Last Updated :
09 Feb, 2023
The complex library implements the complex class to contain complex numbers in cartesian form and several functions and overloads to operate with them.
- real() – It returns the real part of the complex number.
- imag() – It returns the imaginary part of the complex number.
CPP
#include <iostream>
#include <complex>
using namespace std;
int main()
{
std::complex< double > mycomplex(10.0, 2.0);
cout << "Real part: " << real(mycomplex) << endl;
cout << "Imaginary part: " << imag(mycomplex) << endl;
return 0;
}
|
Real part: 10
Imaginary part: 2
Time Complexity: O(1)
Auxiliary Space: O(1)
- abs() – It returns the absolute of the complex number.
- arg() – It returns the argument of the complex number.
CPP
#include <iostream>
#include <complex>
using namespace std;
int main ()
{
std::complex< double > mycomplex (3.0, 4.0);
cout << "The absolute value of " << mycomplex << " is: ";
cout << abs (mycomplex) << endl;
cout << "The argument of " << mycomplex << " is: ";
cout << arg(mycomplex) << endl;
return 0;
}
|
The absolute value of (3,4) is: 5
The argument of (3,4) is: 0.927295
Time Complexity: O(1)
Auxiliary Space: O(1)
- polar() – It constructs a complex number from magnitude and phase angle. real = magnitude*cosine(phase angle) imaginary = magnitude*sine(phase angle)
CPP
#include <iostream>
#include <complex>
using namespace std;
int main ()
{
cout << "The complex whose magnitude is " << 2.0;
cout << " and phase angle is " << 0.5;
cout << " is " << polar (2.0, 0.5) << endl;
return 0;
}
|
The complex whose magnitude is 2 and phase angle is 0.5 is (1.75517,0.958851)
Time Complexity: O(1)
Auxiliary Space: O(1)
- norm() – It is used to find the norm(absolute value) of the complex number. If z = x + iy is a complex number with real part x and imaginary part y, the complex conjugate of z is defined as z'(z bar) = x – iy, and the absolute value, also called the norm, of z is defined as :
CPP
#include <iostream>
#include <complex>
using namespace std;
int main ()
{
std::complex< double > mycomplex (3.0, 4.0);
cout << "The norm of " << mycomplex << " is "
<< norm(mycomplex) <<endl;
return 0;
}
|
The norm of (3,4) is 25.
Time Complexity: O(1)
Auxiliary Space: O(1)
- conj() – It returns the conjugate of the complex number x. The conjugate of a complex number (real,imag) is (real,-imag).
CPP
#include <iostream>
using namespace std;
#include <complex>
int main ()
{
std::complex< double > mycomplex (10.0,2.0);
cout << "The conjugate of " << mycomplex << " is: ";
cout << conj(mycomplex) << endl;
return 0;
}
|
The conjugate of (10,2) is (10,-2)
Time Complexity: O(1)
Auxiliary Space: O(1)
- proj() – It returns the projection of z(complex number) onto the Riemann sphere. The projection of z is z, except for complex infinities, which are mapped to the complex value with a real component of INFINITY and an imaginary component of 0.0 or -0.0 (where supported), depending on the sign of the imaginary component of z.
CPP
#include <iostream>
using namespace std;
#include <complex>
int main()
{
std::complex< double > c1(1, 2);
cout << "proj" << c1 << " = " << proj(c1) << endl;
std::complex< double > c2(INFINITY, -1);
cout << "proj" << c2 << " = " << proj(c2) << endl;
std::complex< double > c3(0, -INFINITY);
cout << "proj" << c3 << " = " << proj(c3) << endl;
}
|
proj(1,2) = (1,2)
proj(inf,-1) = (inf,-0)
proj(0,-inf) = (inf,-0)
Time Complexity: O(1)
Auxiliary Space: O(1)
- sqrt() – Returns the square root of x using the principal branch, whose cuts are along the negative real axis.
CPP
#include <iostream>
using namespace std;
#include <complex>
int main()
{
cout << "Square root of -4 is "
<< sqrt (std::complex< double >(-4, 0)) << endl
<< "Square root of (-4,-0), the other side of the cut, is "
<< sqrt (std::complex< double >(-4, -0.0)) << endl;
}
|
Square root of -4 is (0,2)
Square root of (-4,-0), the other side of the cut, is (0,-2)
Time Complexity: O(log(n))
Auxiliary Space: O(1)
Next article: Complex numbers in C++ | Set 2
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...