Python cMath Module

The cmath module in Python provides mathematical functions for complex numbers. It is similar to the math module but works specifically with complex numbers. Complex numbers have real and imaginary components, represented as a + bj, where a is the real part and b is the imaginary part.

Importing the cmath Module

To use the cmath module, you need to import it:

import cmath

Key Features of cmath

  1. Complex Number Representation
    • Complex numbers can be represented as complex(real, imag) or by directly writing them as a + bj, where j is the imaginary unit in Python.
    z = 3 + 4j
    z2 = complex(5, 6)
    print(z)  # Output: (3+4j)
    print(z2)  # Output: (5+6j)
    
  2. Mathematical Functions for Complex Numbers
    • cmath.sqrt(z): Returns the square root of a complex number z.
      z = 4 + 9j
      print(cmath.sqrt(z))  # Output: (3.122499j+1.436012)
      
    • cmath.exp(z): Returns the exponential of z (e^z), where e is the mathematical constant.
      z = 1 + 2j
      print(cmath.exp(z))  # Output: (-1.1312043837568135+2.4717266720048188j)
      
    • cmath.log(z, base): Returns the natural logarithm (logarithm to the base e) of z, or optionally to the given base.
      z = 2 + 3j
      print(cmath.log(z))  # Output: (1.2824746787307684+0.982793723247329j)
      
    • cmath.log10(z): Returns the base-10 logarithm of z.
      z = 100 + 0j
      print(cmath.log10(z))  # Output: (2+0j)
      
    • cmath.sin(z): Returns the sine of z radians.
      z = 1 + 1j
      print(cmath.sin(z))  # Output: (1.2984575814159773+0.6349639147847361j)
      
    • cmath.cos(z): Returns the cosine of z radians.
      z = 1 + 1j
      print(cmath.cos(z))  # Output: (0.8337300251311491-0.9888977057628651j)
      
    • cmath.tan(z): Returns the tangent of z radians.
      z = 1 + 1j
      print(cmath.tan(z))  # Output: (0.27175258531951174+1.0839233273386946j)
      
    • cmath.asin(z): Returns the arc sine of z in radians.
      z = 1 + 1j
      print(cmath.asin(z))  # Output: (0.6662394324925153+1.0612750619050357j)
      
    • cmath.acos(z): Returns the arc cosine of z in radians.
      z = 1 + 1j
      print(cmath.acos(z))  # Output: (0.9045568943023813-1.0612750619050357j)
      
    • cmath.atan(z): Returns the arc tangent of z in radians.
      z = 1 + 1j
      print(cmath.atan(z))  # Output: (1.0172219678978514+0.4023594781085251j)
      
  3. Polar and Rectangular Conversions
    • cmath.polar(z): Converts the complex number z from rectangular coordinates (a + bj) to polar coordinates (r, φ), where r is the magnitude and φ is the phase angle (in radians).
      z = 1 + 1j
      print(cmath.polar(z))  # Output: (1.4142135623730951, 0.7853981633974483)
      
    • cmath.rect(r, phi): Converts a complex number from polar coordinates to rectangular form (a + bj).
      r, phi = 1.4142135623730951, 0.7853981633974483
      print(cmath.rect(r, phi))  # Output: (1+1j)
      
  4. Hyperbolic Functions
    • cmath.sinh(z): Returns the hyperbolic sine of z.
      z = 1 + 1j
      print(cmath.sinh(z))  # Output: (0.6349639147847361+1.2984575814159773j)
      
    • cmath.cosh(z): Returns the hyperbolic cosine of z.
      z = 1 + 1j
      print(cmath.cosh(z))  # Output: (0.8337300251311491+0.9888977057628651j)
      
    • cmath.tanh(z): Returns the hyperbolic tangent of z.
      z = 1 + 1j
      print(cmath.tanh(z))  # Output: (1.0839233273386946+0.27175258531951174j)
      
  5. Phase and Magnitude
    • cmath.phase(z): Returns the phase (angle) of the complex number z in radians.
      z = 1 + 1j
      print(cmath.phase(z))  # Output: 0.7853981633974483 (pi/4)
      
    • abs(z): Returns the magnitude (modulus) of the complex number z.
      z = 1 + 1j
      print(abs(z))  # Output: 1.4142135623730951
      

Constants in cmath

  • cmath.pi: The mathematical constant pi (π), approximately 3.14159.
    print(cmath.pi)  # Output: 3.141592653589793
    
  • cmath.e: The mathematical constant e, approximately 2.71828.
    print(cmath.e)  # Output: 2.718281828459045
    
  • cmath.inf: Represents positive infinity in complex numbers.
    print(cmath.inf)  # Output: inf
    
  • cmath.nan: Represents “Not a Number” (NaN).
    print(cmath.nan)  # Output: nan
    

Example Usage

import cmath

# Complex number
z = 1 + 2j

# Square root of z
print("Square root:", cmath.sqrt(z))  # Output: (1.272019649514069+0.7861513777574233j)

# Exponential of z
print("Exponential:", cmath.exp(z))  # Output: (-1.1312043837568135+2.4717266720048188j)

# Logarithm of z
print("Logarithm:", cmath.log(z))  # Output: (0.8047189562170503+1.1071487177940904j)

# Convert to polar coordinates
print("Polar coordinates:", cmath.polar(z))  # Output: (2.23606797749979, 1.1071487177940904)

# Phase of the complex number
print("Phase:", cmath.phase(z))  # Output: 1.1071487177940904 radians

The cmath module is essential for performing mathematical operations on complex numbers, enabling a variety of calculations related to exponential, logarithmic, and trigonometric functions.

Leave a Reply 0

Your email address will not be published. Required fields are marked *