FRACTALS in PYTHON

What's a Fractal?

In [1]:
from IPython.display import IFrame
IFrame('http://fract.al/background', width=1000, height=1100)
Out[1]:

What's Python?

In [9]:
IFrame('http://en.wikipedia.org/wiki/Python_(programming_language)', width=1000, height=1550)
Out[9]:

First fractal: MANDELBROT SET

In [2]:
from IPython.display import Image
In [3]:
Image(filename='images/Mandelbrot1.png')
Out[3]:
In [7]:
%matplotlib inline
from numpy import *
from matplotlib import pyplot as plt

def mandelbrot( h,w, maxit=40): #20
        '''
        Returns an image of the Mandelbrot fractal of size (h,w).
        '''
        y,x = ogrid[ -1.4:1.4:h*1j, -2:0.8:w*1j ]
        #y,x = ogrid[ -0.25:0.25:h*1j, -1.8:-1.3:w*1j ]
        #y,x = ogrid[ -0.05:0.05:h*1j, -1.5:-1.4:w*1j ]
        c = x+y*1j
        z = c
        divtime = maxit + zeros(z.shape, dtype=int)

        for i in xrange(maxit):
                z  = z**2 + c
                diverge = z*conj(z) > 2**2            # who is diverging
                div_now = diverge & (divtime==maxit)  # who is diverging now
                divtime[div_now] = i + 100                 # note when
                z[diverge] = 2                        # avoid diverging too much

        return divtime

#plt.figure()
fig = plt.subplots(1,figsize=(20,20))
s = "The Mandelbrot Set ([-2.0,0.8]x[-0.4,1.4], iterations = 40)"
#s = "The Mandelbrot Set ([-1.8,-1.3]x[-0.25,0.25], iterations = 40)"
#s = "The Mandelbrot Set ([-1.5,-1.4]x[-0.05,0.05], iterations = 40)" #"+str(maxit)+")"
plt.title(s)
plt.imshow(mandelbrot(1000,1000)) 
plt.axis('off')
plt.show()
In [7]:
run Mandelbrot.py
In [8]:
run Mandelbrot.py
In [9]:
run Mandelbrot.py
In [10]:
from IPython.display import IFrame
IFrame('http://en.wikipedia.org/wiki/Mandelbrot_set', width=1000, height=500)
Out[10]:

Julia Sets

In [10]:
run julia.py
Running ... 

In [12]:
Image(filename='images/MandelJulia.png')
Out[12]:
In [11]:
IFrame('http://en.wikipedia.org/wiki/Julia_set', width=1000, height=1000)
Out[11]:

ITERATED FUNCTION SYSTEMS (or IFS): John F. Hutchinson (1981), Michael Barnsley (1988)

In [12]:
IFrame('http://en.wikipedia.org/wiki/Iterated_function_system', width=1000, height=600)
Out[12]:

Second fractal: CANTOR SET

In [15]:
IFrame('http://en.wikipedia.org/wiki/Cantor_set', width=1000, height=1080)
Out[15]:
In [13]:
run Cantor.py -i 6 
{0: [0.0, 1.0], 1: [0.0, 0.3333333333333333, 0.6666666666666666, 1.0], 2: [0.0, 0.1111111111111111, 0.2222222222222222, 0.3333333333333333, 0.6666666666666666, 0.7777777777777777, 0.8888888888888888, 1.0], 3: [0.0, 0.037037037037037035, 0.07407407407407407, 0.1111111111111111, 0.2222222222222222, 0.25925925925925924, 0.2962962962962963, 0.3333333333333333, 0.6666666666666666, 0.7037037037037037, 0.7407407407407407, 0.7777777777777777, 0.8888888888888888, 0.9259259259259258, 0.9629629629629629, 1.0], 4: [0.0, 0.012345679012345678, 0.024691358024691357, 0.037037037037037035, 0.07407407407407407, 0.08641975308641975, 0.09876543209876543, 0.1111111111111111, 0.2222222222222222, 0.2345679012345679, 0.24691358024691357, 0.25925925925925924, 0.2962962962962963, 0.30864197530864196, 0.32098765432098764, 0.3333333333333333, 0.6666666666666666, 0.6790123456790124, 0.691358024691358, 0.7037037037037037, 0.7407407407407407, 0.7530864197530864, 0.7654320987654321, 0.7777777777777777, 0.8888888888888888, 0.9012345679012346, 0.9135802469135802, 0.9259259259259258, 0.9629629629629629, 0.9753086419753085, 0.9876543209876543, 1.0], 5: [0.0, 0.004115226337448559, 0.008230452674897118, 0.012345679012345678, 0.024691358024691357, 0.028806584362139915, 0.03292181069958847, 0.037037037037037035, 0.07407407407407407, 0.07818930041152262, 0.08230452674897118, 0.08641975308641975, 0.09876543209876543, 0.10288065843621398, 0.10699588477366254, 0.1111111111111111, 0.2222222222222222, 0.22633744855967075, 0.23045267489711932, 0.2345679012345679, 0.24691358024691357, 0.2510288065843621, 0.2551440329218107, 0.25925925925925924, 0.2962962962962963, 0.3004115226337448, 0.30452674897119336, 0.30864197530864196, 0.32098765432098764, 0.3251028806584362, 0.3292181069958848, 0.3333333333333333, 0.6666666666666666, 0.6707818930041152, 0.6748971193415638, 0.6790123456790124, 0.691358024691358, 0.6954732510288065, 0.6995884773662552, 0.7037037037037037, 0.7407407407407407, 0.7448559670781894, 0.7489711934156379, 0.7530864197530864, 0.7654320987654321, 0.7695473251028806, 0.7736625514403292, 0.7777777777777777, 0.8888888888888888, 0.8930041152263375, 0.897119341563786, 0.9012345679012346, 0.9135802469135802, 0.9176954732510287, 0.9218106995884774, 0.9259259259259258, 0.9629629629629629, 0.9670781893004115, 0.97119341563786, 0.9753086419753085, 0.9876543209876543, 0.9917695473251029, 0.9958847736625513, 1.0]}

Third fractal: SIERPINSKI TRIANGLE

In [14]:
run Sierpinski.py

Randomly colored Sierpinski triangle

In [15]:
run rcoloredSierpinski.py

Asymmetric Sierpinski triangle

In [16]:
run asymmSierpinski.py

Randomly colored asymmetric Sierpinski triangle

In [17]:
run asymmrcoloredSierpinski.py

A star of randomly colored Sierpinski triangles

In [18]:
run Sierpinski_rcoloredStar.py

A square of four randomly colored asymmetric Sierpinski triangles

In [19]:
run RectasymmrcoloredSierpinski.py
In [90]:
IFrame('http://en.wikipedia.org/wiki/Sierpinski_triangle', width=1000, height=1200)
Out[90]:

Fourth fractal: SIERPINSKI CARPET

In [20]:
run SierpinskiCarpet.py

Randomly colored Sierpinski carpet

In [21]:
run rcoloredSierpinskiCarpet.py
In [91]:
IFrame('http://en.wikipedia.org/wiki/Sierpinski_carpet', width=1000, height=500)
Out[91]:

Fifth fractal: VICSEK FRACTAL

In [26]:
run Vicsek_fractal.py
In [27]:
run Vicsek_fractal1.py
In [92]:
from IPython.display import IFrame
IFrame('http://en.wikipedia.org/wiki/Vicsek_fractal', width=1000, height=1300)
Out[92]:

Sixth fractal: KOCH SNOWFLAKE

Η γραμμή της νυφάδας του Koch

In [28]:
run Koch_line.py

Koch snowflake triangle

In [29]:
run Koch_triangle.py

