Python Complex Numbers - Cmath

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.

c = 1 + 2jprint(type(c))print(c)c1 = complex(2, 4)print(type(c1))print(c1)

Output:

<class 'complex'>(1+2j)<class 'complex'>(2+4j)

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.

c = 1 + 2jprint('Real Part =', c.real)print('Imaginary Part =', c.imag)print('Complex conjugate =', c.conjugate())

Output:

Real Part = 1.0Imaginary Part = 2.0Complex conjugate = (1-2j)

Complex Numbers Mathematical Calculations

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

c = 1 + 2jc1 = 2 + 4jprint('Addition =', c + c1)print('Subtraction =', c - c1)print('Multiplication =', c * c1)print('Division =', c1 / c)

Output:

Addition = (3+6j)Subtraction = (-1-2j)Multiplication = (-6+8j)Division = (2+0j)

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.

Python Complex Numbers - cmath

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.

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

Output:

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

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.

c = 1 + 2jmodulus = abs(c)phase = cmath.phase(c)polar = cmath.polar(c)print('Modulus =', modulus)print('Phase =', phase)print('Polar Coordinates =', polar)print('Rectangular Coordinates =', cmath.rect(modulus, phase))

Output:

Modulus = 2.23606797749979Phase = 1.1071487177940904Polar Coordinates = (2.23606797749979, 1.1071487177940904)Rectangular Coordinates = (1.0000000000000002+2j)

cmath module constants

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

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)

Output:

π = 3.141592653589793e = 2.718281828459045tau = 6.283185307179586Positive infinity = infPositive Complex infinity = infjNaN = nanNaN Complex = nanj

Power and Log Functions

There are some useful functions for logarithmic and power operations.

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

Output:

e^c = (-3.074932320639359+6.71884969742825j)log2(c) = (1.5000000000000002+1.1330900354567985j)log10(c) = (0.4515449934959718+0.3410940884604603j)sqrt(c) = (1.5537739740300374+0.6435942529055826j)

Trigonometric Functions

c = 2 + 2jprint('arc sine =', cmath.asin(c))print('arc cosine =', cmath.acos(c))print('arc tangent =', cmath.atan(c))print('sine =', cmath.sin(c))print('cosine =', cmath.cos(c))print('tangent =', cmath.tan(c))

Output:

arc sine = (0.7542491446980459+1.7343245214879666j)arc cosine = (0.8165471820968505-1.7343245214879666j)arc tangent = (1.311223269671635+0.2388778612568591j)sine = (3.4209548611170133-1.5093064853236156j)cosine = (-1.5656258353157435-3.2978948363112366j)tangent = (-0.028392952868232294+1.0238355945704727j)

Hyperbolic Functions

c = 2 + 2jprint('inverse hyperbolic sine =', cmath.asinh(c))print('inverse hyperbolic cosine =', cmath.acosh(c))print('inverse hyperbolic tangent =', cmath.atanh(c))print('hyperbolic sine =', cmath.sinh(c))print('hyperbolic cosine =', cmath.cosh(c))print('hyperbolic tangent =', cmath.tanh(c))

Output:

inverse hyperbolic sine = (1.7343245214879666+0.7542491446980459j)inverse hyperbolic cosine = (1.7343245214879666+0.8165471820968505j)inverse hyperbolic tangent = (0.2388778612568591+1.311223269671635j)hyperbolic sine = (-1.5093064853236156+3.4209548611170133j)hyperbolic cosine = (-1.5656258353157435+3.2978948363112366j)hyperbolic tangent = (1.0238355945704727-0.028392952868232294j)

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.

print(cmath.isfinite(2 + 2j)) # Trueprint(cmath.isfinite(cmath.inf + 2j)) # Falseprint(cmath.isinf(2 + 2j)) # Falseprint(cmath.isinf(cmath.inf + 2j)) # Trueprint(cmath.isinf(cmath.nan + 2j)) # Falseprint(cmath.isnan(2 + 2j)) # Falseprint(cmath.isnan(cmath.inf + 2j)) # Falseprint(cmath.isnan(cmath.nan + 2j)) # Trueprint(cmath.isclose(2+2j, 2.01+1.9j, rel_tol=0.05)) # Trueprint(cmath.isclose(2+2j, 2.01+1.9j, abs_tol=0.005)) # False

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

  • 0