Scientif Computing

Homework 5

JuliaSets third version

Done by Sidy Danioko


In [1]:
# Math libraries
import math
import numpy as np 
from math import sqrt

# Matplotlib plotting libraries
import matplotlib.pyplot as plt
%matplotlib inline

# Bokeh plotting libraries
import bokeh.plotting as blt
blt.output_notebook()


BokehJS successfully loaded.

In [2]:
#!/usr/bin/env python
version = 0.4  # This is a version required for the homework

class JuliaSet(object):
    def __init__(self, c, min_val, max_val, n=100):
        self.c = c
        self.n = n
        self.min_val = min_val
        self.max_val = max_val
        self._d = .001
        self.set = np.array([])
        self._complexplane = np.array([])
    
    # Declare the function that will be used for iteration
    def juliamap(self, z):
        return z**2 + self.c # cosntruct the mapping function
    
    # Decalre the function that will allow iteration
    def iterate(self, z):
        m = 0
        while True:
            z = self.juliamap(z)
            m = m + 1 # increment thecounter
            if abs(z)>2: # make sure we do not count points whose modulus is > 2
                return m
            elif m >= self.n:
                return 0
    
    # Declare and Define that will set a plane while
    # forming an array of values in the x and y directions
    def gen_complexplane(self):
        arr = np.arange(self.min_val, self.max_val, self._d)
        x, y = np.meshgrid(arr, arr)
        self._complexplane = x+y*1j
        
    # Declare function that will us set the spacing             
    def set_spacing(self, d):
        self._d = d
        self.gen_complexplane()
    # Declare and Define function that will allow
    # the iteration function go through points and 
    # return a plane
    def generate(self):
        vfunc = np.vectorize(self.iterate)
        self.set = vfunc(self._complexplane)
        return self.set

In [3]:
class JuliaSetPlot(JuliaSet):
    
    def __init__(self, *args, **kwargs):
        JuliaSet.__init__(self, *args, **kwargs)
        self.img = np.array([])
    
    def get_dim(self):
        return int((self.min_val-self.max_val) / self._d)
    
    def render(self):
        if not self.set.any(): self.generate()
        self.img = np.array(self.set)
        dim = int(sqrt(self.img.size))
        print(self.img.size)
        
    def show(self):
        if not self.img.size: self.render()
        plt.figure(1, figsize=(12,9))   
        xy = np.linspace(self.min_val,self.max_val,int(sqrt(self.img.size)))
        plt.pcolormesh(xy, xy, self.img, cmap=plt.cm.hot)
        plt.colorbar()
        plt.show()
        
    def interact(self):
        from matplotlib.colors import rgb2hex
        if not self.img.size: self.render()
        colormap = plt.cm.get_cmap("hot")
        bokehpalette = [rgb2hex(m) for m in colormap(np.arange(colormap.N))]
        f = blt.figure(x_range=(self.p,self.q), y_range=(self.p,self.q), plot_width=600, plot_height=600)
        f.image(image=[self.img], x=[self.min_val], y=[self.max_val], dw=[self.max_val-self.min_val], dh=[self.max_val-self.min_val], palette=bokehpalette, dilate=True)
        blt.show(f)

In [4]:
JuliaObject = JuliaSetPlot(-1.0 + 0.0j, -2, 2)
%time JuliaObject.set_spacing(0.005)
%time JuliaObject.generate()
%time JuliaObject.show()


CPU times: user 16 ms, sys: 4 ms, total: 20 ms
Wall time: 20.2 ms
CPU times: user 7.13 s, sys: 40 ms, total: 7.17 s
Wall time: 9.51 s
640000
CPU times: user 2.06 s, sys: 260 ms, total: 2.32 s
Wall time: 2.75 s

In [7]:
JuliaObject = JuliaSetPlot(-1.037 + .17j, -2, 2)
%time JuliaObject.set_spacing(0.005)
%time JuliaObject.generate()
%time JuliaObject.show()


CPU times: user 12 ms, sys: 8 ms, total: 20 ms
Wall time: 18 ms
CPU times: user 5.8 s, sys: 28 ms, total: 5.83 s
Wall time: 7.6 s
640000
CPU times: user 2.14 s, sys: 100 ms, total: 2.24 s
Wall time: 3.52 s

In [ ]: