Python Complex Numbers With Examples

A complex number is created from two real numbers. Python complex number can be created using complex() function as well as using direct assignment statement.

Complex numbers are mostly used where we define something using two real numbers. For example, a circuit element that is defined by Voltage (V) and Current (I). They are mostly used in geometry, calculus and scientific calculations.

Python Complex Numbers

Let’s first learn how to create complex numbers in python.

Output:

Python complex numbers are of type complex. Every complex number contains one real part and one imaginary part.

Python Complex Numbers Attributes and Functions

Let’s look at some attributes and instance functions of complex numbers.

Output:

Complex Numbers Mathematical Calculations

Complex numbers support mathematical caluclations such as addition, subtraction, multiplication and division.

Output:

Complex numbers don’t support comparison operators. If we try to execute c < c1 then the error message will be thrown as TypeError: '<' not supported between instances of 'complex' and 'complex'.

Python cmath module

Python cmath module provides access to mathematical functions for complex numbers. Let’s look at some of the important features of complex numbers and how we can use cmath module function to calculate them.

Phase of Complex Number

The phase of a complex number is the angle between the real axis and the vector representing the imaginary part. Below image illustrates the phase of a complex number and how to get this value using cmath and math modules.

A complex number is created from two real numbers. Python complex number can be created using complex() function as well as using direct assignment statement.

Note that the phase returned by math and cmath modules are in radians, we can use numpy.degrees() function to convert it to degrees. The range of phase is from -π to +π (-pi to +pi) in radians and it’s equivalent to -180 to +180 degrees.

Output:

Polar and Rectangular Coordinates

We can write a complex number in polar coordinates, which is a tuple of modulus and phase of the complex number.

We can use cmath.rect() function to create a complex number in rectangular format by passing modulus and phase as arguments.

Output:

cmath module constants

There are a bunch of constants in cmath module that are used in the complex number calculations.

Output:

Power and Log Functions

There are some useful functions for logarithmic and power operations.

Output:

Trigonometric Functions

Output:

Hyperbolic Functions

Output:

Classification Functions

There are some miscellaneous functions to check if the complex number is finite, infinite or nan. There is also a function to check if two complex numbers are close.

The output of each statement is provided in the comments.

You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: API Doc

By admin

Leave a Reply

%d bloggers like this: