# Complex Numbers

We introduced complex numbers in the first post, talking about numeric data types. For those not familiar with them, it is worth diving a little deeper so we understand just how they are manipulated, and what it means to add, subtract, divide and multiply them.

In mathematics, a complex number is represented as `a + bi`

, where `i`

represents the square root of `-1`

. Note that when specifying complex numbers in Python, `j`

is used instead of `i`

. For this post, we will stick with the standard mathematical notation and use `i`

.

## Addition and subtraction

To add or subtract complex numbers, we add/subtract all the **real** parts and all the **imaginary** parts.

If we have a series of `n`

complex numbers:

$$a_{1} + b_{1}\imath, a_{2} + b_{2}\imath,...,a_{n} + b_{n}\imath$$

Then the sum is:

$$(a_{1} + a_{2} + ... + a_{n}) + (b_{1} + b_{2} + ... b_{n})\imath$$

To give a simple example with just 2 complex numbers:

$$(1 + 2\imath) + (3 - 1\imath) = (1 + 3) + (2 - 1)\imath = 4 + \imath$$

## Multiplication

Multiplication can get a little trickier, if only because there are 2 parts to each number. It might seem like we should just repeat the method we used for addition: multiply the **real** parts and the **imaginary** parts, but this is not the way to go.

Instead, think of a complex number as an algebraic equation for the moment, where the imaginary part is simply a variable, for which we do not currently have the value. In fact, let's turn it into `x`

for the moment so we don't get confused. This means if we multiply `a + bx`

by `c + dx`

, we get the following:

$$(a + bx)(c + dx) = ac + adx + bcx + bdx^{2} = ac + (ad + bc)x + bdx^{2}$$

Now, think about this for a moment. We know the values of `a`

, `b`

, `c`

, and `d`

, which means that `ac`

, `ad + bc`

and `bd`

are just numbers, so the answer is really of the form:

$$a + bx + cx^{2}$$

Since we swapped our imaginary part - `i`

- for `x`

to try and avoid some confusion. Let's swap it back again:

$$a + b\imath + c\imath^{2}$$

And we know the value of `i`

- it is the square root of `-1`

, which means:

$$\imath^{2} = -1$$

Giving us the result:

$$a + b\imath + (c \times -1) = (a - c) + b\imath$$

Once again, `a`

, `b`

, and `c`

are just numbers, for which we know the values, so we are back to a number which has 2 parts - a **real** part and an **imaginary** part. In other words, multiplying complex numbers together gives a complex number as a result. Not really any surprise there.

Let's do a more concrete example:

\begin{align} (1 + 2\imath)(2 + 3\imath) &= 1\cdot2 + 1\cdot3\imath + 2\cdot2\imath + 2\imath\cdot3\imath\\ &= 2 + 3\imath + 4\imath + 6\imath^{2}\\ &= 2 + 7\imath + 6\cdot-1\\ &= 2 - 6 + 7\imath\\ &= -4 + 7\imath \end{align}

## Division

Division of complex numbers is probably the hardest of the basic operations. It is probably easiest to illustrate with a specific example.

$$-4 + 7\imath \over 1 + 2\imath$$

Firstly, we need what is called the **complex conjugate** of the denominator (the number below the line). This is nothing other than the same complex number, but with the operator swapped from + to - or vice versa. In this case, the complex conjugate is:

$$1 - 2\imath$$

We then multiply both the top and the bottom by this number, which does not change anything as a number divided by itself is 1.

$$

\left (-4 + 7\imath \over 1 + 2\imath \right ) \cdot \left (1 - 2\imath \over 1 - 2\imath \right )

$$

Now, multiply out the top and the bottom, which gives us:

$$

\left (10 + 15\imath \right ) \over (1 + 4)

$$

This works becuase of the general formula:

$$(x + y)(x - y) = x_{2} - y_{2}$$

Which means we now have a non-complex number as the denominator.

$$

\left (10 + 15\imath \over 5 \right ) = \frac{10}{5} + \frac{15\imath}{5} = 2 + 3\imath

$$