Complex Numbers

Complex Numbers

Since complex numbers are so important in fractal rendering I try to give a very basic tutorial on complex numbers in this topic. You might want to draw a sine function and cosine function on paper if you haven't got them memorized because when proofing the used formula's I often switch form sine to cosine and vice versa by adding and substracting angles.

The complex plane

Complex numbers can be pictured as vectors in a plane. Nothing special about that except for the fact that one axe is called the imaginary axe and one the real axe.

The imaginary axe has a special constant "tagged" on. This constant we shall call "j" as often used in electrical engineering because otherwise it might be confused with the 'I' of eeh... current. Mathematicians usually use "i" which stands for imaginary. However in some fonts "i" looks like the number one so I use the notation "j".

This constant "j" has the following property.

j * j = -1

The complex number Z is for instance written as

Z = a + jb

This vector has a length of

|Z| = ( a2 + b 2 ) 1/2

Note that taking the power '1/n' of a number is the same as taking the root 'n' of a number.

The advantage of using complex numbers is that we can multiply, divide in fact do any sort of calculation without having to deal with sine's and cosine's. To use cosines and sines with vectors, it is sometimes easier to picture the vector as something with a length and a direction. The angle is usually pictured as a vector with a length of one lying on something called the unit circle.

The Unit Circle

The unit circle is just a circle with radius one. Therefore every vector that has its endpoint on the unit circle and has its origin in '0 + 0j = O' has of course also a length of one.

In some respects this makes calculations a little easier because on the unit circle the imaginary axe (say the Y axe) is the sine of the angle of the vector and the real axe the cosine of the angle. Since the subtendent side of the right angle is one and a2 + b2 = c2 which will be shown in a following paragraph.

We can then, sort of, split the vector in two components. One has the angle information and one has the length information.

Z = |Z| ( cos( a ) + j sin( a ) )

|Z| is here the absolute value, i.e. the length, of the vector. While the length of 'cos( a ) + j sin( a )' is exactly '1' but has an angle of a.

However sometimes its easier to use sine and cosine and sometimes we just use te vectors themselves. The first we have to do is deduct an important formula we will use. Then we will use this formula to see what happens when we perform the different functions on the vectors.

Becoming a pythagorean

First we have to proof pythagoras theorem that the

Square on the hypotenuse of a right angled triangle equals the sum of the squares of the other sides

This is easy to see in the next picture. We can divide the large square whoms area = ( a + b )2 into four triangles with sides 'a' and 'b' and one square with side 'c'.

We can combine the four triangles into two rectangles with an area of a b

So the area of the large square equals the area of the small square plus the area of the two rectangles. As a formula this gives ( a + b ) 2 = 2 a b + c2. Simplifying this formula a little bit gives a2 + 2 a b + b2 = 2 a b + c2. Substracting 2 a b from both sides give the following formula.

a2 + b2 = c2

Two handy formula's

Usually you first have a problem then you find the solution. However I first start by giving a solution for a problem we find in the next chapters. The solution are the following formula's.

sin ( a + b ) = cos( b ) . sin( a ) + cos( a ) . sin( b )

cos ( a + b ) = cos( b ) . cos( a ) - sin( a ) . sin( b )

Lets examine the triangle on the right. There are three corners a, b and g. Together these are of course 180° as with all triangles.

Figure 1
Figure 2

Now lets draw some line from two corners to the opposite side in such a way that the angle it makes with the opposite side is 90°. From our gonio lessons we remember sin( a ) = h / b.

we are going to proof the formula at the beginning of the paragraph by calculating the area of the complete triangle in two different ways then we say Area 1 = Area 2 which should of course be true since it is the same triangle.

The first way is by using the line marked 'h' which divide the triangle in a 'left' and 'right' triangle. The second way is using corner g and line 'a' and 'b'.

The left triangle has an area of half the rectangle A * h. Since cos( a ) = A / b the area of the left triangle is 0.5 h A = 0.5 h b cos( a ). However h = a sin( b ). Finally resulting in the Area of the left triangle being 0.5 a b cos( a ) sin( b )

For the right triangle the same is true ( we use of course angle b and side a ). Thus the area of the right triangle is 0.5 h a cos( b ). Finally resulting in the Area of the left triangle being 0.5 a b cos( b ) sin( a )

This gives us the first formula for the area of the complete triangle which is:

0.5 a b ( cos( a ) sin( b ) + cos( b ) sin( a ) ).

Now we are going to calculate the area of the complete triangle in a different way. Again we say the area of the comlete triangle is half that of the rectangle l * b. Thus Area = 0.5 * l * b. Since l = a * sin( g ). This results in the complete area being 0.5 * b * a * sin( g ). Since g = 180° - ( a + b ) we can write this as 0.5 * b * a * sin( 180 - ( a + b ) ).

When we draw a sine function on paper we can see that sin( 180 - Angle ) = sin( Angle ).

This gives 0.5 * b * a * sin( a + b ) as the area for the complete area.

Now since the first calculated area must equal the second (it is the same triangle after all).

0.5 * b * a * sin( a + b ) = 0.5 a b ( cos( a ) sin( b ) + cos( b ) sin( a ) ).

Dividing by 0.5 * b * a gives us the wanted formula which will help a lot in the next chapters.

sin ( a + b ) = cos( b ) . sin( a ) + cos( a ) . sin( b )

However from this formula we can derrive an additional formula. When looking at the above formula the question arises if a similar formula exist with the cosine i.e. something like cos ( a + b ) = ... Well there is but I save you the trouble of going through 2 triangles and a lot of text. Instead we take a shortcut.

Lets say we define a = 90 + d. We can do this since we are not interested in absolute values but just in the "blueprint" of the formula.

