← Back to Chapters

Python cmath Module

? Python cmath Module

⚡ Quick Overview

The cmath module provides mathematical functions for complex numbers in Python. It is similar to the math module, but all functions are designed to work with values that may have both real and imaginary parts.

Use cmath whenever you are dealing with operations like magnitude, phase, trigonometric, exponential, or logarithmic functions on complex numbers.

? Key Concepts

  • Complex numbers are written as a + bj in Python (e.g., 3 + 4j).
  • Real part and imaginary part are accessed using z.real and z.imag.
  • Conjugate of a + bj is a - bj and is obtained using z.conjugate().
  • Magnitude (or modulus) is the distance from origin: use abs(z).
  • Phase is the angle (in radians) of the complex number: use cmath.phase(z).
  • Polar formrectangular form using cmath.polar() and cmath.rect().
  • Trigonometric functions like sin, cos, exp, log in cmath support complex inputs.

? Syntax and Theory

To use the cmath module, you must first import it:

import cmath

A complex number can be created directly using the literal syntax:

z = 3 + 4j

For a complex number z = x + yj:

  • z.real → returns x (floating point).
  • z.imag → returns y (floating point).
  • z.conjugate() → returns x - yj.
  • abs(z) → returns √(x² + y²), the magnitude.

Polar representation of z is (r, θ), where:

  • r is the magnitude.
  • θ is the phase angle in radians.

? Code Examples

? Importing cmath and creating a complex number
import cmath

z = 3 + 4j
print(z)       # (3+4j)
? Accessing real, imaginary, conjugate, and magnitude
z = 3 + 4j

print(z.real)        # 3.0
print(z.imag)        # 4.0
print(z.conjugate()) # (3-4j)
print(abs(z))        # 5.0 (magnitude)
? Phase, polar coordinates, and rectangular form
import cmath

z = 3 + 4j

print(cmath.phase(z))         # 0.9272952180016122 (angle in radians)
r, theta = cmath.polar(z)
print(r, theta)               # 5.0 0.9272952180016122
print(cmath.rect(r, theta))   # (3.0000000000000004+4j)
? Trigonometric, square root, exponential and logarithm
import cmath

z = 3 + 4j

print(cmath.sqrt(z))   # (2+1j)
print(cmath.exp(z))    # (-13.128783081462158-15.200784463067954j)
print(cmath.log(z))    # (1.6094379124341003+0.9272952180016122j)
print(cmath.sin(z))    # (3.853738037919377+27.016813258003932j)
print(cmath.cos(z))    # (-27.034945603074224-3.851153334811777j)
? Hyperbolic functions for complex numbers
import cmath

z = 3 + 4j

print(cmath.sinh(z))   # (-6.548120040911002+7.61923172032141j)
print(cmath.cosh(z))   # (-6.580663718241853+7.581552742746153j)
print(cmath.tanh(z))   # (1.0001871061082424+0.0037640256415043743j)

? Live Output and Explanation

? Understanding the example with z = 3 + 4j

  • print(z) shows the complex number in standard form: (3+4j).
  • z.real returns 3.0 and z.imag returns 4.0.
  • z.conjugate() flips the sign of the imaginary part, giving (3-4j).
  • abs(z) computes √(3² + 4²) = 5.0, the magnitude of the vector.
  • cmath.phase(z) returns the angle in radians between the positive real axis and the vector z.
  • cmath.polar(z) returns (r, θ), where r = 5.0 and θ ≈ 0.9273.
  • cmath.rect(r, θ) reconstructs the complex number from polar form back to approximately 3 + 4j.
  • Functions like sqrt, exp, log, sin, and cos return complex outputs even if inputs are complex.

? Tips and Best Practices

  • Use abs(z) to get the magnitude of a complex number.
  • Use cmath.phase(z) to get the angle in radians.
  • Use cmath.polar(z) and cmath.rect(r, theta) to switch between polar and rectangular form.
  • Prefer cmath over math when working with complex numbers; math is for real numbers only.
  • Remember that angles returned by cmath functions are in radians, not degrees.
  • Be mindful that many cmath functions always return a complex result, even if the imaginary part is 0j.

? Use Cases / When to Use

  • Electrical engineering (AC circuits, impedance calculations).
  • Signal processing and Fourier transforms.
  • Control systems and stability analysis.
  • Quantum mechanics and physics simulations.
  • Any domain where values are naturally represented as complex numbers.

? Try It Yourself

  • Create a complex number 5 + 12j and find its magnitude using abs() and phase using cmath.phase().
  • Convert 5 + 12j into polar coordinates with cmath.polar() and then back to rectangular form with cmath.rect().
  • Calculate sqrt, sin, and cos of a complex number 2 + 3j using cmath.
  • Compute exp(1 + 1j) and log(1 + 1j), and observe how real and imaginary parts change.
  • Write a function that takes a complex number and prints its magnitude, phase, polar form, and conjugate.