In :

%pylab inline




Populating the interactive namespace from numpy and matplotlib



# Complex numbers

A complex number has the property that multipied by itself get a negative answer. For example, if an imaginary number like z could be -1

$$j \cdot j = -1$$

then, the imaginary operatior $j$ is

$$j= \sqrt{-1}$$

Then, a complex numbers is a number that, when squared gives a negative result.



In :

# initiation and examples
z = complex(3,4)

print('The complex {}, where {} is the real and {} the imaginary part'.format(z, z.real, z.imag))




The complex (3+4j), where 3.0 is the real and 4.0 the imaginary part



A complex number give us the distance to the (0,0) origin and the angle to the positive axis



In :

z = 1+1j # alternative way to complex numbers
dist = abs(z)
angle = np.angle(z, deg = True)

print('distance = {:2.4f}, angle = {} deg'.format(dist, angle))




distance = 1.4142, angle = 45.0 deg




In :

# distance with trigonometry formula
dist = np.sqrt( np.power(z.imag,2) + np.power(z.real,2))

print('distance = {:2.4f}, angle = {} deg'.format(dist, angle))




distance = 1.4142, angle = 45.0 deg



If we multiply a complex number by its conjugate, we obtain a new complex number, whose module is the square of the complex number module



In :

z2 = z * z.conjugate()
np.abs(np.sqrt(z2))




Out:

1.4142135623730951



# The complex plane

It's a plane for complex numbers, where we have real and imaginary axis in the plane. We can think of a complex number as a vector (a,bi) or we can express a complex number in its polar form with its module and angle (m, $\phi$). For example, (3+4j) can be also expressed as (5, 0.9273 rad).



In :

z = (3+4j)
m = np.abs(z)
phi = np.angle(z)
print('cartesian coordinates = ({:0.0f}, {:0.0f})'.format(z.real, z.imag))
print('polar coordinates     = ({:0.0f}, {:2.4f})'.format(m, phi))
plt.plot(z.real, z.imag, 'ro', ms =12)
plt.plot([0,z.real],[0, z.imag], 'r-', lw=2)
plt.grid(True)
plt.axis([-5,5,-5,5], option = 'square');




cartesian coordinates = (3, 4)
polar coordinates     = (5, 0.9273)



# Euler's formula

We can use $m e^{j\phi}$, where $m$ is the distance to the origin, and $\phi$ the angle with the ordinal axis to express any irrational number in a circle with radius m. This alternative way to a complex numbers turns out to be very useful, because there are many cases, like multiplication, where it is easier to deal with exponentials than with standard complex numbers.

Euler's formula also provides a link between trigonometry and imaginary numbers.

$$e^{ \pm j\theta } = \cos \theta \pm j\sin \theta$$

It describes the vector from the origin to some point of the circle with radius one. If you want to have larger than one vector, we can use:

$$m e^{ \pm j\theta } = m \cos \theta \pm m j\sin \theta$$

Then, we can have complex waves simply expressed as $m e^{j\theta }$



In :

# trigonometric relation
m = np.abs(z)
phi = np.angle(z)

x = m*np.cos(phi)
y = m*np.sin(phi)
print('({} + {}j) is {}e^{:2.4f}j '.format(z.real, z.imag, m, phi))




(3.0 + 4.0j) is 5.0e^0.9273j



Raising a complex number to the natural log base (e) gives a vector with absolute value of 1 and an angle of the real component of the number. Thus,$e^{j\phi}$ is an efficient way to represent oscillatory information using polar notation.



In [ ]: