vadim_kataev (vadim_kataev) wrote,

Beauty of complex functions

The complex numbers help to solve many engineering problems. They form a
superset of real numbers, an algebraical structure that has many useful

Each complex number is an ordered pair of real numbers. Complex
numbers are well supported by Python (and even natively), so that it
simplifies many computational tasks. In what follows we will see some really
exciting applications in the 2D Image Transformation.

A 2D digital image is an array of points, where each point has unique
2D coordinate and RGB color values p=(x,y,RGB)

Complex plane is a 2D coordinate system. Each pixel of a 2D digital image
is uniquely associated with a complex number z on the complex plane. For
example, if some p=(a,b,RGB) then it's ok to view some z=a+bj.

We could then apply a complex function f(z) for each z to get a new transformed
2D digital image. We just map each pixel with coordinate z onto coordinate w:
(a,b,RGB) -> (a',b',RGB)

This is our original image, w = z

Let's apply such function that will create reflection of each pixel about the real axis. This is so called conjugation of the complex number and it's defined as (a+bj) -> (a-bj). Python provides each member of the type complex with a function conjugate:

w = z.conjugate()

Ok. Let's try to rotate our original image. Rotation in complex plane is so easy that you could even think that it's a kind of magic, but the real magic is the famous Euler's identity:
ej*pi + 1 = 0

Euler's identity is considered to be the most beautiful mathematical equation because it binds pi, e, 1, 0, imaginary number, multiplication, addition, and equality together.

What Euler's identity comes from is rotation in complex plane. In order to rotate any z around (0+0j) we just have to multiply z with ej*angle, where angle is to be given in radians. You could easily check it yourself on a Python's console. Let's see what happens if we switch places of pi and e.

This is w = z * pi ** (1j*e)

Python has a library of standard mathematical functions to deal with complex numbers called cmath. From now on we assume we've done:
from cmath import *
Let's see how it's easy to create a simple lens-effect:

z * cos(z.real/140) * cos(z.imag/140)

Natural logarithm in complex plane is an inverse mapping of the rotation. Let's transform our original image with:

w = log(z)*30

Simple beautiful function:

w = z * (-1)**z.real

Well done! Now you can "feel" the topology of complex numbers better. If no, there is still a lot of beautiful transformations. For example, can you recognize a galaxy on the next image?

w = z * e**(1j*z.imag)

w = z * e**(1j*z.real)

Let's mix them both. They form fascinating image of a fast rotational "something". It looks like a semi-chaotic "something". Can you explain why it has 25 line ends out of visible circle?

w = z * e**(1j*z.imag + 1j*z.real)

Can you see the origin of a fractal on the next image? Try to zoom.

w = z**5j + z**4j + z**3j + z**2j + z

Square root shows very funny "circularity", as if it tears a square-topology image in attempt to achieve the circularity. This transformation is remarkable because it is an injective function, so that an area of transformed image is equal to original one. Guess what should be an inverse transformation for that one.

w = z + z**0.5

And finally, my favorite one. Let's create a pure rotational chaos!

What function is it? Guess it. Find inverse one.
Tags: complexity, fun, math, python
  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your IP address will be recorded