Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

The cmath Module vs the math Module

00:00 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 cmath module.

00:15 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.

00:44 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.

01:02 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 log() function, log10(), and the square root function.

01:17 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 cos(), sin(), tan(), and then their inverse functions, acos(), asin(), and atan().

01:40 These can accept complex numbers and produce complex numbers.

01:45 And then we’ve got the classification functions, isfinite(), isinf(), isnan(), and isclose(), and these test whether a complex number is finite, is inf, is nan, or whether two complex numbers are close.

02:01 Now, here’s an important thing to take note about the cmath functions is that they’re always going to return a complex number, even if the result has a zero imaginary part.

02:12 So if the value that is returned could be expressed as a real number, the cmath functions we’ll add in a zero imaginary part to that real number.

02:23 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.

02:50 In mathematics, the number whose square equals -1 is usually denoted by i, but in Python, like in many other programming languages, it’s noted by the letter j.

03:02 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 c squared,

03:35 (-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.

04:00 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 c.

04:29 Now, to understand what it means to take the exponential of a complex number or the sine of a complex number, you need to take a course in complex analysis.

04:39 But, you know, if you’re taking such a course, it’s kind of cool to know that just by loading a module in Python, you can do these computations using complex numbers.

04:50 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 math module.

Become a Member to join the conversation.