Complex Numbers in Python?

PythonServer Side ProgrammingProgramming

A complex number is created from real numbers. Python complex number can be created either using direct assignment statement or by using complex () function.

Complex numbers which are mostly used where we are using two real numbers. For instance, an electric circuit which is defined by voltage(V) and current(C) are used in geometry, scientific calculations and calculus.


complex([real[, imag]])

Creating a simple complex number in python

>>> c = 3 +6j
>>> print(type(c))
<class 'complex'>
>>> print(c)
>>> c1 = complex(3,6)
>>> print(type(c1))
<class 'complex'>
>>> print(c1)

From above results, we can see python complex numbers are of type complex. Each complex number consist of one real part and one imaginary part.

Python Complex Numbers- Attributes and Functions

>>> #Complex Number:
>>> c = (3 + 6j)
>>> #Real Part of complex number
>>> print('Complex Number: Real Part is = ', c. real)
Complex Number: Real Part is = 3.0
>>> #Imaginary Part of complex number
>>> print('Complex Number: Imaginary Part is = ', c. imag)
Complex Number: Imaginary Part is = 6.0
>>> #Conjugate of complex number
>>> print('Complex Number: conjugate Part = ', c. conjugate())
Complex Number: conjugate Part = (3-6j)

Mathematical Calculations on Complex numbers

We can do simple mathematical calculations on complex numbers:

>>> #first complex number
>>> c1 = 3 + 6j
>>> #Second complex number
>>> c2 = 6 + 15j
>>> #Addition
>>> print("Addition of two complex number =", c1 + c2)
Addition of two complex number = (9+21j)
>>> #Subtraction
>>> print("Subtraction of two complex number =", c1 - c2)
Subtraction of two complex number = (-3-9j)
>>> #Multiplication
>>> print("Multiplication of two complex number =", c1 * c2)
Multiplication of two complex number = (-72+81j)
>>> #Division
>>> print("Division of two complex number =", c1 / c2)
Division of two complex number = (0.4137931034482759-0.03448275862068964j)

However, complex numbers don’t support comparison operators like <, >, <=, => and it will through TypeError message:

>>> c2 <= c2
Traceback (most recent call last):
File "<pyshell#40>", line 1, in <module>
c2 <= c2
TypeError: '<=' not supported between instances of 'complex' and 'complex'

Python cmath module

Python cmath module provide access to mathematical functions for complex numbers. Let’s looks at some of the important features of complex numbers using math module function.

Phase of complex number

The phase of a complex number is the angle between the real axis and the vector representing the imaginary part.

The phase returned by math and cmath modules are in radians and we use the numpy.degrees() function to convert it to degrees.

import cmath, math, numpy
c = 4+ 4j
# phase
phase = cmath.phase(c)
print('4+ 4j Phase =', phase)
print('Phase in Degrees =', numpy.degrees(phase))
print('-4-4j Phase =', cmath.phase(-4-4j), 'radians. Degrees =', numpy.degrees(cmath.phase(-4-4j)))
# we can get phase using math.atan2() function too
print('Complex number phase using math.atan2() =', math.atan2(2, 1))


4+ 4j Phase = 0.7853981633974483
Phase in Degrees = 45.0
-4-4j Phase = -2.356194490192345 radians. Degrees = -135.0
Complex number phase using math.atan2() = 1.1071487177940904

cmath module constants

There are a couple of constans available in cmath module that are used in the complex number calculations:

import cmath
print('π =', cmath.pi)
print('e =', cmath.e)
print('tau =', cmath.tau)
print('Positive infinity =', cmath.inf)
print('Positive Complex infinity =', cmath.infj)
print('NaN =', cmath.nan)
print('NaN Complex =', cmath.nanj)


π = 3.141592653589793
e = 2.718281828459045
tau = 6.283185307179586
Positive infinity = inf
Positive Complex infinity = infj
NaN = nan
NaN Complex = nanj

Power and Log Functions

The cmath() module provides some useful functions for logarithmic and power operations:

import cmath
c = 1 + 2j
print('e^c =', cmath.exp(c))
print('log2(c) =', cmath.log(c, 2))
print('log10(c) =', cmath.log10(c))
print('sqrt(c) =', cmath.sqrt(c))


e^c = (-1.1312043837568135+2.4717266720048188j)
log2(c) = (1.1609640474436813+1.5972779646881088j)
log10(c) = (0.3494850021680094+0.480828578784234j)
sqrt(c) = (1.272019649514069+0.7861513777574233j)

Trigonometric Functions

import cmath
c = 2 + 4j
print('arc sine value:\n ', cmath.asin(c))
print('arc cosine value :\n', cmath.acos(c))
print('arc tangent value of complex number c :\n', cmath.atan(c))
print('sine value:\n', cmath.sin(c))
print('cosine value:\n', cmath.cos(c))
print('tangent value:\n', cmath.tan(c))


arc sine value:
arc cosine value :
arc tangent value of complex number c :
sine value:
cosine value:
tangent value:

Hyperbolic Functions

import cmath
c = 2 + 4j
print('Inverse hyperbolic sine value: \n', cmath.asinh(c))
print('Inverse hyperbolic cosine value: \n', cmath.acosh(c))
print('Inverse hyperbolic tangent value: \n', cmath.atanh(c))
print('Hyperbolic sine value: \n', cmath.sinh(c))
print('Hyperbolic cosine value: \n', cmath.cosh(c))
print('Hyperbolic tangent value: \n', cmath.tanh(c))


Inverse hyperbolic sine value:
Inverse hyperbolic cosine value:
Inverse hyperbolic tangent value:
Hyperbolic sine value:
Hyperbolic cosine value:
Hyperbolic tangent value:
Published on 02-May-2019 12:45:38