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
- Complex Number Representation
- Complex numbers can be represented as
complex(real, imag)or by directly writing them asa + bj, wherejis the imaginary unit in Python.
z = 3 + 4j z2 = complex(5, 6) print(z) # Output: (3+4j) print(z2) # Output: (5+6j) - Complex numbers can be represented as
- Mathematical Functions for Complex Numbers
cmath.sqrt(z): Returns the square root of a complex numberz.z = 4 + 9j print(cmath.sqrt(z)) # Output: (3.122499j+1.436012)cmath.exp(z): Returns the exponential ofz(e^z), whereeis 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 basee) ofz, 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 ofz.z = 100 + 0j print(cmath.log10(z)) # Output: (2+0j)cmath.sin(z): Returns the sine ofzradians.z = 1 + 1j print(cmath.sin(z)) # Output: (1.2984575814159773+0.6349639147847361j)cmath.cos(z): Returns the cosine ofzradians.z = 1 + 1j print(cmath.cos(z)) # Output: (0.8337300251311491-0.9888977057628651j)cmath.tan(z): Returns the tangent ofzradians.z = 1 + 1j print(cmath.tan(z)) # Output: (0.27175258531951174+1.0839233273386946j)cmath.asin(z): Returns the arc sine ofzin radians.z = 1 + 1j print(cmath.asin(z)) # Output: (0.6662394324925153+1.0612750619050357j)cmath.acos(z): Returns the arc cosine ofzin radians.z = 1 + 1j print(cmath.acos(z)) # Output: (0.9045568943023813-1.0612750619050357j)cmath.atan(z): Returns the arc tangent ofzin radians.z = 1 + 1j print(cmath.atan(z)) # Output: (1.0172219678978514+0.4023594781085251j)
- Polar and Rectangular Conversions
cmath.polar(z): Converts the complex numberzfrom rectangular coordinates (a + bj) to polar coordinates (r, φ), whereris 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)
- Hyperbolic Functions
cmath.sinh(z): Returns the hyperbolic sine ofz.z = 1 + 1j print(cmath.sinh(z)) # Output: (0.6349639147847361+1.2984575814159773j)cmath.cosh(z): Returns the hyperbolic cosine ofz.z = 1 + 1j print(cmath.cosh(z)) # Output: (0.8337300251311491+0.9888977057628651j)cmath.tanh(z): Returns the hyperbolic tangent ofz.z = 1 + 1j print(cmath.tanh(z)) # Output: (1.0839233273386946+0.27175258531951174j)
- Phase and Magnitude
cmath.phase(z): Returns the phase (angle) of the complex numberzin radians.z = 1 + 1j print(cmath.phase(z)) # Output: 0.7853981633974483 (pi/4)abs(z): Returns the magnitude (modulus) of the complex numberz.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.141592653589793cmath.e: The mathematical constant e, approximately 2.71828.print(cmath.e) # Output: 2.718281828459045cmath.inf: Represents positive infinity in complex numbers.print(cmath.inf) # Output: infcmath.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.