cmath — Complex math functions

This module implements a subset of the corresponding CPython module, as described below. For more information, please refer to the original CPython documentation: cmath

This module provides functions for performing mathematical operations on complex numbers.

Availability: not available on WiPy and ESP8266. This module requires floating-point support.

Quick usage example:

import cmath

# Complex number operations
z = 1 + 2j
print('z =', z)
print('abs(z) =', abs(z))
print('phase(z) =', cmath.phase(z))
print('polar(z) =', cmath.polar(z))
print('rect(1, cmath.pi/2) =', cmath.rect(1, cmath.pi/2))

# Trigonometric functions
print('sin(z) =', cmath.sin(z))
print('cos(z) =', cmath.cos(z))
print('tan(z) =', cmath.tan(z))

# Exponentials and logarithms
print('exp(z) =', cmath.exp(z))
print('log(z) =', cmath.log(z))
print('log10(z) =', cmath.log10(z))

# Square root
print('sqrt(z) =', cmath.sqrt(z))

Constants

cmath.e

The base of the natural logarithm, e, approximately equal to 2.718281828459045.

cmath.pi

The ratio of a circle’s circumference to its diameter, approximately equal to 3.141592653589793.

Functions

cmath.phase(x)

Return the phase (argument) of a complex number, in radians, in the range (-pi, +pi].

cmath.polar(x)

Return the polar representation of a complex number, as a tuple (r, phi), where r is the modulus and phi is the phase.

cmath.rect(r, phi)

Return the complex number with modulus r and phase phi.

cmath.sin(x)

Return the sine of the complex number.

cmath.cos(x)

Return the cosine of the complex number.

cmath.tan(x)

Return the tangent of the complex number.

cmath.exp(x)

Return e raised to the power of x.

cmath.log(x)

Return the natural logarithm of x (base e). The branch cut is along the negative real axis.

cmath.log10(x)

Return the base-10 logarithm of x. The branch cut is along the negative real axis.

cmath.sqrt(x)

Return the square root of x.