     Mathematical Visualization, Computer Graphics, APL & J
               February 28, 1994, Toronto SIG APL

Outline

 Background
     Academic
     Computer Graphics
     APL & J

 Overview of Visualization Course
     Computer Graphics
     Fractals
     J
     Windows Programming
     POVRAY

 Detailed examples
     First Night 
     Iterated function systems
     Box counting to estimate fractal dimension
     Image processing
     Mandelbrot set
     Bit based fractals
     Plasma clouds
     Ray tracing 3-Dimensional Fractals

 Closing perspectives
     What was good
     What was bad
     What could be better
Background

 Academic
     Mathematician
     Not a programmer
     Freedoms
     Courses

 Computer graphics
     Mandelbrot set, Newton's method (Cayley) pop math
     Rayleigh Quotient Algorithm 
          Turbo Pascal, BGI
          Raster images
          Image compression

 APL & J
     As a student
     Linear Algebra
     Special Topics Courses
     Graphics
     Visualization Course:

Ideas
 Computer Graphics
     Screen coordinates
     Plotting curves (parametric equations)
     Polygons
     Transformations in the plane
     Raster Arrays
     Color Models (RGB, Hue ...)
     Image processing
     3D projection, perspective
     3D surfaces, Painter's algorithm
     Ray-tracing

 Fractals
     Iterated Function systems
          Deterministic
          Probabilistic
     Contours / Bit based fractals
     Julia and Mandelbrot sets
     3-D Fractals

Languages
 J
     Array processing
     Functional
     (Learn enough to do what is needed)

 Windows Graphics
     Object graphics commands
     Windows programming

 POVRAY (Persistence of Vision RayTracer)

Structure & StudentsExample 1      First Night

Object Graphics

   |:2 1 o./ 2r5p1*i.5   NB. A Pentagon
        1         0
 0.309017  0.951057
_0.809017  0.587785
_0.809017 _0.587785
 0.309017 _0.951057





Raster Graphics

   ]m2=.2 2$1 1 1 0
1 1
1 0
   ]m4=.(m2,.m2),m2
1 1 1 1
1 0 1 0
1 1 0 0
1 0 0 0
   ]m8=.(m4,.m4),m4
1 1 1 1 1 1 1 1
1 0 1 0 1 0 1 0
1 1 0 0 1 1 0 0
1 0 0 0 1 0 0 0
1 1 1 1 0 0 0 0
1 0 1 0 0 0 0 0
1 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0         Lab 1:  First Experiments with Object Graphics

The following commands provide facilities for doing
graphics from J.  Several of the commands are defined
functions designed to make plotting simpler.  All the
graphics windows opened have coordinates ranging from 0
to 1000 in a standard left handed coordinate system with
the origin in the lower left.

Command summary:

[size] sogwin 'title'         Start Object Graphics Window. 
[color] spoly poly            Show Polygon; "color" is an rgb
                              triple or _1 and "poly" is a
                              matrix of vertices.
color spixel point            Show pixel; "color" is an rgb
                              triple and "point" is the
                              coordinates of the pixel.
[color] sline points          Show lines between given points.

wd 'gclear;'                  Window Driver - Clears
                              graphics.
wd 'reset;'                   Windows Driver - Close all
                              windows.


Experiment with the following commands.

   2r5p1            Compare to .
   2r5p1*i.5
   2 1 o./2r5p1*i.5
   |:<.500*1+2 1 o./2r5p1*i.5
   sogwin 'OG'
   255 0 0 spoly |:<.500*1+2 1 o./2r5p1*i.5

What is the shape of the figure?  Experiment with
resizing the window; clearing, and adding other polygons.

Can you display a Blue 17-gon?  Can you display a Blue 5-
pointed star?  A Magenta 17-pointed star?
Example 2      Iterated function systems

t0=.(0.5 0 0,0 0.5 0,:0 0 1)&(+/ .*)"1
t1=.(0.5 0 0.5,0 0.5 0,:0 0 1)&(+/ .*)"1
t2=.(0.5 0 0,0 0.5 0.5,:0 0 1)&(+/ .*)"1
   sq
0 0 1
0 1 1
1 1 1
1 0 1
     t0`t1`t2`:0 sq
  0   0 1
0.5   0 1
  0 0.5 1

  0 0.5 1
0.5 0.5 1
  0   1 1

0.5 0.5 1
  1 0.5 1
0.5   1 1

0.5   0 1
  1   0 1
0.5 0.5 1
   $ t0`t1`t2`:0 sq
