Open In App

Complex numbers in C++ | Set 1

The complex library implements the complex class to contain complex numbers in cartesian form and several functions and overloads to operate with them.




// Program illustrating the use of real() and
// imag() function
#include <iostream>    
 
// for std::complex, std::real, std::imag
#include <complex>     
using namespace std;
 
// driver function
int main()
{   
  // defines the complex number: (10 + 2i)
  std::complex<double> mycomplex(10.0, 2.0);
 
  // prints the real part using the real function
  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)






// Program illustrating the use of arg() and abs()
#include <iostream>    
 
// for std::complex, std::abs, std::atg
#include <complex>
using namespace std;
 
// driver function
int main ()
{   
  // defines the complex number: (3.0+4.0i)
  std::complex<double> mycomplex (3.0, 4.0);
 
  // prints the absolute value of the complex number
  cout << "The absolute value of " << mycomplex << " is: ";
  cout << abs(mycomplex) << endl;
   
  // prints the argument of the complex number
  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)




// Program illustrating the use of polar()
#include <iostream>    
 
// std::complex, std::polar
#include <complex>
using namespace std;
 
// driver function
int main ()
{
  cout << "The complex whose magnitude is " << 2.0;
  cout << " and phase angle is " << 0.5;
   
  // use of polar()
  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)






// example to illustrate the use of norm()
#include <iostream>    
 
// for std::complex, std::norm
#include <complex>
using namespace std;
 
// driver function
int main ()
{   
  // initializing the complex: (3.0+4.0i)
  std::complex<double> mycomplex (3.0, 4.0);
 
  // use of norm()
  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)




// Illustrating the use of conj()
#include <iostream>
using namespace std;
 
// std::complex, std::conj
#include <complex>     
 
// driver program
int main ()
{
  std::complex<double> mycomplex (10.0,2.0);
 
  cout << "The conjugate of " << mycomplex << " is: ";
   
  // use of conj()
  cout << conj(mycomplex) << endl;
  return 0;
}

 The conjugate of (10,2) is (10,-2)

Time Complexity: O(1)
Auxiliary Space: O(1)




// Illustrating the use of proj()
 
#include <iostream>
using namespace std;
 
// For std::complex, std::proj
#include <complex>
  
// driver program
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)




// Illustrating the use of sqrt()
#include <iostream>
using namespace std;
 
// For std::ccomplex, stdc::sqrt
#include <complex>
  
// driver program
int main()
{   
    // use of sqrt()
    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


Article Tags :
C++