Advertisement
morty0505

Untitled

Oct 9th, 2016
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. import math
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4.  
  5. import time
  6.  
  7.  
  8. def mandel_iter(c,max_escape_time):
  9. z = c
  10. for count in range(1,max_escape_time+1):
  11. if abs(z) > 2:
  12. return count
  13. z = z*z + c
  14.  
  15. return max_escape_time
  16.  
  17.  
  18. def compute_mandelbrot(xmin,xmax,ymin,ymax,Nx,Ny,max_escape_time=1000,
  19. plot_filename=None,cur_cmap=plt.cm.prism,totime=False):
  20.  
  21.  
  22. t0 = time.clock()
  23.  
  24.  
  25. X = np.linspace(xmin, xmax, Nx, endpoint=True) #real values
  26. Y = np.linspace(ymin, ymax, Ny, endpoint=True) #imag values
  27. Y = np.flipud(Y) #reverse Y to get the right direction on the plot
  28.  
  29. #A little bit faster with list instead of array for some reason
  30. res = [[0 for x in range(len(X))] for y in range(len(Y))]
  31. # res = np.empty(shape=(Ny,Nx),dtype=np.float)
  32.  
  33. # do computation
  34. for y_index,imag in enumerate(Y):
  35. for x_index,real in enumerate(X):
  36. res[y_index][x_index] = mandel_iter(complex(real,imag),max_escape_time)
  37.  
  38. t1 = time.clock()
  39.  
  40.  
  41. t1 = time.clock()
  42.  
  43. if totime:
  44. print("Computation time (basic python) : " + str(t1-t0))
  45.  
  46. #Draw if plot_filename provided
  47. if plot_filename:
  48. #Mask for drawing, to get points in mandelbrot black
  49. Z = np.asarray(res,dtype=np.float64)
  50. Z[Z == max_escape_time] = np.NaN
  51. masked_array = np.ma.array(Z, mask=np.isnan(Z))
  52.  
  53. cur_cmap.set_bad('black',1.)
  54. plt.imshow(Z, cmap = cur_cmap, extent =(xmin, xmax, ymin, ymax))
  55. plt.savefig(plot_filename+'.png')
  56. plt.show()
  57.  
  58. res = np.asarray(res,dtype=np.float64)
  59. return res #return non-masked results, with max_escape_time instead of np.NaN
  60.  
  61. if __name__ == "__main__":
  62. compute_mandelbrot(
  63. xmin = -2,
  64. xmax = 1,
  65. ymin=-1,
  66. ymax=1,
  67. Nx=300,
  68. Ny=300,
  69. max_escape_time=1000,
  70. # plot_filename="m1",
  71. plot_filename=None,
  72. totime=True
  73. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement