Python cmath Module - A Complete Tutorial

In this guide, you'll explore Python's cmath module, designed for mathematical operations with complex numbers. Learn its functions and examples for practical use.

The cmath module in Python provides mathematical functions for complex numbers. It includes functions for basic arithmetic, trigonometry, logarithms, and more, as well as constants like pi and e.

Table of Contents

  1. Introduction
  2. Constants
  3. Number-theoretic and Representation Functions
    • phase
    • polar
    • rect
    • exp
    • log
    • log10
    • sqrt
  4. Trigonometric Functions
    • acos
    • asin
    • atan
    • cos
    • sin
    • tan
  5. Hyperbolic Functions
    • acosh
    • asinh
    • atanh
    • cosh
    • sinh
    • tanh
  6. Examples
  7. Real-World Use Case
  8. Conclusion
  9. References

Introduction

The cmath module provides a comprehensive set of mathematical functions and constants for working with complex numbers. Complex numbers have both a real and an imaginary part, and the cmath module provides the necessary tools to perform calculations with these numbers.

Constants

pi

The mathematical constant π.

import cmath

print(cmath.pi)  # 3.141592653589793

e

The mathematical constant e.

print(cmath.e)  # 2.718281828459045

tau

The mathematical constant Ï„, which is 2Ï€.

print(cmath.tau)  # 6.283185307179586

Number-theoretic and Representation Functions

phase

Returns the phase of a complex number.

z = 1 + 1j
print(cmath.phase(z))  # 0.7853981633974483

polar

Returns the polar coordinates of a complex number.

z = 1 + 1j
print(cmath.polar(z))  # (1.4142135623730951, 0.7853981633974483)

rect

Returns the complex number from polar coordinates.

r, phi = 1.4142135623730951, 0.7853981633974483
print(cmath.rect(r, phi))  # (1.0000000000000002+1j)

exp

Returns the exponential of a complex number.

z = 1 + 1j
print(cmath.exp(z))  # (1.4686939399158851+2.2873552871788423j)

log

Returns the natural logarithm of a complex number.

z = 1 + 1j
print(cmath.log(z))  # (0.34657359027997264+0.7853981633974483j)

log10

Returns the base-10 logarithm of a complex number.

z = 1 + 1j
print(cmath.log10(z))  # (0.1505149978319906+0.3410940884604603j)

sqrt

Returns the square root of a complex number.

z = 1 + 1j
print(cmath.sqrt(z))  # (1.0986841134678098+0.45508986056222733j)

Trigonometric Functions

acos

Returns the arc cosine of a complex number.

z = 1 + 1j
print(cmath.acos(z))  # (0.9045568943023813-1.0612750619050357j)

asin

Returns the arc sine of a complex number.

z = 1 + 1j
print(cmath.asin(z))  # (0.6662394324925153+1.0612750619050357j)

atan

Returns the arc tangent of a complex number.

z = 1 + 1j
print(cmath.atan(z))  # (1.0172219678978514+0.40235947810852507j)

cos

Returns the cosine of a complex number.

z = 1 + 1j
print(cmath.cos(z))  # (0.8337300251311491-0.9888977057628651j)

sin

Returns the sine of a complex number.

z = 1 + 1j
print(cmath.sin(z))  # (1.2984575814159773+0.6349639147847361j)

tan

Returns the tangent of a complex number.

z = 1 + 1j
print(cmath.tan(z))  # (0.27175258531951174+1.0839233273386946j)

Hyperbolic Functions

acosh

Returns the inverse hyperbolic cosine of a complex number.

z = 1 + 1j
print(cmath.acosh(z))  # (1.0612750619050357+0.9045568943023813j)

asinh

Returns the inverse hyperbolic sine of a complex number.

z = 1 + 1j
print(cmath.asinh(z))  # (0.6662394324925153+0.7853981633974483j)

atanh

Returns the inverse hyperbolic tangent of a complex number.

z = 1 + 1j
print(cmath.atanh(z))  # (0.40235947810852507+1.0172219678978514j)

cosh

Returns the hyperbolic cosine of a complex number.

z = 1 + 1j
print(cmath.cosh(z))  # (0.8337300251311491+0.9888977057628651j)

sinh

Returns the hyperbolic sine of a complex number.

z = 1 + 1j
print(cmath.sinh(z))  # (0.6349639147847361+1.2984575814159773j)

tanh

Returns the hyperbolic tangent of a complex number.

z = 1 + 1j
print(cmath.tanh(z))  # (1.0839233273386946+0.27175258531951174j)

Examples

Complex Exponential

Calculate the exponential of a complex number.

import cmath

z = 2 + 3j
print(cmath.exp(z))  # (-7.315110094901103+1.0427436562359045j)

Polar and Rectangular Conversion

Convert between polar and rectangular coordinates.

z = 1 + 1j
r, phi = cmath.polar(z)
print(r, phi)  # 1.4142135623730951 0.7853981633974483

z_rect = cmath.rect(r, phi)
print(z_rect)  # (1.0000000000000002+1j)

Complex Logarithm

Calculate the natural logarithm of a complex number.

z = 2 + 3j
print(cmath.log(z))  # (1.2824746787307684+0.982793723247329j)

Complex Trigonometry

Calculate the sine and cosine of a complex number.

z = 2 + 3j
print(cmath.sin(z))  # (9.15449914691143-4.168906959966565j)
print(cmath.cos(z))  # (-4.189625690968807-9.109227893755337j)

Real-World Use Case

Signal Processing

In signal processing, complex numbers are used to represent signals and their transformations. For example, the Fourier Transform, which decomposes a signal into its constituent frequencies, uses complex numbers extensively.

import cmath
import math

def discrete_fourier_transform(signal):
    N = len(signal)
    result = []
    for k in range(N):
        s = complex(0)
        for t in range(N):
            angle = 2j * cmath.pi * t * k / N
            s += signal[t] * cmath.exp(-angle)
        result.append(s)
    return result

# Example usage
signal = [math.sin(2 * math.pi * t / 8) for t in range(8)]
dft_result = discrete_fourier_transform(signal)
print(dft_result)

Conclusion

The cmath module in Python provides a wide range of mathematical functions and constants for complex numbers. By utilizing these functions, you can perform complex arithmetic, trigonometry, logarithms, and more with ease and precision.

References

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare