Wednesday, June 30, 2021

Python for Beginners: Introduction to cmath module in Python

While working on data science, machine learning or scientific calculations, we often need to perform calculations on numeric data types including  complex numbers. In this article, we will use the cmath module in python to perform operations on complex numbers using different methods provided in the module.

Calculate phase of a complex number

The phase of a complex number is defined as the angle between the real axis and the vector representing the complex number. Using the cmath module, we can find the phase of a complex number using the phase() method. The phase method takes a complex number as input and returns a floating point number representing the phase of the complex number as follows.

import cmath
myNum=3+2j
print("The complex number is:",myNum)
myPhase= cmath.phase(myNum)
print("Phase of  the complex number is:",myPhase)

Output:

The complex number is: (3+2j)
Phase of  the complex number is: 0.5880026035475675

Polar coordinates of a complex number

In polar coordinates, a complex number is defined as a tuple consisting of the modulus of the complex number as the first element and phase of the complex number as the second element. We can find the polar coordinates of a complex number using polar() method in python. The polar() method takes a complex number as input and returns a tuple representing the polar coordinates as follows.

import cmath
myNum=3+2j
print("The complex number is:",myNum)
myPol= cmath.polar(myNum)
print("Polar coordinates of  the complex number are:",myPol)

Output:

The complex number is: (3+2j)
Polar coordinates of  the complex number are: (3.605551275463989, 0.5880026035475675)

If we know the modulus and phase of a complex number i.e. If we know the polar coordinates of a complex number, we can obtain the  complex number using the rect() method. The rect()  method takes the modulus as the first argument and phase of the complex number as the second argument and returns the corresponding complex number as follows.

import cmath
myPol= (3.605551275463989, 0.5880026035475675)
print("Polar coordinates of  the complex number are:",myPol)
print("Modulus of the complex number is:",myPol[0])
print("Phase of the complex number is:",myPol[1])
myRec=cmath.rect(myPol[0],myPol[1])
print("Complex number in rectangular form is:",myRec)

Output:

Polar coordinates of  the complex number are: (3.605551275463989, 0.5880026035475675)
Modulus of the complex number is: 3.605551275463989
Phase of the complex number is: 0.5880026035475675
Complex number in rectangular form is: (3+1.9999999999999996j)

Constants in cmath module

The cmath module also provides certain mathematical constants like infinity, NaN and pi which are useful in mathematical calculations. Some of the constants are given in the following example.

import cmath
print("Given below are some of the constants defined in cmath module.")
print("e:",cmath.e)
print("Infinity (real axis):",cmath.inf)
print("Infinity (Imaginary axis):",cmath.infj)
print("NaN (real):",cmath.nan)
print("NaN (imaginary):",cmath.nanj)
print("Pi:",cmath.pi)
print("Tau:",cmath.tau)

Output:

Given below are some of the constants defined in cmath module.
e: 2.718281828459045
Infinity (real axis): inf
Infinity (Imaginary axis): infj
NaN (real): nan
NaN (imaginary): nanj
Pi: 3.141592653589793
Tau: 6.283185307179586

Trigonometric functions in cmath module

For mathematical calculations on complex numbers, the cmath module provides a set of trigonometric functions.All of the trigonometric functions take the complex number as input and also return a complex number which represents the corresponding output for the trigonometric functions. Examples are discussed below.

import cmath
myNum=3+2j
print("Complex number is:",myNum)
print("Sine of the complex number is:",cmath.sin(myNum))
print("Cosine of the complex number is:",cmath.cos(myNum))
print("Tangent of the complex number is:",cmath.tan(myNum))
print("Inverse Sine of the complex number is:",cmath.asin(myNum))
print("Inverse Cosine of the complex number is:",cmath.acos(myNum))
print("Inverse Tangent of the complex number is:",cmath.atan(myNum))

Output:

Complex number is: (3+2j)
Sine of the complex number is: (0.5309210862485197-3.59056458998578j)
Cosine of the complex number is: (-3.7245455049153224-0.5118225699873846j)
Tangent of the complex number is: (-0.009884375038322495+0.965385879022133j)
Inverse Sine of the complex number is: (0.9646585044076028+1.9686379257930964j)
Inverse Cosine of the complex number is: (0.6061378223872937-1.9686379257930964j)
Inverse Tangent of the complex number is: (1.3389725222944935+0.14694666622552977j)

