Learning Python- Intermediate course: Day 10, Complex numbers part 2

Yesterday we learnt the basics of operating with complex numbers. Today we will cover many different functions related to complex numbers.

Phase of complex number

The phase or the argument of any complex number can be found out using the phase() function.

import cmath
x = -1.0
y = 0.0
# converting x and y into complex number
z = complex(x,y);
# printing phase of a complex number using phase()
print ("The phase of complex number is : ",cmath.phase(z))
The phase of complex number is :  3.141592653589793

Polar form of complex number.

We can convert a complex number to polar form using the polar() and back into rectangular form using the rect() function.

import cmath
z = complex(1,1)
a = cmath.polar(z)
print ("The polar complex number is : ",end="")
print (a) # returns a tuple
z2= cmath.rect(a[0],a[1])
print ("The rectangular form of complex number is : ",end="")
print (z2)
The polar complex number is : (1.4142135623730951, 0.7853981633974483)
The rectangular form of complex number is : (1.0000000000000002+1j)

Note the return types for the functions

  • polar() returns a tuple.
  • rect() returns a complex number.

Functions is the cmath module

Let us now explore the functions in the cmath module which are frequently used. The example below explains the use of the most commonly used functions. Entire list of functions with documentation can be found here

>>> import cmath
>>> z=complex(-2,1)
#make a complex number.
>>> cmath.exp(z)
# Raise z to a complex power.
>>> cmath.exp(z.real)
# the cmath module takes in real as well as complex parameters.
>>> cmath.log(z,10)
#logarithm of z to the base 10
>>> cmath.log(10,z)
# logarithm of 10 to  the base z
>>> cmath.log(z,z)
#alogarithm of z to the base z
>>> cmath.sqrt(z)
# square root of z
>>> cmath.acos(z)
# arccos of z
>>> cmath.atan(z)
# arctan of z
>>> cmath.sin(z)
# arc sine of z
>>> cmath.acosh(z)
# hyperbolic inverse cosine
>>> cmath.tanh(z)
# hyperbolic tangent
>>> cmath.pi
# The usual pi constant
>>> pow(z,z)
# z raised to the power z.(note that this is not from the cmath module.

Note- j is often used in electronics instead of i, hence in Python expressions like 1+1i are written as 1+ij

Applications of complex numbers to computer science.

Well I know you all must be wondering why in the world are we learning about complex numbers. Well, this is because complex numbers are a very handy tool in solving many real world problems. They are a great way to store in coordinate systems. As we just saw, they are very easy to implement than when compared to vectors. In Python, complex numbers can be operated naturally just like plain old real numbers.

Other applications of complex numbers include-

  • Signal processing
  • Image processing
  • Scientific computing
  • Graphics
  • Computer vision
  • Data compression

Read also

For those who are new to the concept of complex numbers might like the video below.

Did You like the content?😎 Please let me know in the comment section below 👇. And don't forget to like the post if you did. 😍 I am open to any suggestions or doubts. 🤠 Just post in the comments below or gmail me. 😉
Thank you all👍

Also please visit the Learning-Python repo made especially for this course and don't forget to star it too!