4 3 3
   $ 0 2|:t0`t1`t2`:0 sq
3 4 3     
   (<"2)0 2|:t0`t1`t2`:0 sq
Ŀ
  0   0 10.5   0 1  0 0.5 1
  0 0.5 10.5 0.5 1  0   1 1
0.5 0.5 1  1 0.5 10.5   1 1
0.5   0 1  1   0 10.5 0.5 1
Example 3.     Box counting for fractal dimension

If N(h) denotes the number of boxes of size h needed to cover
a set then the fractal dimension is defined by



So for unit segment:
  h     N(h)

  1      1
 2-1     2
 2-2     22
 2-3     23
 2-4     24
...
 2-n     2n




So for unit square:
  h     N(h)

  1      1
 2-1     4
 2-2     42
 2-3     43
 2-4     44
...
 2-n     4n

For Sierpinski Triangle:
  h     N(h)

  1      1
 2-1     3
 2-2     32
 2-3     33
 2-4     34
...
 2-n     3n


For a square raster image of size 2n by 2n we can approximate
this by breaking the image up into m blocks in each direction. 
Then if N(m) gives the number of blocks touching the fractal
(i.e., containing a 1), then


   ]r=.1~:16 16{.x +./ . *.|:x=.3 3 3#:i.27
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 0 0 0 1 1 1 1 1 1 0 0 0 1
1 0 1 0 0 0 1 0 1 1 0 1 0 0 0 1
1 1 1 0 0 0 1 1 1 1 1 1 0 0 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0
1 0 1 1 0 1 1 0 1 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0
1 1 1 0 0 0 1 1 1 0 0 0 0 0 0 0
1 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0
1 1 1 0 0 0 1 1 1 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0

   $1 3|:2 8 2 8$,r
2 2 8 8


   r;<(<"2)1 3|:2 8 2 8$,r
Ŀ
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1Ŀ
1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 11 1 1 1 1 1 1 11 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 0 1 1 0 1 1 01 1 0 1 1 0 1 1
1 1 1 0 0 0 1 1 1 1 1 1 0 0 0 11 1 1 1 1 1 1 11 1 1 1 1 1 1 1
1 0 1 0 0 0 1 0 1 1 0 1 0 0 0 11 1 1 0 0 0 1 11 1 1 1 0 0 0 1
1 1 1 0 0 0 1 1 1 1 1 1 0 0 0 11 0 1 0 0 0 1 01 1 0 1 0 0 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 0 0 0 1 11 1 1 1 0 0 0 1
1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 11 1 1 1 1 1 1 11 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 0 1 1 0 1 1 01 1 0 1 1 0 1 1
1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0Ĵ
1 0 1 1 0 1 1 0 1 0 0 0 0 0 0 01 1 1 1 1 1 1 11 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 01 1 1 1 1 1 1 11 0 0 0 0 0 0 0
1 1 1 0 0 0 1 1 1 0 0 0 0 0 0 01 0 1 1 0 1 1 01 0 0 0 0 0 0 0
1 0 1 0 0 0 1 0 1 0 0 0 0 0 0 01 1 1 1 1 1 1 11 0 0 0 0 0 0 0
1 1 1 0 0 0 1 1 1 0 0 0 0 0 0 01 1 1 0 0 0 1 11 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 01 0 1 0 0 0 1 01 0 0 0 0 0 0 0
                               1 1 1 0 0 0 1 11 0 0 0 0 0 0 0
                               1 1 1 1 1 1 1 11 0 0 0 0 0 0 0
                               ٳ



   +/+/+./"1(+./"1)1 3|:2 8 2 8$,r
4

   +/+/+./"1(+./"1)1 3|:4 4 4 4$,r
15

   +/+/+./"1(+./"1)1 3|:8 2 8 2$,r
52





