Python Reference ยท Standard Library: Math, Data and Text
The cmath module
Reference for Python's cmath module: square roots of negative numbers, polar and rectangular form, phase, exp, log, trigonometry and isclose for complex numbers.
The cmath module is the complex-number twin of math. Its functions accept ints, floats and complex numbers and return complex results where needed, so cmath.sqrt(-4) gives 2j instead of raising an error like math.sqrt(-4).
Complex numbers are built into Python: write 3 + 4j or call complex(3, 4). The imaginary unit is j, not i. The built-in abs() returns a complex number's magnitude, and the .real, .imag attributes and .conjugate() method give its parts. cmath adds everything else: roots, exponentials, logarithms, trigonometry and conversions to polar form.
import cmath print(cmath.sqrt(-4))
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Frequently used names
| Name | Description |
|---|---|
sqrt(x) |
Principal square root. sqrt(-1) is 1j. |
exp(x), log(x[, base]), log10(x) |
Exponential and logarithms. log of a negative number returns a complex result with an imaginary part of pi. |
polar(x) |
Returns (r, phi): magnitude and phase in radians. |
rect(r, phi) |
Builds a complex number from polar coordinates. |
phase(x) |
The angle, in the range -pi to pi. |
sin, cos, tan, asin, acos, atan, sinh, cosh, tanh and inverses |
Trigonometric and hyperbolic functions on complex arguments. |
isfinite(x), isinf(x), isnan(x) |
True when both parts are finite; either part is infinite; either part is NaN. |
isclose(a, b, rel_tol=1e-09, abs_tol=0.0) |
Tolerant equality for complex numbers. |
pi, e, tau, inf, infj, nan, nanj |
Constants (infj is inf times j). |
Usage notes
- Equality is exact. Results such as
cmath.sqrt(-1) ** 2are(-1+0j)and small rounding errors are common, so compare withcmath.isclose(). - Branch cuts. Functions like
sqrtandlogreturn the principal value. A complex number has two square roots and infinitely many logarithms;cmathpicks one, and the choice jumps across the negative real axis. - Real results are still complex.
cmath.sqrt(4)is(2+0j), not2.0. Usemathwhen you only want real numbers. - Complex numbers cannot be ordered.
1j < 2jraisesTypeError, andmax()orsorted()need akey, for exampleabs. - Operators already work.
+,-,*,/and**accept complex numbers without any import;//,%anddivmod()do not. - Strings.
complex("1+2j")parses text, but there must be no spaces around the+. - Performance. For arrays of complex numbers, NumPy is much faster and also supports complex dtypes.