Learning how to use complex numbers with python
Learning how to use complex numbers with python¶
I've recntly appreciated that complex numbers are not just for mathematicians, but have have far reaching applications in applied physics and engineering. I am interested in using complex numbers for dynamics work, as well as vectors, but find them fasinating. Euler's Idendity is my favorite equation and I feel an obligation to understand it better.
eulers identity¶
$$ {e^{i \pi}} + 1 = 0 $$How can something so seemingly simple be so powerful? Lets take a look.
Python has a built in complex math library, but I am just going to use numpy.
# numpy library
import numpy as np
import matplotlib.pyplot as pl
Lets define 2 real numbers
x = 2
y = 4
and we can define a single, new number, but it is complex, python uses j to indicate the imaginary number
z = complex(x,y)
z
Here is another way to define a complex number that is slightly less verbose, but a little more cryptic
z = x + y*1j
z
to view the real and imaginary parts, use the attributes
z.real
z.imag
To find the magnitude, or modulus of the complex number you can use
zmag = np.sqrt( z.real**2 + z.imag**2 )
zmag
or
zmag = np.abs(z)
zmag
or using the conjugate property
zmag = np.sqrt(z * z.conjugate() )
zmag
to get angle information of our vectors, we can use the phase() method
zang = np.angle(z)
zang, np.rad2deg(zang)
and if you still can't let go of trig yet, here is a check to confirm the angle
np.arctan(4/2)
so our vector is about 63 deg, that looks about right
pl.plot([0, z.real], [0, z.imag],'-o')
pl.title('Our complex number')
pl.xlabel('Re')
pl.ylabel('Im')
pl.xlim([-5, 5])
pl.ylim([-5, 5])
the complex conjugate sounds scary, but simply reverses the sign which flips the vertical, imginary axis
zc = z.conjugate()
zc
pl.plot([0, z.real], [0, z.imag],'-o', label= 'first complex number')
pl.plot([0, zc.real], [0, zc.imag],'-o', label='conjugate')
pl.xlim([-5, 5])
pl.ylim([-5, 5])
pl.title('complex number with complex conjugate')
pl.xlabel('Re')
pl.ylabel('Im')
pl.legend()
another amazing property of complex numbers is the ease of rotating vectors. This can be acomplished by multiplying by j for 90 deg, pi/2 rotation
zr = z * 1j
zr
pl.plot([0, zr.real], [0, zr.imag],'-o')
pl.title('complex number rotated by j, or pi/2')
pl.xlabel('Re')
pl.ylabel('Im')
pl.xlim([-5, 5])
pl.ylim([-5, 5])
Arrays of complex numbers are just as easy to handle. Let's create an numpy array of complex numbers, inspect the properties, and plot them
# numpy complex
a = np.array([1+2j, 3+4j, 4.7+2j])
a
a.imag, a.real
np.angle(a)
np.conjugate(a)
np.abs(a)
for z in a:
pl.plot([0, z.real], [0, z.imag],'-o')
pl.plot([0, zc.real], [0, zc.imag],'-o')
pl.xlim([-5, 5])
pl.ylim([-5, 5])
pl.title('array of complex numbers plotted')
pl.xlabel('Re')
pl.ylabel('Im')
And finally, I think the most incredible part of eulers equation is the ability to represent oscillations, or sinusoidal functions. Let's create an array of 24 numbers from 0-2pi and from that, using euler's idendity, create 24 complex numbers and see what happens
n = 24
narr = np.linspace(0, 2*np.pi, n+1)
narr, np.rad2deg(narr)
w = np.e ** ( narr * 1j )
w
PLotting the real and imaginary part reveals the beauty of the equation. We can easily create sinusoidal motion with this equation!
pl.plot(w.real, '-o', label='Re')
pl.plot(w.imag, '-o', label='Im')
pl.legend()
pl.title('eulers idenity components')
pl.plot(w.real, w.imag, '-o')
pl.xlabel('Re')
pl.ylabel('Im')
#pl.title(r'plotting $ \frac{ {e^{k2\pi i}} }{{n}}$a for n=30 and k=[0,n]',fontsize=20)
pl.title(r'plotting $e^{kj}$ for n=24 and k=[0,2$\pi$]',fontsize=18)
of course, this is evident from the complete idendity which has a cos() as the horizontal and the vertical is a sin()
$$ {e^{kj}} = cos(k) + j*sin(k) $$Thanks for reading and stay curious!
## bonus, simple way to export your notebook to html
# !jupyter nbconvert "complex math notes with python.ipynb" --to html
Comments
Comments powered by Disqus