Python

Complex Numbers in Python

Python is an incredibly flexible language for working with numerical data. The ability to work with both actual and fictitious numbers is also supported. We frequently have to execute computations on numeric data types, including complex numbers, when engaging in data science, deep learning, or scientific computations. More information on imaginary numbers and how to use them in Python is covered in this session.

Complex numbers are frequently seen in pairs and are utilized to find the square roots of the negative numbers. Python can effectively handle numbers and their associated functions in addition to the real numbers by using the “cmath” file. Python offers helpful capabilities to manage and modify the complex numbers which are valuable in many applications connected to mathematics.

Syntax of the Complex Number in Python

The following syntax of the complex number is utilized in the Python language:

# complex([real[, imag]])

There isn’t much difference between creating and working with the complex numbers in Python compared to the other built-in data types, especially the numeric types. It is feasible since the language grants them the first-class citizenship. This implies that there isn’t much overhead involved in expressing mathematical calculations with the complex numbers.

In the same way that you would call the functions on the other numbers in Python, the complex numbers are supported in arithmetic expressions. It produces beautiful syntax that resembles a math textbook in several ways.

Example 1: Program to Convert the Complex Number to A Real Number

The “x + yi” is the symbol for a complex number. Using the complex function, Python transforms x and y from real values into complex (x,y) values. Using the real() function, the real portion may be retrieved and the imag() function can be used to represent the imaginary portion.

import cmath

n1 = 6
n2 = 1

res = complex(n1,n2);

print ("Real complex number : ",end="")
print (res.real)

print ("Imaginary complex number : ",end="")
print (res.imag)

In the previous example, we imported the cmath module to work with the complex numbers. Then, we declared two variables as n1 and n2. These variables are set with the integer values. The complex function takes these two variables as input inside. The complex function is called in the res variable. The print statement being called takes the real and imag numbers as a parameter with the res variable.

They can see the real and imaginary complex numbers in the following image:

Example 2: Program of the Complex Number Phase

The angle between the complex number’s representational vector and the positive real axis is known as the complex number’s phase in geometry. The term “argument of a complex number” may also be used to describe this. Phase(), which accepts a complex number as an input, returns phase.

import cmath

a = -5.0
b = 0.0
c = complex(a,b);
print ("Phase complex number : ",end="")
print (cmath.phase(c))

Here, after importing the cmath module, we defined the two variables as a and b. Variable a is initialized with the negative numeric value and variable b is initialized with the positive numeric value. We declared another variable as c where the complex function is invoked. For the complex function, we provided the variable a and b for the conversion in complex numbers. Then, we printed the complex numbers using the phase function.

The phase complex number is generated on the following console screen:

Example 3: Program to Convert the Complex Number to Rectangular Coordinates

Using the polar() function which returns a pair(r,ph) indicating the modulus r and phase angle ph, the polar data is converted. Abs() and phase are both functions that can be used to display the modulus().

The rect(r, ph), where r is the modulus and ph is the phase angle , translates a complex integer into rectangular coordinates. It gives back a number that is equivalent to r * (math.cos(ph) + math.sin(ph)*1j).

import cmath


p = 4.0
q = 4.0

r = complex(p,q);

print ("Sine complex number: ",end="")
print (cmath.sin(r))

print ("Cosine complex number: ",end="")
print (cmath.cos(r))

print ("Tangent complex number: ",end="")
print (cmath.tan(r))

We included the cmath and math module for the complex number operations. Then, we declared the two variables i and j which have real numbers. The real numbers are passed to the complex function and the complex function is defined in the variable z. We called the polar function to convert the complex numbers into polar. After that, we have the rect() function for the conversion of the complex number to the rectangular coordinates.

The results of the polar and rectangular complex numbers are shown as follows:

Example 4: Program of Trigonometric Function Complex Numbers

Here, we explain the complex number of trigonometric functions:

  1. sin(): The complex number provided as an argument and returned by this function is its sine.
  2. cos(): When a complex number is supplied as an argument, this method returns its cosine.
  3. tan(): The complex number that is specified as an argument receives its tangent from this function.
import cmath
import math
u = 8.0
v = 8.0
w = complex(u, v);

print ("log10 complex number: ", end="")
print (cmath.log10(w))

print ("Square root complex number: ", end="")
print (cmath.sqrt(w))

We simply passed the defined real number inside the complex function. Then, the complex numbers are passed inside the trigonometric functions. The print statement displays the trigonometric complex numbers.

The output obtained from the previous Python script is as follows:

Example 5: Program of Exponent and Log Complex Number

Here, we discussed some operations of the complex number which include the exp() function and the log() function.

exp(): The complex number indicated in its argument’s exponent is returned by this function.

log(a,b): When both of the inputs to this function are specified, it gives the logarithmic result of “a” with “base b”. The natural log of “a” is produced in the absence of a base argument.

import cmath
import math
i = 3.0
j = 3.0

z = complex(i,j);

c = cmath.polar(z)

print ("Polar complex number modulus and argument : ",end="")
print (c)
c = cmath.rect(4.242640687119285, 0.7853981633974483)
print ("Rectangular complex number: ",end="")
print (c)

We passed the specified real numbers inside the complex function. Then, we printed the log10 of the complex number by invoking the log10 function inside the print statement. Also, we printed the square root of the complex numbers.

The outcome of the previous script is as follows:

Conclusion

The methods by which Python enables the implementation and storage of numerical data are complex numbers. It’s seen as being crucial to the Python programming. There are numerous ways to manage the complex numbers using the Python programming language. We covered a few of these methods in this article.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content