On this page
cmath
Prev: calendar | Up: stdlib_module | Next: cmd
Rel: numbers, math, decimal, fractions, random
Ref: docs.python.org — cmath
Tags: #public #python3 #stdlib
Import
import cmath
Overview
Mathematical functions for complex numbers (sqrt, exp, log, sin, cos, polar, rect, phase, isclose). Use cmath whenever square roots or logarithms of negative numbers should produce complex results instead of raising ValueError in math.
Quick Start
import cmath
assert cmath.sqrt(-4) == 2j
r, phi = cmath.polar(1 + 1j)
assert cmath.isclose(cmath.rect(r, phi), 1 + 1j)
Implementation Walkthrough
1. Solving General Quadratic Equations (Real or Complex Roots)
import cmath
def quadratic_roots(a: float, b: float, c: float) -> tuple[complex, complex]:
disc = cmath.sqrt(b * b - 4 * a * c)
return ((-b + disc) / (2 * a), (-b - disc) / (2 * a))
r1, r2 = quadratic_roots(1, 0, 1) # x^2 + 1 = 0
assert {r1, r2} == {1j, -1j}
2. Converting Between Rectangular & Polar Coordinates
import cmath
z = -1 + 0j
assert cmath.isclose(cmath.phase(z), cmath.pi)
assert cmath.isclose(cmath.exp(1j * cmath.pi), -1 + 0j)
Gotchas & Boundaries
cmathalways returnscomplex, even when the imaginary part is0j(cmath.sqrt(4) == (2+0j)).cmath.isclose(a, 0)needs an explicitabs_tol: Just likemath.isclose, defaultabs_tol=0.0fails near zero.- Complex numbers cannot be ordered with
<or>: Compare magnitudes withabs(z)instead.
Common Functions
- cmath.acos() —
acos(z, /)— Return the arc cosine of z. - cmath.acosh() —
acosh(z, /)— Return the inverse hyperbolic cosine of z. - cmath.asin() —
asin(z, /)— Return the arc sine of z. - cmath.asinh() —
asinh(z, /)— Return the inverse hyperbolic sine of z. - cmath.atan() —
atan(z, /)— Return the arc tangent of z. - cmath.atanh() —
atanh(z, /)— Return the inverse hyperbolic tangent of z. - cmath.cos() —
cos(z, /)— Return the cosine of z. - cmath.cosh() —
cosh(z, /)— Return the hyperbolic cosine of z. - cmath.exp() —
exp(z, /)— Return the exponential value e**z. - cmath.isclose() —
isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)— Determine whether two complex numbers are close in value. - cmath.isfinite() —
isfinite(z, /)— Return True if both the real and imaginary parts of z are finite, else False. - cmath.isinf() —
isinf(z, /)— Checks if the real or imaginary part of z is infinite. - cmath.isnan() —
isnan(z, /)— Checks if the real or imaginary part of z not a number (NaN). - cmath.log() —
log(z[, base]) -> the logarithm of z to the given base.— If the base is not specified, returns the natural logarithm (base e) of z. - cmath.log10() —
log10(z, /)— Return the base-10 logarithm of z. - cmath.phase() —
phase(z, /)— Return argument, also known as the phase angle, of a complex. - cmath.polar() —
polar(z, /)— Convert a complex from rectangular coordinates to polar coordinates. - cmath.rect() —
rect(r, phi, /)— Convert from polar coordinates to rectangular coordinates. - cmath.sin() —
sin(z, /)— Return the sine of z. - cmath.sinh() —
sinh(z, /)— Return the hyperbolic sine of z. - cmath.sqrt() —
sqrt(z, /)— Return the square root of z. - cmath.tan() —
tan(z, /)— Return the tangent of z. - cmath.tanh() —
tanh(z, /)— Return the hyperbolic tangent of z.
Constants & Attributes
cmath.e(float) =2.718281828459045cmath.inf(float) =infcmath.infj(complex) =infjcmath.nan(float) =nancmath.nanj(complex) =nanjcmath.pi(float) =3.141592653589793cmath.tau(float) =6.283185307179586
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/calendar
- tuts/python3/stdlib_module/cmath/acos
- tuts/python3/stdlib_module/cmath/acosh
- tuts/python3/stdlib_module/cmath/asin
- tuts/python3/stdlib_module/cmath/asinh
- tuts/python3/stdlib_module/cmath/atan
- tuts/python3/stdlib_module/cmath/atanh
- tuts/python3/stdlib_module/cmath/cos
- tuts/python3/stdlib_module/cmath/cosh
- tuts/python3/stdlib_module/cmath/exp
- tuts/python3/stdlib_module/cmath/isclose
- tuts/python3/stdlib_module/cmath/isfinite
- tuts/python3/stdlib_module/cmath/isinf
- tuts/python3/stdlib_module/cmath/isnan
- tuts/python3/stdlib_module/cmath/log
- tuts/python3/stdlib_module/cmath/log10
- tuts/python3/stdlib_module/cmath/phase
- tuts/python3/stdlib_module/cmath/polar
- tuts/python3/stdlib_module/cmath/rect
- tuts/python3/stdlib_module/cmath/sin
- tuts/python3/stdlib_module/cmath/sinh
- tuts/python3/stdlib_module/cmath/sqrt
- tuts/python3/stdlib_module/cmath/tan
- tuts/python3/stdlib_module/cmath/tanh
- tuts/python3/stdlib_module/cmd
- tuts/python3/stdlib_module/decimal
- tuts/python3/stdlib_module/fractions
- tuts/python3/stdlib_module/math
- tuts/python3/stdlib_module/numbers
- tuts/python3/stdlib_module/random
- tuts/python3/stdlib_module/statistics
- tuts/python3/stdlib_module/stdlib_module
Related notes
- tuts/python3/stdlib_module/calendar Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/cmath/acos Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/cmath/acosh Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/cmath/asin Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/cmath/asinh Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/cmath/atan Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/cmath/atanh Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/cmath/cos Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/cmath/cosh Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/cmath/exp Linked here; links to this page; shared tags: #stdlib