C++ complex header file


The C++ <complex> header provide C++ programmer with various functions to manipulate the complex numbers -a complex number is represented in the form “a + ib” , ‘a’ is the real part and ‘b’ is the imaginary part.Note this <complex> header consist of a template class name complex,the declaration of this template class is shown below,

template<class T>
class complex;

If you ever come across the <ccomplex> header note that it is referring to the <complex> header.They are the same.

To define a complex number we simply pass two values as an arguments to the object of the complex class.The first argument is the ‘real’ value and the second argument is the ‘imaginary’ value.A simple declaration of the ‘complex’ class is shown below.

Code example

complex < double > com( 23.4 , 4.56); //23 is the real part and 4.56 is the imaginary part

complex< int > com1(12 , 90) ; //12 is the real number and 90 is the imaginary number

Note if ‘z’ is any complex number then:

-> reinterpret_cast< T(&)[2] >(z)[0] , gives the real number.

-> reinterpret_cast< T(&)[2] >(z)[1] , gives the imaginary number.

Link : C++ reinterpret_cast

A simple code example is given below.

Code example

complex < double > com( 23.4 , 4.56);

cout<< reinterpret_cast< double(&)[2] >(com)[0] << endl
<< reinterpret_cast< double(&)[2] >(com)[1] ;

Output ,

23.4
4.56

There are three specializations of the ‘complex’ class.By specialization we mean the template class is defined to work with a specific type.The general template complex class is utilized for all the types but the specialized class template is used only for the specific type for which it is defined.The three specializations is given below.

template< > class complex< float >
template< > class complex< double >
template< > class complex< long double >

The types and member functions and non-member functions of the ‘complex’ class is shown below.

Types

typedef T value_type;

The ‘T’ is the type of the real and imaginary value of the complex value.

Constructors

constexpr complex(const T& re = T() , const T& im = T( ) );
constexpr complex(const complex&);
template<class X> constexpr complex(const complex<X>&);

The first constructor is called when two arguemnts are passed and it will also act as the default constrcutor when no arguments is passed.

The second constrcutor is the copy constructor.

The third constructor can be also used as copy constructor and it also a template type.A template copy constructor is liable to be more versatile than the normal copy constructor.

Member functions

constexpr T real( ) const; //Gives the real value of the complex number
void real(T); //Assign new value to the real value
constexpr T imag( ) const; //Gives the imaginary value of the complex number
void imag(T); //Assign new value to the imaginary value
complex<T>& operator= (const T&);
complex<T>& operator+=(const T&);
complex<T>& operator-=(const T&);
complex<T>& operator*=(const T&);
complex<T>& operator/=(const T&);
complex& operator=(const complex&); //Assignment operator
template<class X> complex<T>& operator= (const complex<X>&);
template<class X> complex<T>& operator+=(const complex<X>&);
template<class X> complex<T>& operator-=(const complex<X>&);
template<class X> complex<T>& operator*=(const complex<X>&);
template<class X> complex<T>& operator/=(const complex<X>&);




Non-member functions

Manipulations functions
template<class T> constexpr T real(const complex< T >&);
template<class T> constexpr T imag(const complex<T>&);
template<class T> T abs(const complex<T>&);
template<class T> T arg(const complex<T>&);
template<class T> T norm(const complex<T>&);
template<class T> complex<T> conj(const complex<T>&);
template <class T> complex<T> proj(const complex<T>&);
template<class T> complex<T> polar(const T&, const T& = 0);
Trigonometric functions
template<class T> complex<T> acos(const complex<T>&);
template<class T> complex<T> asin(const complex<T>&);
template<class T> complex<T> atan(const complex<T>&);
template<class T> complex<T> cos (const complex<T>&);
template<class T> complex<T> sin (const complex<T>&);
template<class T> complex<T> tan (const complex<T>&);
Hyperbolic functions
template<class T> complex<T> acosh(const complex<T>&);
template<class T> complex<T> asinh(const complex<T>&);
template<class T> complex<T> atanh(const complex<T>&);
template<class T> complex<T> cosh (const complex<T>&);
template<class T> complex<T> sinh (const complex<T>&);
template<class T> complex<T> tanh (const complex<T>&);
Exponential and logarithmic functions
template<class T> complex<T> exp (const complex<T>&);
template<class T> complex<T> log (const complex<T>&);
template<class T> complex<T> log10(const complex<T>&);
Power and absolute functions
template<class T> complex<T> pow(const complex<T>&, const T&);
template<class T> complex<T> pow(const complex<T>&, const complex<T>&);
template<class T> complex<T> pow(const T&, const complex<T>&);
template<class T> complex<T> sqrt (const complex<T>&);
template<class T> complex<T> operator+(const complex<T>&);
Operators +
template<class T>
complex operator+(const complex<T>&, const complex<T>&);
template<class T> complex<T> operator+(const complex<T>&, const T&);
template<class T> complex<T> operator+(const T&, const complex<T>&);
Operators –
template<class T> complex<T> operator-(
const complex<T>&, const complex<T>&);
template<class T> complex<T> operator-(const complex<T>&, const T&);
template<class T> complex<T> operator-(const T&, const complex<T>&);
Operators *
template<class T> complex<T> operator*(
const complex<T>&, const complex<T>&);
template<class T> complex<T> operator*(const complex<T>&, const T&);
template<class T> complex<T> operator*(const T&, const complex<T>&);
Operators /
template<class T> complex<T> operator/(
const complex<T>&, const complex<T>&);
template<class T> complex<T> operator/(const complex<T>&, const T&);
template<class T> complex<T> operator/(const T&, const complex<T>&);
Operators ==
template<class T> constexpr bool operator==(
const complex<T>&, const complex<T>&);
template<class T> constexpr bool operator==(const complex<T>&, const T&);
template<class T> constexpr bool operator==(const T&, const complex<T>&);
Operators !=
template<class T> constexpr bool operator!=(const complex<T>&, const complex<T>&);
template<class T> constexpr bool operator!=(const complex<T>&, const T&);
template<class T> constexpr bool operator!=(const T&, const complex<T>&);
Operators<< and operator>>
template<class T, class charT, class traits>
basic_istream<charT, traits>&
operator>>(basic_istream<charT, traits>&, complex<T>&);
template<class T, class charT, class traits>
basic_ostream<charT, traits>&
operator<<(basic_ostream<charT, traits>&, const complex<T>&);

A code example is given below to give an overall view of how to use the complex class and it’s member functions.

Code example

#include <iostream>
#include <complex>

using namespace std ;

int main( )
{
complex<double> z( 2.1 , 90.45) , z1(23.4 , 6.90 ) , sum ;

//getting the real value of z and z1
cout<< z.real() << ” ” << z1.real() << endl ;

sum=z+z1 ; //calls the nonmember operator+ function

cout<< sum.real( ) << ” ” << sum.real( ) << endl ;

//Adding value only to the real value
z+=100.3 ; //calls the member operator+= function

cinn.get( );
return 0;
}

Output ,

2.1   23.4
25.5   83.55
102.4