# Lecture 8

## Complex Numbers


```python
import numpy as np
import sympy as sp
import scipy.integrate
sp.init_printing()
##################################################
##### Matplotlib boilerplate for consistency #####
##################################################
from ipywidgets import interact
from ipywidgets import IntSlider
from ipywidgets import FloatSlider
from matplotlib import pyplot as plt

import cmath

%matplotlib inline

from IPython.display import set_matplotlib_formats
set_matplotlib_formats('svg')

global_fig_width = 8
global_fig_height = global_fig_width / 1.61803399
font_size = 12

plt.rcParams['axes.axisbelow'] = True
plt.rcParams['axes.edgecolor'] = '0.8'
plt.rcParams['axes.grid'] = True
plt.rcParams['axes.labelpad'] = 8
plt.rcParams['axes.linewidth'] = 2
plt.rcParams['axes.titlepad'] = 16.0
plt.rcParams['axes.titlesize'] = font_size * 1.4
plt.rcParams['figure.figsize'] = (global_fig_width, global_fig_height)
plt.rcParams['font.sans-serif'] = ['Computer Modern Sans Serif', 'DejaVu Sans', 'sans-serif']
plt.rcParams['font.size'] = font_size
plt.rcParams['grid.color'] = '0.8'
plt.rcParams['grid.linestyle'] = 'dashed'
plt.rcParams['grid.linewidth'] = 2
plt.rcParams['lines.dash_capstyle'] = 'round'
plt.rcParams['lines.dashed_pattern'] = [1, 4]
plt.rcParams['xtick.labelsize'] = font_size
plt.rcParams['xtick.major.pad'] = 4
plt.rcParams['xtick.major.size'] = 0
plt.rcParams['ytick.labelsize'] = font_size
plt.rcParams['ytick.major.pad'] = 4
plt.rcParams['ytick.major.size'] = 0
##################################################
```

## Imaginary numbers

**The Imaginary Number $i$:**

- The polynomial $~~x^2-1=0~~$ has two real roots:  $~~1~~$ and $~~-1~~$
- The polynomial $~~x^2+1=0~~$ has **no** real roots.

Consider solving:
$$x^2 = 1\qquad\mbox{and}\qquad x^2 = -1$$

- We introduce an "imaginary" number $~~i~~$ so that there are two solutions to $~~x^2 = -1~~$: $~~i~~$ and $~~-i~~$.  That is, $~~i~~$ is a number with the property that $~~i^2=-1$.

## Complex numbers

The complex numbers are  the set of all expressions of the form $a + bi$ where $i^2=-1$ and $a$ and $b$ are real numbers:

$$\mathbb{C}=\left\{a + bi~~\vert~~a,~b~\in\mathbb{R}\right\}$$

For $z=a+bi\in\mathbb{C}$ we define the *real* and *imaginary*
parts of $z$ to be ${\Re}(z) = a$ and $\Im(z)=b$.

The imaginary number $i$ has no home on the real number line.
Instead, we locate it on the **complex plane** at the point $(0,1)$.

- we can represent any complex number $z=a+bi$  as the
point $(a,b)$ in the complex plane.  
- The coordinates $(a,b)$ are
usually called the *cartesian* coordinates for $z$.  (Named
after the mathematician and philosopher Rene Descartes).  
- In this
plane, the real numbers lie on the horizontal axis.  We usually
refer to the horizontal axis of $\mathbb{C}$ as the **real axis**.


```python
def plot_imag_axis(a,b):
    plt.scatter([a],[b])
    plt.plot([0,a],[b,b],ls='--')
    plt.axvline(x=0,c='k')
    plt.axhline(y=0,c='k')
    plt.plot([0,a],[b,b],ls='--')
    plt.plot([a,a],[0,b],ls='--')
    plt.xlabel('Real Axis')
    plt.ylabel('Imaginary Axis')
    plt.xlim(left=-5, right=25)
    plt.ylim(bottom=-5, top=25)
```


```python
interact(plot_imag_axis, a = IntSlider(value=10, min=0, max=20, continuous_update=False), b = IntSlider(value=10, min=0, max=20, continuous_update=False))
```

## Complex conjugates

- The complex plane $\mathbb{C}$ contain **all** of the roots of **every** polynomial.  

E.g.

$$x^2-8x+25 = 0 =ax^2 +bx +c \iff x= {-b \pm\sqrt{b^2-4ac}\over 2a} $$

$$={{8\pm\sqrt{64-100}}\over2}={{8\pm\sqrt{-36}}\over2}={{8\pm
6i}\over2}=4\pm3i$$

- Note that these two roots are reflections of one another through the
real axis. They are *conjugates* of one another.  

- In general, let $z=a + bi$. The **conjugate** of $z$ is the complex number 
  $\bar{z}=a-bi$.

We can also use Sympy's `solve` method to solve polynomials:


```python
x = sp.symbols('x')
sp.solve(x**2 - 8*x + 25)

```

## Modulus (size) of a complex number

- The distance to a point on the complex plane from 0 is called its **modulus**, and we find this by calculating the hypotenuse of the triange with base  ${\Re}(z)$ and height $\Im(z)$:

E.g. The modulus of $4\pm3i$ is $\sqrt{3^2+4^2}=\sqrt{9+16}=\sqrt{25}=5$


- In general, the **modulus** of $z=a+bi$ is the real number 
  $|z|=\sqrt{a^2+b^2}$.


- The **modulus** is connected to the **conjugate** by means of the formula 
  $z\cdot \bar{z}=|z|^2$.  Indeed:

\begin{align}
    z\cdot\bar{z}&=(a+bi)(a-bi)=a^2-(bi)^2=a^2-b^2\cdot i^2\\
                 &=a^2-b^2(-1)=a^2+b^2=|z|^2
\end{align}

## Complex numbers in Python


```python
x = 1 + 2j
print(f'x = {x}     Re(x) = {x.real}     Im(x) = {x.imag}     |x| = {abs(x)}')

```

Complex numbers using `Sympy`:


```python
x = 1 + 2 * sp.I
print(f'x = {x}     Re(x) = {sp.re(x)}     Im(x) = {sp.im(x)}     |x| = {sp.Abs(x)}')

```

## Addition and Subtraction: 

Addition and subtraction of complex numbers work as you would expect:

$$(a+bi)\pm(c+di)=(a\pm c) + (b\pm d)i$$

and

$$-(a+bi)=-a-bi$$

Try adding: $(5+6i)+(1-i)$=


```python
print((5 + 6j) + (1 - 1j))

```

Try subtracting: $(5+6i)-(1-i)$=


```python
print((5 + 6j) - (1 - 1j))

```

## Multiplication: 

Multiplication is not quite so convenient in cartesian coordinates:

\begin{align*}
    (a+bi)(c+di)&=ac + adi + bci + bidi \\ &= ac + adi + bci -bd \\ &= 
    (ac-bd)+(ad+bc)i
    \end{align*}

Try multiplying: $(5+6i)(1-i)$=


```python
print((5 + 6j) * (1 - 1j))

```

## Division: 

Division is even more awkward in cartesian
coordinates: we have to multiply the numerator and the denominator
by the complex conjugate of the denominator.

\begin{align*}
    {{a+bi}\over{c+di}}&={{(a+bi)(c-di)}\over{(c+di)(c-di)}}\\
    &={{(ac+bd)+(bc-ad)i}\over{c^2+d^2}}=\left({{ac+bd}\over{c^2+d^2}}\right)+
\left({{bc-ad}\over{c^2+d^2}}\right)i
    \end{align*}

Try dividing: $${(-4+7i)\over (2+3i)}=$$



```python
print((-4 + 7*sp.I) / (2 + 3*sp.I))

```

## Polar Coordinates

It's often convenient to represent  the complex number
$z = a + bi$ in terms of its polar coordinates $\langle r,\theta
\rangle$.
- The angle $\theta$ is called the *argument* of $z$.
  
- The real number $r=|z|$ is sometimes denoted mod$(z)$.


```python
ax = plt.subplot(121)
ax.plot([0,1],[0,1],'o-')
ax = plt.subplot(122,projection='polar')
ax.plot([0,np.pi/4.0],[0,np.sqrt(2)],'o-')
```

## Connection between cartesian and polar

Let $z=x+iy$.   If we are given the polar coordinates of
$z$ and want to express the cartesian coordinates use
$$\displaylines{x=r\cos\theta\cr
    y=r\sin\theta\cr
    z=r\cos\theta + ri\sin\theta=r(\cos\theta + i\sin\theta)\cr}$$

