Learn Object Oriented Programming by re-implementing Python’s Complex class: Part-1

leangaurav
Industrial Python
Published in
7 min readSep 10, 2018

This series is about understanding Object Oriented Programming in Python from very basics. We start from the very basics and then learn about Python’s OOP features in a very simple intuitive manner.

This is the Part-1 of the multi-part series. Below is a breakdown of the four parts:
1. Part-1: Simple understanding of complex numbers
2. Part-2: Using python’s built-in complex data type
3. Part-3: Writing our own functions for manipulating complex numbers
4. Part-4: Building our own Complex data type (OOP)

You might already be comfortable with some of the things, and you can choose to skip. But still, I’d suggest you go through all of this step by step to better understand the reason for doing certain things.

This part deals only with the math, to understand complex numbers better,. This will help us better express it in Python code.

What are Complex Numbers

To understand complex numbers, we need to first understand Real Numbers and Imaginary Numbers.

Real numbers

Real numbers are just positive numbers and negative numbers, with or without decimal. For Example: 1, -991, 0, 1.5555, 9.999, -0.111…

We can do operations on real numbers like addition, multiplication, division etc. Same way we have complex numbers but their arithmetic is different from real numbers.

Imaginary Numbers

These as the name suggests are literally imaginary 😶. To understand better, we need to take a look at how Squares and Square Roots of real numbers work.

Lets say we have a variable x with a value of 2
x = 2
then,
x² = +4 (read as x square or x squared)

but, if we take a negative value for x
x = -2
x² = +4

From above two examples, we know that squaring a number will always gives you positive, irrespective of the actual number being positive or negative.

Now coming to Square Root i.e. doing the opposite of finding square.

if x= 4
√X = √4
= +2 / -2

Since both +2 and -2 when squared result in +4, hence the square root of +4 can be either +2 or -2. This means that a the square root of a positive number can be either positive or negative.

Now let’s see what happens when we do square root of negative real numbers.

if x= -4
√X =
(-4)
= √(-2 x 2)

We cannot simply solve the above. As we don’t have any way to represent square root of negative number in the world of Real Numbers. No real number exists for which after finding it’s square, we get a negative result.

For representing such situation we need Imaginary Numbers. What we do is be break down -2 into negative and positive parts and deal with negative part separately.

if we rewrite -2 = (-1 x 2)

Now,
√X = √(-1 x 2 x 2)
√X =√(-1) . √(2 x 2)
√X =√-1 . 2
√X =2. √-1

Now this looks better as we separate out the root of -1. But writing square root of -1 i.e. √-1 again and again is not a good idea. So, we have a symbol to represent it i or maybe j.

The i is also referred to as iota or the imaginary unit.

this means now we can write

√X = i.2 = 2.i or -2i

This is read as two iota and is called as an Imaginary Number.
And there’s plenty of these Imaginary Numbers just like real numbers. For example: i or 1i, -i, -20i, 10.55i, …. just keep imagining.

Now with this knowledge, we can talk about complex numbers.

Complex Numbers

By definition Complex numbers are the numbers which are written in the form:
c = a + bἰ

where a and b are real numbers. a is called the real part and bἰ is the imaginary part.

What ?
In simple terms, if you take a real number and an imaginary number and join them by a ‘+’, what you get is called a Complex Number.

Example:
if the real part a = 10 and imaginary part b = 20ἰ then the complex number is:
c = 10 + 20ἰ

The imaginary part can be negative, for example if b = -20, then the resulting complex number is:
c = 10 - 20ἰ

Examples of other complex numbers are:
1 + ἰ, 1 - ἰ, -1 - ἰ
0.5 – 2ἰ
, -1 + 0.5ἰ
1, 10ἰ

All above examples should look find except the last two 1 and 10i.Yes these are complex numbers as well. Since by definition a complex number is anything that can be written as a + bἰ.
This means 1 can be written as 1 + 0.i hence it is also a complex number.

So all of real numbers are also imaginary numbers, but all imaginary numbers are not real numbers. Now we can come to the arithmetic operations part of real numbers.

Operations on Complex numbers

1. Conjugate of a Complex Number

Since the name is complex numbers, so it’s not that simple either!!
So before we see other operations, it’s good to know in advance this simple thing about Complex Numbers.

If you just invert or change the symbol of the imaginary part of a complex number what you get is the Conjugate of a complex number.

