Yesterday we learnt the basics of operating with complex numbers. Today we will cover many different functions related to complex numbers.
The phase or the argument of any complex number can be found out using the
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
We can convert a complex number to polar form using the
polar() and back into rectangular form using the
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,a) 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.
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. (0.07312196559805963+0.1138807140643681j) >>> cmath.exp(z.real) # the cmath module takes in real as well as complex parameters. (0.1353352832366127+0j) >>> cmath.log(z,10) #logarithm of z to the base 10 (0.3494850021680094+1.1630167557051545j) >>> cmath.log(10,z) # logarithm of 10 to the base z (0.2369795135136017-0.7886208085195003j) >>> cmath.log(z,z) #alogarithm of z to the base z (1+0j) >>> cmath.sqrt(z) # square root of z (0.34356074972251244+1.455346690225355j) >>> cmath.acos(z) # arccos of z (2.6342363503726487-1.4693517443681852j) >>> cmath.atan(z) # arctan of z (-1.1780972450961724+0.17328679513998632j) >>> cmath.sin(z) # arc sine of z (-1.4031192506220405-0.4890562590412937j) >>> cmath.acosh(z) # hyperbolic inverse cosine (1.4693517443681852+2.6342363503726487j) >>> cmath.tanh(z) # hyperbolic tangent (-1.0147936161466335+0.0338128260798967j) >>> cmath.pi # The usual pi constant 3.141592653589793 >>> pow(z,z) # z raised to the power z.(note that this is not from the cmath module. (-0.00220568464655929+0.013562654681556313j)
Note- j is often used in electronics instead of i, hence in Python expressions like
1+1iare written as
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
- Computer vision
- Data compression
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!