If we are given the cartesian coordinates and want to find the polar
coordinates use:

$$
r={\rm mod}(z)=|z|=\sqrt{x^2+y^2}
$$

\begin{align*}
\theta={\rm arg}(z)=\tan^{-1}{y\over x}=
\begin{cases}\pi/2,&\mbox{ if }~~ x=0,y>0 \\
    -\pi/2,&\mbox{ if }~~ x=0,y<0 \\
    \arctan\left({y\over x}\right),&\mbox{ if }~~ x>0\\
    \arctan\left({y\over x}\right)+\pi,&\mbox{ if }~~ x<0, y\geq 0\\
    \arctan\left({y\over x}\right)-\pi,&\mbox{ if }~~ x<0, y<0\\
   \end{cases}
    \end{align*}


<!--N.B. All of the fuss about the value of $\theta$ in the formula
above is to make sure that $z$ gets into the proper quadrant. Beware
of the sign of this tangent: depends on which quadrant you are in.
The positive $x$ axis is defined as having $\theta=0$ and positive
$\theta$ goes in an anticlockwise sense around the $xy$
plane.-->

## Some examples:

- Find the cartesian coordinates for the complex number $z$ with polar 
  coordinates $r=2$ and $\theta=\pi/6$.

$$\Re(z)=x=r\cos\theta=2\cos(\pi/6)=2\left({{\sqrt{3}\over2}}\right)=\sqrt{3}$$
$$\Im(z)=y=r\sin\theta=2\sin(\pi/6)=2\left({{1\over2}}\right)=1$$
$${\rm Therefore\ } z = \sqrt{3} + i$$


```python
print(cmath.rect(2, np.pi/6))

```

## Some examples:
- Find the polar coordinates for the complex number $z= -3+4i$.

$$|z|=r = $$

$$\sqrt{(-3)^2+4^2}=\sqrt{25}=5$$

$${\rm arg}(z)=\theta=\arctan\left({{y}\over{x}}\right)=$$

$$-0.93+\pi{\rm ~radians}\approx 127^\circ$$


```python
print(cmath.polar(-3 + 4j))

```

## Some examples:
- Find the polar coordinates for the complex number $z= -2i$.

$${\rm mod}(z)=r = |z|=2$$

$${\rm arg}(z)=\theta=-{{\pi}\over2}$$


```python
print(cmath.polar(-2j))

```

## Multiplication in Polar Coordinates:  

First a reminder of three useful and important identities:

$$\cos^2\theta + \sin^2\theta = 1$$

$$\cos(\theta_1+\theta_2)=\cos\theta_1\cos \theta_2 - \sin\theta_1\sin\theta_2$$
    
$$\sin(\theta_1+\theta_2)=\sin\theta_1\cos \theta_2 + \sin\theta_2\cos\theta_1$$

Now let $z_1=r_1\cos\theta_1+ir_1\sin\theta_1$ and $z_2=r_2\cos\theta_2+ir_2\sin\theta_2$.

We first compute the real part of the product $z_1\cdot z_2$:

\begin{align*}
\Re(z_1\cdot z_2) &= r_1\cos\theta_1\cdot r_2\cos\theta_2 - r_1\sin\theta_1\cdot 
r_2\sin\theta_2\cr
&=r_1r_2(\cos\theta_1\cos\theta_2 - \sin\theta_1\sin\theta_2)\cr
&=r_1r_2\cos(\theta_1 + \theta_2)
\end{align*}

Note that for the real part the moduli have been multiplied and the
arguments added.  

Now we compute the imaginary part of $z_1\cdot z_2$:

\begin{align*}
\Im(z_1\cdot z_2) &= r_1\sin\theta_1\cdot r_2\cos\theta_2 + r_2\sin\theta_2\cdot 
    r_1\cos\theta_1\cr
&=r_1r_2(\sin\theta_1\cos\theta_2 - \sin\theta_2\cos\theta_1)\cr
&=r_1r_2\sin(\theta_1 + \theta_2)
    \end{align*}

For the imaginary part too, the  moduli multiply while the arguments
add.   

This gives a relatively compact and highly geometric result
for the product:

$$z_1\cdot z_2 = r_1r_2(\cos(\theta_1 + \theta_2)+i\sin(\theta_1 + \theta_2))$$


It is **multiplicative** in the modulus and **additive** in the argument:

