Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Folder full of pertinent coursework

1666 views
Kernel: Python 2 (SageMath)
# Math libraries 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()
WARNING: 1 intermediate output message was discarded.
import math import numpy as np class JuliaSet(object): def __init__(self, c, m = 100): self._d = 0.001 self.c = c self.m = m self._complexplane = np.array([]) self.set = np.array([]) def create_a_plane(self): self.limit = 2 self.r = np.arange(-self.limit,self.limit,self._d) self._complexplane = self.r[:,np.newaxis] + self.r[np.newaxis,:]*1j def juliamap(self,z): return z**2 + self.c def iterate(self,z): for counter in range(1,self.m+1): z = self.juliamap(z) if math.sqrt(z.real**2 + z.imag**2) > 2 : return counter return 0 def set_spacing(self,d): self._d = d self.create_a_plane() return self._complexplane def generate(self): if not self._complexplane.size: self.create_a_plane() vfunc = np.vectorize(self.iterate) self.set = vfunc(self._complexplane) return self.set
class JuliaSetPlot(JuliaSet): """Extend JuliaSet to add plotting functionality""" def __init__(self, *args, **kwargs): # Invoke constructor for JuliaSet first, unaltered super(JuliaSetPlot, self).__init__(*args, **kwargs) # Add one more attribute: a rendered image array self.img = np.array([]) def get_dim(self): # get what should be an attribute return int(4.0 / self._d) def render(self): if not self.set: self.generate() # Convert inefficient list to efficient numpy array self.img = np.array(self.set) dim = self.get_dim() # Reshape array into a 2d complex plane self.img = np.reshape(self.img, (dim,dim)).T def show(self): if not self.img.size: self.render() # Specify complex plane axes efficiently xy = np.linspace(-2,2,self.get_dim()) # Use matplotlib to plot image as an efficient mesh plt.figure(1, figsize=(12,9)) 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() # Mimic matplotlib "hot" color palette colormap = plt.cm.get_cmap("hot") bokehpalette = [rgb2hex(m) for m in colormap(np.arange(colormap.N))] # Use bokeh to plot an interactive image f = blt.figure(x_range=[-2,2], y_range=[-2,2], plot_width=600, plot_height=600) f.image(image=[j.img], x=[-2,2], y=[-2,2], dw=[4], dh=[4], palette=bokehpalette) blt.show(f)
j = JuliaSetPlot(-1.037 + 0.17j) %time j.set_spacing(0.006) %time j.show()
CPU times: user 0 ns, sys: 4 ms, total: 4 ms Wall time: 16.2 ms
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-11-2a95bb0acec6> in <module>() 1 j = JuliaSetPlot(-1.037 + 0.17j) 2 get_ipython().magic(u'time j.set_spacing(0.006)') ----> 3 get_ipython().magic(u'time j.show()') /projects/sage/sage-6.9/local/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in magic(self, arg_s) 2334 magic_name, _, magic_arg_s = arg_s.partition(' ') 2335 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) -> 2336 return self.run_line_magic(magic_name, magic_arg_s) 2337 2338 #------------------------------------------------------------------------- /projects/sage/sage-6.9/local/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in run_line_magic(self, magic_name, line) 2255 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals 2256 with self.builtin_trap: -> 2257 result = fn(*args,**kwargs) 2258 return result 2259 /projects/sage/sage-6.9/local/lib/python2.7/site-packages/IPython/core/magics/execution.pyc in time(self, line, cell, local_ns) /projects/sage/sage-6.9/local/lib/python2.7/site-packages/IPython/core/magic.pyc in <lambda>(f, *a, **k) 191 # but it's overkill for just that one bit of state. 192 def magic_deco(arg): --> 193 call = lambda f, *a, **k: f(*a, **k) 194 195 if callable(arg): /projects/sage/sage-6.9/local/lib/python2.7/site-packages/IPython/core/magics/execution.pyc in time(self, line, cell, local_ns) 1161 if mode=='eval': 1162 st = clock2() -> 1163 out = eval(code, glob, local_ns) 1164 end = clock2() 1165 else: <timed eval> in <module>() <ipython-input-10-2c3d5a46376a> in show(self) 21 22 def show(self): ---> 23 if not self.img.size: self.render() 24 # Specify complex plane axes efficiently 25 xy = np.linspace(-2,2,self.get_dim()) <ipython-input-10-2c3d5a46376a> in render(self) 18 dim = self.get_dim() 19 # Reshape array into a 2d complex plane ---> 20 self.img = np.reshape(self.img, (dim,dim)).T 21 22 def show(self): /projects/sage/sage-6.9/local/lib/python2.7/site-packages/numpy/core/fromnumeric.pyc in reshape(a, newshape, order) 216 except AttributeError: 217 return _wrapit(a, 'reshape', newshape, order=order) --> 218 return reshape(newshape, order=order) 219 220 ValueError: total size of new array must be unchanged
j = JuliaSetPlot(-0.624 + 0.435j) %time j.set_spacing(0.006) %time j.interact()
%prun j.generate()
%load_ext line_profiler %lprun -f j.generate j.generate()