Like if c= 10 + 20ἰ (using variable name c for the entire complex number)

Then conjugate of c will be 10 – 20ἰ. Or we say that 10 + 20ἰ and 10 – 20ἰ are conjugates of each other.

Examples (Conjugates mentioned in pairs):
1 – 2ἰ is conjugate of 1 + 2ἰ or vice versa.
ἰ, -ἰ
10, 10

Simple enough? I hope yes. We’ll be using it in later operations… so try to remember this.

2. Square of i

Again we need to learn this thing as well before going towards addition and all.

i² = -1

How??

i = √-1

i²= (√-1)²

i² = -1

This means
i⁴ = +1

i⁶ = -1

i⁵ = ἰ

i⁷ = -ἰ

Just let it stick in your head.

3. Addition

Remember algebra??
Anyways, lets revise. If you need to add x + 5y and 10x — 2y
So you would go ahead and add the terms with x together and the terms with y together

Adding x + 10x and 5y + (-2y) gives you: 11x + 3y

Similarly if you have two complex numbers, add the real parts together and the imaginary parts together:

C1 = 10 – 20 ἰ

C2 = -5 – 10 ἰ

C1 + C2 = (10 -5) + (-20 ἰ — 10 ἰ)

C1 + C2 = 5–30 ἰ

So in general

Formula

C1 = a + bἰ

C2 = p + qἰ

C1 + C2 = (a + p) + (b + q)ἰ

4. Subtraction

This is similar to adding 2 complex numbers. Just change the sign

Formula

C1 = a + bἰ

C2 = p + qἰ

C1 — C2 = (a — p) + (b — q)ἰ

Example

C1 = 10–20 ἰ

C2 = 5 + 10 ἰ

C1 - C2 = (10 - 5) + (-20 ἰ - 10 ἰ)

C1 - C2= 5–30 ἰ

Confusing?? Same result as addition. Check that the values of C1 and C2 used are different.

5. Multiplication

Well this is somewhat different but still follows this simple rule of algebra
(x + y)(p + q) = x(p + q) + y(p + q) = xp + xq + yp + yq

For Complex numbers, multiplication can be generalized to:

Formula

C1 = a + bἰ

C2 = p + qἰ

C1 * C2 = a.p + a.q ἰ + b.p ἰ + b.q ἰ2

= a.p + (a.q + b.p) ἰ + b.q (-1) [Using - 2]

= (a.p — b.q) + (a.q + b.p)ἰ

Try to get the result of C1 * C2 using the values from addition section.

You should get the result as -45 + 0 ἰ

6. Multiplying conjugates

Let C1 = 10 + 20 ἰ
Its Conjugate is C2 = 10–20 ἰ

C1 * C2

= 100–200 ἰ + 200 ἰ — 400 ἰ2

= 100 + 400

= 500

This means for a complex number C = a + bἰ, multiplication by its conjugate gives a² + b²

Try few more examples for practice.

7. Division

This special case requires the use of conjugates. To understand logic for division first understand this:

2/3 = (2.7)/(3.7) = 14/21

So, if you multiply the numerator and denominator of a number by same number, it stays the same. We will use this property along with conjugates to get around division.

C1 = 1 – 3 ἰ

C2 = 1 + 2 ἰ

C1/C2

= (1 – 3 ἰ)/(1 + 2 ἰ)

=(1 – 3ἰ).(1 – 2 ἰ) / (1 + 2 ἰ).(1 – 2 ἰ)

= (–5 – 5ἰ) / (1 + 4)

= (–5 – 5ἰ) / 5

= –1 –ἰ

When dividing two complex numbers, multiply both numerator and denominator by the conjugate of the denominator and then simplify the resulting expression.

That’s it for the basics of complex numbers!!

Random Questions:

Why don’t we write ἰ + 1 instead of 1 + ἰ?

Well you can write, but don’t know why. Even I have seen it always like that. Maybe imaginary numbers are considered as second-class members in the numbers family and they always have to be at the last. Anyways, if you get to know of a good reason why, let me know as well. TIA.

Next: Complex Numbers 2: Python Built-in

--

--

leangaurav
Industrial Python

Engineer | Trainer | writes about Practical Software Engineering | Find me on linkedin.com/in/leangaurav | Discuss anything topmate.io/leangaurav