COMPLEXOSCOPE

Written by Giles Greenway

WARNING ! This program is unfinished, slow, uses LINEA, and is the  most
blatant piece of dialogue-ware every written. Complexoscope, is however,
unique. That must be a good thing. Now, read on...

Most programs I write are for my own amusement, and are not very usable.
This one is no exception, but some people may find it instructive or fun
to play with. I really love  to mess about with fractals.  Complexoscope
deals with the class  of fractals known as  complex iteration maps,  the
most well known of which is the Mandelbrot set. First a very brief maths
lesson:

The square root of -1 is  written as i. The square  of i is -1. That  is
all the  explanation  you are  going  to get.  Root  -1 is  said  to  be
"imaginary", and allows a  new class of numbers  to be defined,  complex
numbers. A complex number can  be written as x +  iy, where x and y  are
said to be the real and imaginary parts. Such numbers can be represented
as points  on a  graph  where the  x axis  is  real and  the y  axis  is
imaginary.

Now for the fun  part. Choose and complex  number u, and define  another
complex number z to be initially 0 + 0i. Cycle the number z through  the
equation z = z^2 + u.  (Or z = z**2 + u  if you write FORTRAN.) For  any
choice of u there are two things that can happen: The magnitude of z (or
its distance from the origin at 0  + 0i) will remain within some  finite
boundary, or it will head off to infinity. Colour the points that remain
bounded black, and assign nice, pretty colours to the rest according  to
how quickly they grow,  and you get  a Mandelbrot set.  If you vary  the
initial value of z, and  keep u fixed you end  up with what is called  a
Julia set. There's a lot  more to it than  this, if you're interested  I
suggest you do a bit of reading.

If you  run  Complexoscope  and  hit return  you  just  get  a  standard
Mandelbrot set. -Don't do it, though, there are better, faster  programs
to do that job. But what if the quation was z = z^3 + u ? What about z =
(sin z)^2 +  u ? You'd  have to write  a seperate program  to find  out.
Until now.  Complexoscope lets  you  enter your  own equations.  At  the
moment it can add, subtract, multiply,  raise to integral powers and  do
regular and hyperbolic  sines and  cosines. You  also need  to give  the
program an  cut-off to  decide decide  if a  point is  diverging, and  a
maximum number  of  iterations  after  which points  are  deemed  to  be
bounded, in addition to picture boundarys and the value of u if it is to
be held constant. A set of  radio buttons switches between Julia  (fixed
u) and  Mandelbrot  (variable  u)  modes.  I've  included  some  example
pictures for z = cosh(z)  + u in low  and high resolution Degas  format.
The picture boundarys were  -6.4 to 6.4 and  -4i to 4i. Mandelbrot  mode
was used, the cut-off and number of iterations were both 50. At the  end
the program asks you if  you want to save  the picture in Degas  format.
Slow, isn't it ? One  day this program will  have a nicer interace,  use
resolutions other that standard ST high,  medium and low, not use  LINEA
calls and be compiled for use with co-proscessors. Even in its  crippled
state, I still reckon the program is quite fun to play with. Every  time
I add a new feature, I have  to mess around with it, no wonder  progress
is so slow.

The only really clever bit is  how the program parses the equations.  In
case ICTARI readers  are interested, here  is how its  done. The  method
used is called the shunting algorithm...

Standard arithmatic is  hard for a  machine to understand,  it would  be
much easier  to  use a  simpler  notation. In  the  shunting  algorithm,
"Reverse Polish" notation  is used. z^2  + u translates  to z 2  ^ u  +.
Deciphering this is easy.  Work from right to  left, pushing the  values
onto a stack. When you  meet an operator, take  as many values from  the
stack as you  need, perform the  operation on them,  and put the  result
back on the stack. If we apply this to our exquation the result will end
up at the top of the stack after four steps as shown below:

 2 |   2  |       |   u   |
   |      |       |       |
 1 |   z  | (z^2) | (z^2) | (z^2)+u
   |      |       |       |
 --+------+-------+-------+---------
   |   1  |   2   |   3   |   4     

This shouldn't be too tough to program, but where does one get the  R.P.
notation from in the first place ? This where the shunting bit comes in.
All operators are given a priority, *, / and ^ are high priority, +  and
- are lower, operators  that take single operands  like sin and cos  are
lower still. This time, two stacks are needed, one for operands and  one
for operators. Working  from left  to right, let  us apply  this to  the
first three symbols in our example:


   | 2 |  |   |
   |   |  |   |
   | z |  | ^ |
   |   |  |   |
   +---+  +---+
   
Things get compilcated  when we  get to  the +  sign. You  can only  put
operators onto their stack if the symbol below is of lower priority.  If
this is not so,  you must shunt  operators one by  one onto the  operand
stack until a lower priority symbol is on top of the operator stack.  In
our example we now have:

  | ^ |  |   |
  |   |  |   |
  | 2 |  |   |
  |   |  |   |
  | z |  | + |
  |   |  |   |
  +---+  +---+
  
Now we continue as normal, and bring the u across:

  | u |  |   |
  |   |  |   |
  | ^ |  |   |
  |   |  |   |
  | 2 |  |   |
  |   |  |   |
  | z |  | + |
  |   |  |   |
  +---+  +---+
  
When the end of the  expression is reached we  just pop the contents  of
the operator stack onto the operand stack one by one. So from bottom  to
top the operand stack now contains z 2  ^ u +, which is what we  wanted.
One final point, there are no brackets in R.P. notation, every time  you
find a ( increase the priority of the opeators you find by one until you
find a matching ).  The first job Complexoscope  does is to analyse  its
input string to split  it up into operators  and operands. Then an  R.P.
expression is  created which  will be  used on  every iteration  of  the
equation.

Easy when you know how, and I'm sure you can do much better. Now go  and
write a spreadsheet !

    Have Fun,
        Giles.
        
        
