In [6]:
"""Numpy usage with basic samples"""
import numpy as np

print(np.version.version)
print(np.array([1, 2, 3, 4]))

c = np.array( [ [1,2], [3,4] ], dtype=complex )
print(c)


1.13.1
[1 2 3 4]
[[ 1.+0.j  2.+0.j]
 [ 3.+0.j  4.+0.j]]

In [10]:
"""For 2D array"""
import numpy as np

a = np.arange(15).reshape(5, 3)
print('1. First 2D array:\n', a)


First 2D array:
 [[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]
 [12 13 14]]

In [16]:
import numpy as np
import matplotlib.pyplot as plt

def mandelbrot( h,w, maxit=20 ):
     """Returns an image of the Mandelbrot fractal of size (h,w)."""
     y,x = np.ogrid[ -1.4:1.4:h*1j, -2:0.8:w*1j ]
     c = x+y*1j
     z = c
     divtime = maxit + np.zeros(z.shape, dtype=int)
     
     for i in range(maxit):
         z = z**2 + c
         diverge = z*np.conj(z) > 2**2         # who is diverging
         div_now = diverge & (divtime==maxit)  # who is diverging now
         divtime[div_now] = i                  # note when
         z[diverge] = 2                        # avoid diverging too much

     return divtime

plt.imshow(mandelbrot(400,400))
plt.show()



In [ ]: