For more information about concepts covered in this lesson, you can check out:
The cmath Module vs the math Module
The functions of the Python
math module aren’t equipped to handle complex numbers. However, Python provides a different module that can specifically deal with complex numbers. This is the
A function in the
math module will raise an exception if the input or inputs fall outside of the domain of the function. For example, the
sqrt() function in the
math module takes in only non-negative floats, so passing in a negative number will raise a
ValueError and the message is
math domain error. However, through mathematical magic, some of the functions in the
math module have an enlarged domain if you’re willing to work with complex numbers.
And if you want to allow complex numbers, then you may want to use the
cmath module. Now, as you saw, the
math module contains a lot of functions, but not all of these functions have the equivalent implementations in the
cmath module. Let me show you the ones that do.
The first couple of function types that we looked at in the
math module were the power functions and the logarithmic functions, and the
cmath module has equivalents for the exponential function, the
log10(), and the square root function.
These functions can accept values that are not necessarily legal in the corresponding
math functions. So, for instance, all of these functions can take in complex numbers. Then, the trigonometric functions have their versions in the
cmath module, so your favorites
tan(), and then their inverse functions,
And then we’ve got the classification functions,
isclose(), and these test whether a complex number is finite, is
nan, or whether two complex numbers are close.
To create a complex number in Python, you need to use the letter
j to denote the complex number whose square is equal to -1. So to write this, you go
1j, and so let’s verify that
1j squared is equal to
-1. Notice that because we’re working with a complex number, Python will automatically default to complex number notation.
Now, one reason why this has done is because in electrical engineering, the letter i is usually reserved for current, and a lot of applications in electromagnetism use complex numbers, so it would be confusing to use both the letter i to denote current and the number whose square equals -1. So in Python,
j is used for i. To create another complex number, for instance,
2 - 3j, and
(-5-12j). Let’s verify that the type of
c is a complex number. Now, you don’t need to import anything to start working with the basic arithmetic of complex numbers, but let’s suppose you wanted to find the natural logarithm of this complex number. You would need to import the
cmath module, so let’s go ahead and do that.
And we can therefore compute, say, the natural logarithm of that complex number
c. Now, if you try to use the
math module to compute the natural logarithm of this complex number, you get a
TypeError with the message that you can’t convert a complex number to a float. And then the other functions—for example, the exponential of
c, and you can even compute, say, the sine of this complex number
If you’re interested in learning more about the support that comes in Python to work with complex numbers, I recommend the course Simplify Complex Numbers With Python. It’s available at realpython.com. In the search field, type in complex numbers and it should be one of the very top courses listed. All right, in the next lesson, we’ll take a look at NumPy, which is a well-known data science module in Python, and compare it with the
Become a Member to join the conversation.