C++ complex polar


template<class T> complex<T> polar(const T& rho , const T& theta = 0);

The C++ <complex> ‘polar’ function returns a complex number having it’s magnitude as rho and the phase angle as theta.

Parameters:
rho -The magnitude of the complex number formed.

theta -The phase angle of the complex number formed.

Return type
complex< T > – The complex number having ‘rho’ as it’s magnitude and ‘theta’ as it’s phase angle.

Some points to note:

i) The complex number returned by this function is same as “rho*cos( theta ) + irho*sin(theta)“.

Code example

double pi=3.14159265359 ;

complex< double > c1( polar(5.0 , pi/6) ); // ‘pi/3 = π/3 = 60° or 0.523599 rad’

cout<<“c1=” << c1 << endl ; //the complex number returned by c1

cout<< “arg(c1)=” << arg( c1 ) << endl ; //the phase angle of c1
cout<< “abs(c1)=” << abs( c1 ) << “\n\n” ; //the magnitude of c1

cout<< “5.0*cos(theta)=” << 5.0*cos( pi/6) << endl
<< “5.0*sin(theta)=” << 5.0*sin( pi/6) ;

Output,

c1=(4.33013,2.5)
arg(c1)=0.523599
abs(c1)=5

5.0*cos(theta)=4.33013
5.0*sin(theta)=2.5

You can see that the c1 phase angle is 0.523599 which is same as pi/6 and also the magnitude of c1 is 5.The real part of c1 which is ‘4.33013’ is same as the resultant value of ‘5.0*cos(theta )’ and the imaginary part of c1 which is 2.5 is same as the resultant value of ‘5.0*sin(theta)’.

Link :C++ cmath sin

Link :C++ cmath cos