Complex Numbers in Python
Python provides a built-in type for handling complex numbers, the complex
type.
For basic calculations with complex numbers, importing a module isn't necessary. However, for advanced functions, you can import the cmath
module.
Create the complex
object
In Python, the imaginary unit for complex numbers is represented as j
, not i
.
c = 3 + 4j
print(c)
# (3+4j)
print(type(c))
# <class 'complex'>
When the imaginary part is 1
, you must explicitly write 1j
; otherwise, a NameError
will occur. If a variable named j
is already defined, it will be treated as that variable.
# c = 3 + j
# NameError: name 'j' is not defined
c = 3 + 1j
print(c)
# (3+1j)
If the real part is 0
, you can omit it.
c = 3j
print(c)
# 3j
To define a complex
value with an imaginary part of 0
, write 0
explicitly. You can also perform operations between complex
, int
, and float
types, as detailed below.
c = 3 + 0j
print(c)
# (3+0j)
You can specify the real and imaginary parts as float
values. Exponential notation is also allowed.
c = 1.2e3 + 3j
print(c)
# (1200+3j)
You can also create complex numbers using the constructor, complex()
, which takes the real and imaginary parts as arguments.
c = complex(3, 4)
print(c)
# (3+4j)
Get the real and imaginary parts: real
, imag
You can access the real and imaginary parts of complex
with the real
and imag
attributes. Both are float
.
c = 3 + 4j
print(c.real)
print(type(c.real))
# 3.0
# <class 'float'>
print(c.imag)
print(type(c.imag))
# 4.0
# <class 'float'>
These attributes are read-only, so you cannot modify them.
# c.real = 5.5
# AttributeError: readonly attribute
Get the complex conjugate: conjugate()
Use the conjugate()
method to get the complex conjugate of a complex number.
c = 3 + 4j
print(c.conjugate())
# (3-4j)
Get the absolute value: abs()
Use the built-in abs()
function to get the absolute value (modulus) of a complex number.
c = 3 + 4j
print(abs(c))
# 5.0
c = 1 + 1j
print(abs(c))
# 1.4142135623730951
Get the phase: cmath.phase()
Use the cmath.phase()
function to get the phase (argument) of a complex number.
cmath.phase(x)
is equivalent to math.atan2(x.imag, x.real)
.
import cmath
import math
c = 1 + 1j
print(cmath.phase(c))
# 0.7853981633974483
print(cmath.phase(c) == math.atan2(c.imag, c.real))
# True
The returned angle is in radians. To convert the angle to degrees, use the math.degrees()
function.
print(math.degrees(cmath.phase(c)))
# 45.0
Polar and Cartesian conversions: cmath.polar()
, cmath.rect()
Use the cmath.polar()
function to obtain the polar coordinates of a complex number.
cmath.polar()
returns a tuple (absolute value, phase)
which is equivalent to (abs(x), cmath.phase(x))
.
import cmath
import math
c = 1 + 1j
print(cmath.polar(c))
# (1.4142135623730951, 0.7853981633974483)
print(cmath.polar(c)[0] == abs(c))
# True
print(cmath.polar(c)[1] == cmath.phase(c))
# True
To convert from polar to Cartesian coordinates, use the cmath.rect()
function. Pass the absolute value and phase as arguments to obtain the equivalent complex number.
print(cmath.rect(1, 0))
# (1+0j)
print(cmath.rect(1, math.pi / 2))
# (6.123233995736766e-17+1j)
Given a complex number with an absolute value r
and a phase ph
, its real part can be computed as r * math.cos(ph)
. The imaginary part is given by r * math.sin(ph)
.
r = math.sqrt(2)
ph = math.pi / 4
print(cmath.rect(r, ph))
# (1.0000000000000002+1j)
print(cmath.rect(r, ph).real == r * math.cos(ph))
# True
print(cmath.rect(r, ph).imag == r * math.sin(ph))
# True
Arithmetic with complex numbers
You can use standard arithmetic operators on complex numbers.
c1 = 3 + 4j
c2 = 2 - 1j
print(c1 + c2)
# (5+3j)
print(c1 - c2)
# (1+5j)
print(c1 * c2)
# (10+5j)
print(c1 / c2)
# (0.4+2.2j)
print(c1**3)
# (-117+44j)
You can also perform arithmetic operations between complex
, int
, and float
types.
c = 3 + 4j
print(c + 3)
# (6+4j)
print(c * 0.5)
# (1.5+2j)
The square root can be calculated using **0.5
, though this may introduce inaccuracies. For more precise results, consider using the cmath.sqrt()
function.
import cmath
print((-3 + 4j) ** 0.5)
# (1.0000000000000002+2j)
print((-1) ** 0.5)
# (6.123233995736766e-17+1j)
print(cmath.sqrt(-3 + 4j))
# (1+2j)
print(cmath.sqrt(-1))
# 1j