# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/patztablook22/mandelbrot/blob/master/Mandelbrot_GPU.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="kkTxjoND_VUb"
# # Bureaucracy

# + colab={"base_uri": "https://localhost:8080/"} id="T2WNjGl7_sYh" outputId="aeca4e64-5593-4b7b-b067-a28e21335425"
# !apt-get install imagemagick

# + colab={"base_uri": "https://localhost:8080/"} id="nTaltNkJCtgY" outputId="bf51b47e-5904-487b-8443-16d362ab5b66"
from google.colab import drive
drive.mount('drive')

# + [markdown] id="vjLz80CKbQW_"
# # Mandelbrot with GPU-optimization
#
# Mandelrot's set $M$:
#
# $ \forall m \in \mathbb{C}: m \in M \leftrightarrow z = z^2 + m$ converges

# + id="9QPxTIj7OL6B"
import numpy as np
#import tensorflow as tf
import tensorflow.compat.v1 as tf
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation


# + id="WwYkzML4OXJl"
# mafs

def mandelbrot(c, threshold, exponent = 2):

  z = tf.zeros_like(c)

  # parallel iteration
  for i in range(threshold):
    z = z**exponent + c

  a = tf.abs(z)  
  # b = tf.constant(5, dtype = tf.dtypes.double)
  # not_diverged = (a <= b)
  # m = not_diverged* threshold
  return a



# + id="m0Frse8trlY6"
def render(resolutionXY, rangeX, rangeY):

  samplesX = tf.linspace(*rangeX, resolutionXY[0])
  samplesY = tf.linspace(*rangeY, resolutionXY[1])
  
  samplesX = tf.cast(samplesX, tf.double)
  samplesY = tf.cast(samplesY, tf.double)
  
  a,b      = tf.meshgrid(samplesX, samplesY)
  samples  = tf.complex(a, b) 

  m = mandelbrot(samples, 50)
  return sess.run(m)


# + colab={"base_uri": "https://localhost:8080/", "height": 1000} id="k5VUc0YA7rGM" outputId="db88c53b-2b41-4d50-dd7d-b558ec533bff"
center = (-.746, 0.1495)
radius = 2
resXY  = (320, 320)
fig = plt.figure(figsize=(12,12))
ax  = plt.axes()

def tick(i):
  i *= 4
  i += 100
  r = radius / (1 + (i/275)**8)
  z = render(
    resolutionXY = resXY,
    rangeX       = (center[0] - r, center[0] + r),
    rangeY       = (center[1] - r, center[1] + r),
  )
  print("progress: {}".format(i))
  ax.clear()
  ax.set_xticks([])
  ax.set_yticks([])
  return [ax.imshow(z)]

ani = 0
with tf.Session() as sess:
  ani = FuncAnimation(
      fig      = fig,
      func     = tick,
      frames   = 190,
      interval = 30,
      blit     = True,
  )
  ani.save("out.gif", writer="imagemagick", )


# + id="pZgwdoRNAKef"
# cp out.gif drive/MyDrive/out/out12.gif

StarcoderdataJupyterScriptsDedupFiltered