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