Lesson 17 - Julia Sets

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

Complex Number Class

In [2]:
class Complex:
    
    def __init__(self, x, y):
        self.x = x
        self.y = y
    
    def __add__(self, other):
        temp = Complex(self.x + other.x, self.y + other.y)
        return temp
        
    def __mul__(self, other):
        newx = self.x * other.x - self.y * other.y
        newy = self.x * other.y + self.y * other.x
        temp = Complex(newx, newy)
        return temp
    
    def __str__(self):
        return str([self.x, self.y])
    
    def size(self):
        return (self.x**2 + self.y**2)**0.5
    
In [3]:
z1 = Complex(2,3)
z2 = Complex(1,2)

print(z1 + z2)
print(z1 * z2)
[3, 5]
[-4, 7]
In [4]:
z = Complex(0.8, 0.2)

for i in range(0, 5):
    z = z * z
    print(z)
[0.6000000000000001, 0.32000000000000006]
[0.25760000000000005, 0.3840000000000001]
[-0.08109824000000006, 0.1978368000000001]
[-0.03256247490314243, -0.03208843257446404]
[3.064726673185575e-05, 0.0020897575607743265]
In [5]:
z1 = Complex(0.8,0.2)
z2 = Complex(-.7,0.25)
z3 = Complex(1,0.1)

xlist1, ylist1 = [z1.x], [z1.y]
xlist2, ylist2 = [z2.x], [z2.y]
xlist3, ylist3 = [z3.x], [z3.y]

for i in range(0, 5):
    z1 = z1 * z1
    z2 = z2 * z2
    z3 = z3 * z3
    
    xlist1.append(z1.x), ylist1.append(z1.y)
    xlist2.append(z2.x), ylist2.append(z2.y)
    xlist3.append(z3.x), ylist3.append(z3.y)
    
fig=plt.figure(figsize=(8, 8))
plt.plot(xlist1, ylist1, marker='.', markersize=10)
plt.plot(xlist2, ylist2, marker='.', markersize=10)
plt.plot(xlist3, ylist3, marker='.', markersize=10)
plt.plot([-2,2], [0,0], c='black', linewidth=1)
plt.plot([0,0], [-2,2], c='black', linewidth=1)
plt.xlim(-1.2,1.2)
plt.ylim(-0.5,1.2)
plt.show()

Heat Maps

In [6]:
xgrid = [0,1,2,3]
ygrid = [0,1,2,3]
shade = [[0,0,0],[0,1,1],[0,2,3]]

for row in shade:
    print(row)

my_cmap = plt.get_cmap('coolwarm')
#cmap = plt.get_cmap('plasma')
#cmap = plt.get_cmap('nipy_spectral')
#cmap = plt.get_cmap('spring_r')
   
plt.close()
plt.figure(figsize = (2,2), dpi=200)
plt.pcolormesh(xgrid, ygrid, shade, cmap=my_cmap)
#plt.axis('off')
plt.colorbar()
plt.show()
[0, 0, 0]
[0, 1, 1]
[0, 2, 3]

Julia Function

In [7]:
def julia(c, iter = 200, res = 100, pal=0):
    
    xgrid = np.array(range(-2*res,2*res))/res
    ygrid = np.array(range(-2*res,2*res))/res
    
    iterations = []
    for i in range(-2*res,2*res):
        
        row = []    
            
        y = i / res
           
        for j in range(-2*res,2*res):
            
            x = j / res
            
            z = Complex(x,y)
            
            for n in range(1,iter + 1):
                
                z = z * z + c
                if(z.size() > 2):
                    break
                
            row.append(n)
            
        iterations.append(row)
    
    palettes = [plt.get_cmap('plasma'), plt.get_cmap('nipy_spectral'), 
                plt.get_cmap('coolwarm'), plt.get_cmap('spring_r')]
    
    cmap = palettes[pal]
    
    plt.close()
    plt.figure(figsize = (4,4), dpi=200)
    plt.pcolormesh(xgrid, ygrid, iterations,cmap = cmap)
    plt.axis('off')
    plt.show()
In [8]:
c = Complex(0, 0)
julia(c)
In [9]:
c = Complex(-0.75, 0)
julia(c)
In [10]:
c = Complex(-0.75, 0.1)
julia(c)
In [11]:
c = Complex(-0.75, 0.1)
julia(c, iter=400)
In [12]:
c = Complex(-0.1, 0.8)
julia(c, iter=300, pal=1)
In [13]:
c = Complex(-0.1, 0.9)
julia(c, iter=300, pal=1)
In [14]:
c = Complex(-0.4, 0.5)
julia(c, iter=300, pal=2)
In [15]:
c = Complex(-0.4, 0.6)
julia(c, iter=300, pal=2)
In [16]:
c = Complex(0.36, 0.10)
julia(c, res=200, iter=400, pal=0)
In [17]:
c = Complex(0.36, 0.11)
julia(c, res=200, iter=400, pal=0)