$$|z_1z_2|= |z_1\cdot |z_2|$$
$$\arg(z_1z_2)=\arg (z_1)+ \arg( z_2)$$


This means that when we multiply by $z$, we are **rotating** through the angle $\arg(z)$ and  **radially stretching** by a factor of $|z|$.

## A Remarkable Connection with $e^{i\theta}$:

First, think of $z=\cos\theta + i\sin\theta$ as a function of $\theta$ and differentiate with respect to $\theta$:

$${\rm~~(1)~~~~}\frac{{\rm d}z}{{\rm d}\theta}=\frac{{\rm d}}{{\rm d}\theta}\left(\cos\theta+i\sin\theta\right)=-\sin\theta+i\cos\theta$$

Next notice that the right-hand side is just the product $iz$:

$${\rm~~(2)~~~~}iz=i(\cos\theta+i\sin\theta)=i\cos\theta+i^2\sin\theta=-\sin\theta+i\cos\theta$$

Thus from (1) and (2) $$\frac{{\rm d}z}{{\rm d}\theta}=iz$$

This is a separable differential equation. Thus:
$$\int{dz\over iz}=\int d\theta~~~~~~~~~\Rightarrow~~~~~~{1\over
i}~\ln z =\theta +c~~~~\Rightarrow~~~~\ln z =i\theta +ic$$ In
exponential form:
$$z=e^{i\theta+ic}~~=~~e^{i\theta}~e^{ic}~~=~~Ae^{i\theta}~~~~{\rm~ with}~~~~
A=e^{ic}$$

When $\theta=0$, $z=1$ giving $A=1$, so:
$$z=\cos\theta + i \sin\theta=e^{i\theta}~~~~~~~~~~~~\rm (1)$$


Similarly we can show that:
$$z=\cos\theta - i \sin\theta=e^{-i\theta}~~~~~~~~~~~~\rm (2)$$


Adding  (1) and (2) and subtracting (1) and (2) gives:
$$\cos\theta ={e^{i\theta}+ e^{-i\theta}\over 2}~~~~~~~~~~~~~~~~~~~~~~~~~
\sin\theta ={ e^{i\theta}-e^{-i\theta}\over 2i}$$

Thus **any complex number** can now be written:
$$z=x+iy=r(\cos\theta + i\sin\theta)=r~e^{i\theta}$$


Several important consequences:

1. Any complex number can be written in the polar form $z = re^{i\theta}$ where $r=|z|$ and $\theta=\arg(z)$.

2. The unit circle in $\mathbb{C}$ consists exactly of those complex numbers with 
   modulus equal to 1, that is the numbers $e^{i\theta}$.

3. Multiplication on the unit circle $r=1$ can be carried out by adding the 
   angles:

$$e^{i\theta_1}\cdot e^{i\theta_2} = e^{i(\theta_1+\theta_2)}$$

$$z=x+iy=r(\cos\theta + i\sin\theta)=r~e^{i\theta}$$


Other important consequences:


4. Exponentiation on the unit circle $r=1$ can be done by multiplying the angle 
   by the index:

$$\left(e^{i\theta}\right)^n = e^{i\theta n}=e^{i(n\theta)}$$

5. This result is known as **DeMoivre's Theorem**.  It is usually stated in its 
   cartesian form:

$$(\cos\theta + i\sin\theta)^n=\cos(n\theta) + i\sin(n\theta)$$

6.  Finally, the famous identity by Leonhard Euler 

$$e^{\pi i}+1=0$$

e69cfdd6088817606cd45e486f0a8f28f32b4ebb
ipynb
Jupyter Notebook
lectures/lecture-08-complex-numbers.ipynb
SABS-R3/2020-essential-maths
5a53d60f1e8fdc04b7bb097ec15800a89f67a047
Apache-2.0
2021-11-27T12:07:13.000Z
2021-11-27T12:07:13.000Z
lectures/lecture-08-complex-numbers.ipynb
SABS-R3/2021-essential-maths
8a81449928e602b51a4a4172afbcd70a02e468b8
Apache-2.0
lectures/lecture-08-complex-numbers.ipynb
SABS-R3/2021-essential-maths
8a81449928e602b51a4a4172afbcd70a02e468b8
Apache-2.0
2020-10-30T17:34:52.000Z
2020-10-30T17:34:52.000Z
Qwen/Qwen-72B
1. YES
2. YES