Python - Complex Numbers

> Procedural Languages > Python > Python - Data Type

1 - About

complex numbers in Python

Python uses 'j' for the imaginary unit, not 'i'.

3 - Snippet

  • Type Complex
>>> 1j
1j
>>> type(1j)
<class 'complex'>
  • Addition
>>> (3 + 1j) + (2 + 2j)
(5+3j)
>>> x = 1+3j
>>> x + 1
(2+3j)
Advertising

4 - Complex Plane

The real and imaginary parts of a complex number can be interpreted as x and y coordinates in the complex plane forming a point.

  • Coordinates
>>> x = 1+3j
>>> x.real # real number coordinates
1.0
>>> x.imag # imaginary number coordinates
3.0
  • Plot
import matplotlib.pyplot as plt
L=[-2+2j,-1+2j,0+2j,1+2j,2+2j,-1+4j,0+4j,1+4j]
X = [x.real for x in L]
Y = [x.imag for x in L]
plt.scatter(X,Y, color='red')
plt.show()

  • Absolute value of z = distance from the origin to the point z in the complex plane. (In Mathematics - Mathese, |z|)

>>> abs(1+2j)
2.23606797749979

5 - Translation

  • Geometric interpretation of f (z) = z + (1+2i)?
  • Increase each real coordinate by 1 and increases each imaginary coordinate by 2.
  • A translation can “move” the picture anywhere in the complex plane

Translation in general:

f (z) = z + z0

where:

Advertising

6 - Arrow

  • Complex Number as an arrow

7 - Composition (Addition)

They correspond to translations f1(z) = z + z1 and f2(z) = z + z2. Functional composition: (f1 circ f2)(z) = 2z + z1 + z2

8 - Multiplication

8.1 - Scaling

Multiplying complex numbers by a positive real number

8.2 - Reflection

plot({-1*z for z in L})

8.3 - By i: rotation by 90 degrees

x+yi right −y + x{i}

i(x + y{i}) = x{i} + y{i^2} = x{i} − y

f(z) = i{z}

plot({1j*z for z in L})
Advertising

8.4 - Rotation

Rotating a complex number z means increasing its argument.

Argument of z is the angle in radians between z arrow and the x axis (1 + 0i arrow).

Euler’s formula: For any real number theta, e^{theta{i}} is the point z on the unit circle with argument theta.

e = 2.718281828…

When theta = pi, z = -1

Every complex number can be written in the form z = r*e^{theta{i}} where:

  • r is the absolute value of z
  • theta is the argument of z

To augment the argument of z, we use exponentiation law <math>e^a * e^b = e^{a+b}</math>

  • r.e^{theta{i}} * e^{tau{i}} = r.e^(theta+tau){i}
  • f(z) = z . e^{tau{i}} does a rotation by angle tau
  • Rotation of 45 degrees
from math import e, pi
plot({e**(45j)*z for z in L})
  • Circle with a rayon of 2
r = 2
circle = 2*pi
plot([r*e**(t*circle/20*1j) for t in range(20)])