Site icon Learn C++

What You Need To Use Complex Numbers in C++ Software

What is Complex Number ? What does real and imaginary number means in complex numbers? Do you want to use Complex numbers in your C++ software? How we can operate with complex numbers.

In mathematics, a Complex Number is a number that is composed with real numbers and imaginary with ‘i’ symbol, in the form a + bi. Here a is called the real part and b is called the imaginary part. Complex numbers come from satisfying the equation i2 = −1. Because there is no real number to satisfy this equation, but we can use complex numbers with this i  (imaginary number by René Descartes) to solve our these kind of problems. 

In C++ there is a complex library that implements the complex class to contain complex numbers in cartesian form and many methods and properties to operate with in complex forms. In this post we will give some of basics to use complex numbers in C++.

What is the definition of a complex number in a C++ software app?

We can define a complex number with real and imaginary parts as given example below,

[crayon-6636145bd087b290039561/]

We can use auto to define a complex variable

[crayon-6636145bd0884200350175/]

We can also define a complex number in lines as below,

[crayon-6636145bd0886242555783/]

How to display a complex number and its Real and Imaginary parts

We can display real and imaginary parts of a complex number as given example below,

[crayon-6636145bd0887951281813/]
[crayon-6636145bd0889694676212/]

How to use addition, subtraction, multiplication, and division operators with complex numbers in C++

We can use + – / * operators as we use in real numbers. When you operate with complex numbers real part and imaginary parts are handled as two different variables. See this example below,

[crayon-6636145bd088b424836948/]

Output of this example will be as follows,

[crayon-6636145bd088c911906934/]

In C++, we can also use other functions with complex numbers,

Using functions with complex numbers

Square Root Function

In C++, sqrt() function returns the square root of complex number using the principal branch, whose cuts are along the negative real axis

Power Function

In C++, pow() function powers the complex number.

Here is an example about Square Root sqrt() and Power pow() functions

[crayon-6636145bd088e882416235/]

Absolute Function

In C++, abs() function returns the absolute of the complex number.

Argument Function

In C++, arg() function returns the argument of the complex number.

Here is an example about abs() and arg() functions

[crayon-6636145bd0890704209588/]

Norm Function

In C++, norm() function is used to find the norm of the complex number. If c = a + bi is a complex number with real part a and imaginary part b, the complex conjugate of c is defined as c'(c bar) = a – bi, and the absolute value, also called the norm, of c is defined as,

[crayon-6636145bd0892422232231/]

Polar Function

In C++, polar() function constructs a complex number from magnitude and phase angle. Note that real part is equal to magnitude*cosine(phase angle) and imaginary part is equal to magnitude*sine(phase angle);

Conjugate Function

In C++, conj() function returns the conjugate of the complex number c. The conjugate of a complex number c = a + bi is c = a – bi, in other terms imaginary part multiplied by -1.

Here is a example for the Polar and Conjugate functions,

[crayon-6636145bd0894917701114/]

Projection Function

In C++, proj() function returns the projection of c complex number in to the Riemann sphere. The projection of c is c, except for complex infinities, that are mapped to the complex value with a real component of infinity. Imaginary part can be 0.0 or -0.0, depending on the sign of the imaginary component of complex number c. See this example below,

[crayon-6636145bd0895571915789/]

Do you want to try out these examples? Why not download a free trial of C++ Builder today?

Exit mobile version