Hyperbolic functions in cmath module

Just like trigonometric functions, cmath module also provides hyperbolic functions and inverse hyperbolic trigonometric functions for mathematical calculations in python. All these functions take a complex number as input and return a complex number representing the hyperbolic or inverse hyperbolic trigonometric output as per their nature. Examples are given below.

import cmath
myNum=3+2j
print("Complex number is:",myNum)
print("Hyperbolic Sine of the complex number is:",cmath.sinh(myNum))
print("Hyperbolic Cosine of the complex number is:",cmath.cosh(myNum))
print("Hyperbolic Tangent of the complex number is:",cmath.tanh(myNum))
print("Inverse Hyperbolic Sine of the complex number is:",cmath.asinh(myNum))
print("Inverse Hyperbolic Cosine of the complex number is:",cmath.acosh(myNum))
print("Inverse Hyperbolic Tangent of the complex number is:",cmath.atanh(myNum))

Output:

Complex number is: (3+2j)
Hyperbolic Sine of the complex number is: (-4.168906959966565+9.15449914691143j)
Hyperbolic Cosine of the complex number is: (-4.189625690968807+9.109227893755337j)
Hyperbolic Tangent of the complex number is: (1.00323862735361-0.003764025641504249j)
Inverse Hyperbolic Sine of the complex number is: (1.9833870299165355+0.5706527843210994j)
Inverse Hyperbolic Cosine of the complex number is: (1.9686379257930964+0.6061378223872937j)
Inverse Hyperbolic Tangent of the complex number is: (0.22907268296853878+1.4099210495965755j)

Logarithmic functions in cmath module

The cmath module provides two methods namely log() and log10() for logarithmic calculations on complex numbers. The log() function takes a complex number as first input and an optional argument representing the base of the logarithmic function. When we pass only the complex number as input to the log() function, it returns the natural log of the complex number with base “e”. When we also pass the second argument i.e. base to the log() function, it calculates the logarithm of the complex number with the provided base. This can be seen in the following example.

import cmath
myNum=3+2j
print("Complex number is:",myNum)
print("Natural log of the complex number is:",cmath.log(myNum))
print("Logarithm of the complex number with base 5 is:",cmath.log(myNum,5))

Output:

Complex number is: (3+2j)
Natural log of the complex number is: (1.2824746787307684+0.5880026035475675j)
Logarithm of the complex number with base 5 is: (0.7968463205835412+0.36534655919610926j)

The log10() method calculates the logarithm of a complex number with base 10 as follows.

import cmath
myNum=3+2j
print("Complex number is:",myNum)
print("Logarithm of the complex number with base 10 is:",cmath.log10(myNum))

Output:

Complex number is: (3+2j)
Logarithm of the complex number with base 10 is: (0.5569716761534184+0.255366286065454j)

Power functions in cmath module

The cmath module provides two power functions namely exp() and sqrt() for calculations in python. The exp() function takes a complex number as input and returns  a complex number representing the exponential value of the input. This can be seen in the following example.

import cmath
myNum=3+2j
print("Complex number is:",myNum)
print("Exponential of the complex number is:",cmath.exp(myNum)) 

Output:

Complex number is: (3+2j)
Exponential of the complex number is: (-8.358532650935372+18.263727040666765j)

The sqrt() function also takes a complex number as input and returns the complex number representing the square root of the input as follows.


import cmath
myNum=3+2j
print("Complex number is:",myNum)
print("Square root of the complex number is:",cmath.sqrt(myNum)) 

Output:

Complex number is: (3+2j)
Square root of the complex number is: (1.8173540210239707+0.5502505227003375j)

Conclusion

In this article, we have studied the functions and methods in the cmath module to perform mathematical operations on complex numbers in Python.We can also write the programs used in this article with exception handling using python try except to make the programs more robust and handle errors in a systematic way. Stay tuned for more informative articles.

The post Introduction to cmath module in Python appeared first on PythonForBeginners.com.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...