A constellation of 7 Koch snowflakes

In [6]:
run cons7Koch.py
In [93]:
IFrame('http://en.wikipedia.org/wiki/Koch_snowflake', width=1000, height=1030)
Out[93]:

Seventh fractal: APOLLONIAN GASKET

In [3]:
run ags1.py
In [19]:
from IPython.display import IFrame
IFrame('http://en.wikipedia.org/wiki/Apollonian_gasket', width=1000, height=1000)
Out[19]:

Descartes' Theorem of Four Kissing Circles - Soddy Circles

In [20]:
IFrame('http://en.wikipedia.org/wiki/Descartes%27_theorem', width=1000, height=1300)
Out[20]:
In [10]:
run ags.py
In [5]:
run TrianglePackedWithCircles.py

LINDENMAYER SYSTEMS (L-SYSTEMS)

In [84]:
IFrame('http://en.wikipedia.org/wiki/L-system', width=1000, height=1300)
Out[84]:

TURTLE GRAPHICS

In [2]:
IFrame('http://en.wikipedia.org/wiki/Turtle_graphics', width=1000, height=800)
Out[2]:

Eighth fractal: HILBERT CURVE

In [6]:
run Hilbert.py
In [95]:
IFrame('http://en.wikipedia.org/wiki/Hilbert_curve', width=1000, height=1200)
Out[95]:

Nineth fractal: DRAGON CURVE

In [7]:
run dragon.py
In [97]:
IFrame('http://en.wikipedia.org/wiki/Dragon_curve', width=1000, height=1300)
Out[97]:

Tenth fractal: LEVY C CURVE

In [8]:
run levyC.py
In [98]:
IFrame('http://en.wikipedia.org/wiki/L%C3%A9vy_C_curve', width=1000, height=1000)
Out[98]:

Eleventh fractal: FRACTAL TREES

In [9]:
run frtree.py
In [6]:
IFrame('http://www.math.union.edu/research/fractaltrees/', width=1000, height=500)
Out[6]:

Twelveth fractal: BARNSLEY'S FERN

In []:
run fern.py
In [12]:
Image(filename='images/BarnsleyFern1.jpg')
Out[12]:
In [13]:
Image(filename='images/BarnsleyFern.jpg')
Out[13]:

CHAOS GAME (Michael Barnsley)

In [11]:
IFrame('http://en.wikipedia.org/wiki/Chaos_game', width=1000, height=600)
Out[11]:
In [7]:
IFrame('http://en.wikipedia.org/wiki/Barnsley_fern', width=1000, height=500)
Out[7]:

HAUSDORFF DIMENSION

In [28]:
IFrame('http://en.wikipedia.org/wiki/Hausdorff_dimension', width=1000, height=1000)
Out[28]:

LIST OF FRACTALS BY HAUSDORFF DIMENSION

In [29]:
IFrame('http://en.wikipedia.org/wiki/List_of_fractals_by_Hausdorff_dimension', width=1000, height=1000)
Out[29]:

FRACTAL DIMENSION

In [55]:
IFrame('http://en.wikipedia.org/wiki/Fractal_dimension', width=1000, height=680)
Out[55]:

INSTALLING PYTHON

In [40]:
IFrame('http://legacy.python.org/download/', width=1000, height=500)
Out[40]:

INSTALLING IPYTHON NOTEBOOK

In [45]:
IFrame('http://ipython.org/notebook.html', width=1000, height=1000)
Out[45]:
In [44]:
IFrame('http://opentechschool.github.io/python-data-intro/core/notebook.html', width=1000, height=1000)
Out[44]:

PYTHON IN THE CLOUD

In [47]:
IFrame('https://www.pythonanywhere.com/', width=1000, height=800)
Out[47]:

IPYTHON NOTEBOOK IN THE CLOUD

In [49]:
IFrame('https://wakari.io/', width=1000, height=1000)
Out[49]:
In []: