Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C++ Library - <ccomplex>



The <ccomplex> header (more commonly <complex> in C++) provides functions for working with complex numbers. It implements the complex class to contain complex numbers in Cartesian form and several functions and overloads to operate with them.

This header provides several functions and macros for handling complex numbers and defines float _Complex, double _Complex, and long double _Complex types.

Including <ccomplex> Header

To include the <ccomplex> header in your C++ program, you can use the following syntax.

#include <ccomplex>

Functions of <ccomplex> Header

Below is list of all functions from <ccomplex> header.

Complex Number Operations

Basic complex number operations in the <ccomplex> header provides functions for working with complex numbers and allows us to handle the real and imaginary parts, and create complex numbers from polar coordinates.

S.No Functions & Description
1 real(x)

This function returns the real part of the complex number x.

2 imag(x)

This function returns the imaginary part of the complex number x.

3 abs(x)

This function returns the absolute value of the complex number x.

4 arg(x)

This function returns the phase angle (or angular component) of the complex number x, expressed in radians.

5 norm(x)

This function returns the norm value of the complex number x.

6 conj(x)

This function returns the conjugate of the complex number x.

7 polar(magnitude, angle)

This function returns a complex object (in Cartesian format) corresponding to the complex number using a magnitude and an angle (in radians).

8 proj(x)

This function returns the projection of the complex number x onto the Riemann sphere.

Basic Manipulations of Complex Numbers

In the following example we are going to use real() and imag() to perform basic manipulations of complex numbers.

#include <iostream>
#include <complex>

int main() {
    std::complex<double> z(3.0, 4.0);
    double real_part = real(z);
    double imag_part = imag(z);
    
    std::cout << "Real part: " << real_part << std::endl;
    std::cout << "Imaginary part: " << imag_part << std::endl;
    return 0;
}

Output

If we run the above code it will generate the following output

Real part: 3
Imaginary part: 4

Transcendental overloads

The <ccomplex> header provides a variety of transcendental and their overloads specifically designed to handle complex numbers. provides functions related to exponentials, logarithms, powers, and arc-trigonometric/hyperbolic.

S.No Functions & Description
1 cos(x)

This function returns the cosine of the complex number x.

2 cosh(x)

This function returns the hyperbolic cosine of the complex number.

3 exp(x)

This function computes the exponential of natural (base-e) logarithm of the complex number x.

4 log10(x)

This function computes the base-10 logarithm of a complex number.

5 pow(x,y)

This function returns the complex power of base x raised to the y-th power.

6 sin(x)

This function returns the sine of the complex number x.

7 sinh(x)

This function returns the hyperbolic sine of the complex number x.

8 sqrt(x)

This function returns the square root of a complex number.

9 tan(x)

This function returns the tangent of the complex number x.

10 cosh(x)

This function returns the hyperbolic cosine of the complex number.

11 tanh(x)

This function returns the hyperbolic tangent of the complex number.

12 acos(x)

This function returns the arc cosine of the complex number x.

13 acosh(x)

This function returns the arc hyperbolic cosine of the complex number x.

14 asinh(x)

This function returns the arc sine of the complex number x.

15 atan(x)

This function returns the arc tangent of the complex number x.

16 atanh(x)

This function returns the arc hyperbolic tangent of the complex number x.

Using Transcendental Functions

In the following example we are going to use transcendental functions (cos(), exp(), and sqrt()) for a complex number.

#include <iostream>
#include <complex>
#include <cmath>
int main() {
    std::complex<double> z(1.0, 2.0);

    std::complex<double> cos_z = cos(z);     
    std::complex<double> exp_z = exp(z);     
    std::complex<double> sqrt_z = sqrt(z);   

    std::cout << "cos(z) = " << cos_z << std::endl;
    std::cout << "exp(z) = " << exp_z << std::endl;
    std::cout << "sqrt(z) = " << sqrt_z << std::endl;
    return 0;
}

Output

If we run the above code it will generate the following output

cos(z) = (2.03272,-3.0519)
exp(z) = (-1.1312,2.47173)
sqrt(z) = (1.27202,0.786151
Advertisements