sin ( 90 + d + b ) = cos( b ) . sin( 90 + d ) + cos( 90 + d ) . sin( b )

Since sin ( 90 + a ) = cos( a ) and cos ( 90 + a ) = -sin( a ) ( draw a sine an cosine function on paper to proof this for yourself) this will finally result in the following formula.

cos ( d + b ) = cos( b ) . cos( d ) - sin( d ) . sin( b )

We can then write the formula in the same way as at the beginning of the paragrap.

cos ( a + b ) = cos( b ) . cos( a ) - sin( a ) . sin( b )

Of course this last a is not the same as before.

In the following paragraphs function Re() will return the real part of a complex number (see this as the X-axe) and Im() will return the imaginary part of a complex number (see this as the Y-axe).

Addition

C1 = a + jb
C2 = c + jd
C3 = C1 + C2 = ( a + c ) + j ( b + d )
Re( C3 ) = a + c
Im( C3 ) = b + d

Substraction

C1 = a + jb
C2 = c + jd
C3 = C1 - C2 = ( a - c ) + j ( b - d )
Re( C3 ) = a - c
Im( C3 ) = b - d

Multiplication

When we multiply two vectors we have to multiply the length of the vector and add the angles to get the resulting vector.

Mulitplication with polar coordinates

C1 = A1. ( cos( a ) + j sin( a ) )
C2 = A2. ( cos( b ) + j sin( b ) )

C1 * C2 = A1 * A2 ( cos( a )cos( b ) - sin( a )sin( b ) + j( cos( a )sin( b ) + sin( a )cos( b ) )

Using the two formula's from the "handy section".

C1 * C2 = A1 * A2 ( cos( a + b ) + j sin( a + b ) )

So multiplying two vectors is multiplying their lengths and adding the angles. This can speed up calculations when raising a complex number to power 'n'.

Mulitplication with vectors

C1 = a + jb
C2 = c + jd
C3 = C1 * C2 = ( a * c + j2 * b * d ) + j ( a * d + c * b )
Since j2 = -1
C3 = C1 * C2 = ( a * c - b * d ) + j ( a * d + c * b )
Re( C3 ) = a * c - b * d
Im( C3 ) = a * d + c * b

Division

Division with polar coordinates

C1 = A1. ( cos( a ) + j sin( a ) )
C2 = A2. ( cos( b ) + j sin( b ) )

C1 / C2 = A1 / A2 ( cos( a ) + j sin( a ) )/( cos( b ) + j sin( b ) )

We are first going to multiply both parts with the complement of C2 (without the length). This is C2 with the imaginary part inverted. This way we get rid of the imaginary part in the denominator.

C1 / C2 = A1 / A2 ( cos( a ) + j sin( a ) )( cos( b ) - j sin( b ) )/( cos2( b ) + sin2( b ) )

Since - sin( b ) = sin( - b )and cos( b ) = cos( - b ) and, very important, sin2( b ) + cos2( b ) = 1 since it is on the unit circle.

C1 / C2 = A1 / A2 ( cos( a ) + j sin( a ) )( cos( - b ) + j sin( - b ) )

Now we again have the formula for multiplication (only angle b is negative). Thus

C1 / C2 = A1 / A2 ( cos( a - b ) + j sin( a - b ) )

Conclusion division is dividing the lengths and substracting the angles.

Division with vectors

C1 = a + jb
C2 = c + jd
C3 = C1 / C2 = ( a + jb ) / ( c + jd ) C3 = C1 / C2 = ( c - jd ) /( c - jd ) ( a + jb ) / ( c + jd ) = ( ca + db - j( da - bc ) )/ ( c2 + d2 )
Re( C3 ) = ( ca + bd ) / ( c2 + d2 )
Im( C3 ) = - ( da - bc ) / ( c2 + d2 )

Power n

This is just a number of multiplications and won't be explained any further. For polar coordinates the formula is easy and efficient.

C1n = A1 ( cos( na ) + j sin( na ) )

Root 'n'

Since root 'n' equals power '1/n' we can write the same formula as with 'Power n'

C11/n = A1 ( cos( a / n ) + j sin( a/ n ) )

However this is only one solution. Also adding 360° to the angle give other solutions. Therefore the complete formula is:

C11/n = A1 ( cos( ( a + k 360 ° ) / n ) + j sin( ( a + k 360 ° ) / n ) )

Where k is in the range [ 0, n - 1 ]

Putting some things together

Now to use some of the above theory lets see how to calculate a mandelbrot fractal by hand. As you might remember form the previous chapters a mandelbrot fractal is defined as

Zn + 1= Zn2 + C

Where C is the current starting point. Lets try this a few times for C = 1 + j

First iteration:

Zn + 1 = ( 1 + j )2 + 1 + j
Zn + 1 = ( 1 + 2j - 1 ) + 1 + j
Zn + 1 = 1 + 3j

Second iteration

Zn + 1 = ( 1 + 3j )2 + 1 + j
Zn + 1 = ( 1 + 6j - 9 ) + 1 + j
Zn + 1 = -7 + 7j
Third iteration

Zn + 1 = ( -7 + 7j )2 + 1 + j
Zn + 1 = ( 49 - 98j - 49 ) + 1 + j
Zn + 1 = 1 - 97j

Now stop when the vector reaches a certain size. The current size is ( 12 + 972 )1/2

Lets say we stop at this point then we have to assign a color to this point for instance based on the current number of iterations which is three.

Then suddenly we realize that this is cumbersome to do by hand and decide to use frachunt instead.

Copyright © 2000,2004 by Ron AF Greve "http://informationsuperhighway.eu+31878753207