fd=.[ (%&^.)~ +/@,@((+./"1)^:(2))@
        (1 3&|:)@((#@]  (]`%`]`%`:0) [)$,@])


















                        Dendrite Crystal
  Courtesy of A. Dougherty, Dept. of Physics, Lafayette College

   2 fd b
2
   4 fd b
1.85022
   4 fd b
1.85022
   8 fd b
1.73648
   16 fd b
1.72672
   32 fd b
1.73307
   64 fd b
1.73754
   128 fd b
1.74451
   512 fd b
1.74076              Lab 9: Experimental Fractal Dimension

     In this exercise you will be able to experimentally
estimate the fractal dimension of 3 fractal images. 
These are in the files carpet.bmp, andy-dla.bmp, and
andy-den.bmp.  View these using WINGIF.  By the way, you
can add these to the list of files that are special in
the lab.  Now we can read the first bitmap into J with:
     ('p';'b')=. readbmp8 'carpet.bmp'
Now look at $p, p, (the palette) and $b.  You don't want
to look at the bitmap b, it is too large.  In order to
use the box counting method, it is convenient to work
with a smaller image, at least to begin with.  We will
reduce the images to 32 by 32 by taking every 16th pixel
in each direction.  So letting t=.512$1,15#0 and sb=.t#"1
t#b.  Check the size of sb.  Now suppose we want to count
how many boxes contain a 1 if we divide the array up into
m=.8 blocks in each direction.  Each block will have p=.4
pixels in each direction.  We can reshape the data into
the right blocks using a 4-dimensional array: 
(m,p,m,p)$,sb.  We want to apply a transpose to make this
have shape 8 8 4 4, i.e., a 8 by 8 matrix of 4 by 4
matrices.  Apply a suitable transpose to do that, and get
result r.  If you compare sb with <"2 r you should see
very similar data.  Now we can check if there is a 1 in
any of the 4 by 4 matrices of r using +./"1+./"1 r.  This
leads to an 8 by 8 boolean matrix.  Use an expression to
count the number of 1's in that matrix, and call your
result N.  Now (^.N)%^.m gives the estimate for the
fractal dimension.  Automate this so that given m and the
array sb you can estimate the fractal dimension of sb
using m blocks in each direction by asking for m fd sb. 
     Now estimate the fractal dimension of the half size
image of carpet.bmp using m equal to 2, 4, 8, 16, 32, 64,
128, and 256.  Are the answers close to the theoretic?
     Try to guess the fractal dimension of andy-dla.bmp. 
Then estimate the fractal dimension of the half size
image of andy-dla.bmp using m equal to 2, 4, 8, 16, 32,
64, 128, and 256.  Are the answers close to what you
expected?
     Try to guess the fractal dimension of andy-den.bmp. 
Then estimate the fractal dimension of the half size
image of andy-den.bmp using m equal to 2, 4, 8, 16, 32,
64, 128, and 256.  Are the answers close to what you
expected?
     By the way, you could do this for the full size
images, but you would first need to reset the virtual
memory to 32M and then each estimate will take a few
minutes. Example 4: Image Processing

Warmup with boolean differencing of vectors

Look at 256 color image:  Bears  

Ask class sequence of questions

      How to make this into a gray-scale image?

      How to make this into a monochrome image?

      How to make this into a 2 color image?

      How to detect the edges?Example 5: Mandelbrot Set

The Mandelbrot set is generated by coloring every complex
point, c, by the escape time of 0 to  under iteration of the
map z2+c.  For example:

f=.'' : '+/4>|(((x.j.y.)&+@*:)^:(i.25) 0'

   1 f 0
3
   0.5 f 0
6
   0.25 f 0
25
   0 f 0
25
    
   0 f 1
25
   0 f 2
2
   0 f 1.01
9
Example 6: Bit based fractals

   |:x=.#:i.16
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
   x +./ . *. |:x
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1
0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1
0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
0 1 0 1 1 1 1 1 0 1 0 1 1 1 1 1
0 0 1 1 1 1 1 1 0 0 1 1 1 1 1 1
0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
0 1 0 1 0 1 0 1 1 1 1 1 1 1 1 1
0 0 1 1 0 0 1 1 1 1 1 1 1 1 1 1
0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1
0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1
0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
   x +/ . * |:x
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1
0 1 1 2 0 1 1 2 0 1 1 2 0 1 1 2
0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
0 1 0 1 1 2 1 2 0 1 0 1 1 2 1 2
0 0 1 1 1 1 2 2 0 0 1 1 1 1 2 2
0 1 1 2 1 2 2 3 0 1 1 2 1 2 2 3
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
0 1 0 1 0 1 0 1 1 2 1 2 1 2 1 2
0 0 1 1 0 0 1 1 1 1 2 2 1 1 2 2
0 1 1 2 0 1 1 2 1 2 2 3 1 2 2 3
0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2
0 1 0 1 1 2 1 2 1 2 1 2 2 3 2 3
0 0 1 1 1 1 2 2 1 1 2 2 2 2 3 3
0 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4Example 7: Plasma Clouds (Fractal Mountains)

NB.  Cosine

cos=. 2&o.                                     


NB.  Uniform random real in [0,1] of given shape

randunif=. (? % ])@($&1e9)


NB.  Random standard normal of given shape

randsn=. (cos@+:@o.@randunif) * (%:@-@+:@^.@randunif) 


NB.  Interpolates along last axis

interp=.(}:@((2&#@i.)+ +: $ 0&,)@#) { (,-:@(}. + }:)) 


NB. Random normal additions to given matrix with
NB. given standard deviation

randadd=.]+[*randsn@$@]                              


NB.  Interpolate and random addition

ira=.[ randadd interp@((interp"1)@])


NB. Interpolate and random fractal addition; 
NB. Left argument is fractal exponent
 
irfa=.((%@<:@#@])^[)ira ]                            


NB.  Gives ranking of numeric arrays into n-ciles 

cile=. $@] $ ((/:@/:@] <.@* (% #)),)   randsn 4 4
  2.01182 _0.556612   1.14007  _0.974733
_0.674335  _2.09609   0.79741   0.499632
_0.362277   1.15972 _0.649773   _0.52574
_0.783146   2.04609 0.0863174 _0.0698044

   ]m=.i.2 2
0 1
2 3

   interp m
0 1
1 2
2 3

   interp"1 interp m
0 0.5 1
1 1.5 2
2 2.5 3

   0.01 ira m
_0.00422027 0.500165 0.991342
    1.00859  1.49494    2.001
    1.99798  2.48459  3.00019

   ]z=.(0.85&irfa)^:2 m
0.0157916 0.389541 _0.085276 1.37333    2.0692
 0.317867  1.46518  0.852204 1.74676   2.37282
_0.471451  1.13786   2.87701 1.12104  0.872519
 0.950376  1.43439   1.44719 1.36025 0.0155125
  1.56213  2.29364   2.01449 1.96959   1.02112

   3 cile z
0 0 0 1 2
0 1 0 2 2
0 1 2 1 0
0 1 1 1 0
2 2 2 2 1
Example 8: Raytracing & 3-D Fractals

   <<"1 #:i.4
Ŀ
Ŀ
0 00 11 01 1
ٳ

   3#<<"1 #:i.4
Ŀ
ĿĿĿ
0 00 11 01 10 00 11 01 10 00 11 01 1
ٳٳٳ

   ${3#<<"1 #:i.4
4 4 4
   {.{.{3#<<"1 #:i.4
Ŀ
ĿĿĿĿ
0 00 00 00 00 00 10 00 01 00 00 01 1
ٳٳٳٳ

   $>>{3#<<"1 #:i.4
4 4 4 3 2
   $+./"2>>{3#<<"1 #:i.4
4 4 4 2
   $*./"1+./"2>>{3#<<"1 #:i.4
4 4 4
   <"2*./"1+./"2>>{3#<<"1 #:i.4
Ŀ
0 0 0 10 0 1 10 1 0 11 1 1 1
0 0 1 10 0 1 11 1 1 11 1 1 1
0 1 0 11 1 1 10 1 0 11 1 1 1
1 1 1 11 1 1 11 1 1 11 1 1 1

   <"2*./"1+./"2>>{3#<<"1 (3 3#:i.9)

0 0 0 0 1 2 0 2 20 0 0 1 1 1 2 2 20 0 0 2 1 2 2 2 20 
0 0 0 1 1 1 2 2 20 0 0 1 1 1 2 2 20 0 0 1 1 1 2 2 21 
0 0 0 2 1 2 2 2 20 0 0 1 1 1 2 2 20 0 0 2 1 2 2 2 22 
0 1 2 0 1 2 0 1 21 1 1 1 1 1 1 1 12 1 2 2 1 2 2 1 20 
1 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 11 
2 1 2 2 1 2 2 1 21 1 1 1 1 1 1 1 12 1 2 2 1 2 2 1 22 
0 2 2 0 1 2 0 2 22 2 2 1 1 1 2 2 22 2 2 2 1 2 2 2 20 
2 2 2 1 1 1 2 2 22 2 2 1 1 1 2 2 22 2 2 1 1 1 2 2 21 
2 2 2 2 1 2 2 2 22 2 2 1 1 1 2 2 22 2 2 2 1 2 2 2 22 
 Closing Perspectives


 Gerunds simplified IFS

 Iteration and agenda were satisfactory program control

 Image processing interactive

 Memory constraints

 Graphics buffer

 Direct dynamic raster image manipulation

 Debugging

 Student reaction

 Hands on