{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# A Mandelbrot set demo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook comes from the following snippet:\n",
    "https://raw.githubusercontent.com/jeremiedecock/snippets/master/python/matplotlib/mandelbrot/mandelbrot.py\n",
    "\n",
    "To display this notebook on nbviewer.jupyter.org, visit the following link:\n",
    "http://nbviewer.jupyter.org/url/download.tuxfamily.org/jdhp/notebook/notebook/mandelbrot.ipynb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# %load https://raw.githubusercontent.com/jeremiedecock/snippets/master/python/matplotlib/mandelbrot/mandelbrot.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "import matplotlib\n",
    "matplotlib.rcParams['figure.figsize'] = (6, 6)\n",
    "\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from mpl_toolkits.mplot3d import axes3d\n",
    "from matplotlib import cm\n",
    "\n",
    "import ipywidgets\n",
    "from ipywidgets import interact"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "EPSILON_MAX = 2.\n",
    "NUM_IT_MAX = 32\n",
    "Z_INIT = complex(0, 0)\n",
    "\n",
    "def mandelbrot(x, y):\n",
    "    it = 0\n",
    "    z = Z_INIT\n",
    "    c = complex(x, y)\n",
    "\n",
    "    # Rem: abs(z) = |z| = math.sqrt(pow(z.imag,2) + pow(z.real,2))\n",
    "    while it < NUM_IT_MAX and abs(z) <= EPSILON_MAX:\n",
    "        z = pow(z, 2) + c\n",
    "        it += 1\n",
    "\n",
    "    # To print all levels\n",
    "    return it\n",
    "\n",
    "    ## To print only the level #N\n",
    "    #if it < N:\n",
    "    #    return 1\n",
    "    #else:\n",
    "    #    return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "REAL_RANGE = np.arange(-2.0, 1.0, 0.05).tolist()\n",
    "IMAG_RANGE = np.arange(-1.2, 1.2, 0.05).tolist()\n",
    "\n",
    "# Compute datas #############\n",
    "xgrid, ygrid = np.meshgrid(REAL_RANGE, IMAG_RANGE)\n",
    "data = np.array([mandelbrot(x, y) for y in IMAG_RANGE for x in REAL_RANGE]).reshape(len(IMAG_RANGE), len(REAL_RANGE))\n",
    "\n",
    "# Plot data #################\n",
    "fig = plt.figure()\n",
    "ax = axes3d.Axes3D(fig)\n",
    "\n",
    "# Plot mean surface\n",
    "ax.plot_surface(xgrid, ygrid, data, cmap=cm.jet, rstride=1, cstride=1, color='b', shade=True)\n",
    "\n",
    "# Set title and labels\n",
    "plt.title(\"Mandelbrot set\")\n",
    "ax.set_xlabel(\"Re(c)\")\n",
    "ax.set_ylabel(\"Im(c)\")\n",
    "ax.set_zlabel(\"Color\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Plot data #################\n",
    "\n",
    "REAL_RANGE = np.arange(-2.0, 1.0, 0.005).tolist()\n",
    "IMAG_RANGE = np.arange(-1.2, 1.2, 0.005).tolist()\n",
    "\n",
    "# Compute datas #############\n",
    "\n",
    "xgrid, ygrid = np.meshgrid(REAL_RANGE, IMAG_RANGE)\n",
    "data = np.array([mandelbrot(x, y) for y in IMAG_RANGE for x in REAL_RANGE]).reshape(len(IMAG_RANGE), len(REAL_RANGE))\n",
    "\n",
    "# Plot data #################\n",
    "fig, ax = plt.subplots()\n",
    "\n",
    "# Plot mean surface\n",
    "ax.imshow(data, extent=[xgrid.min(), xgrid.max(), ygrid.min(), ygrid.max()], interpolation=\"bicubic\")\n",
    "\n",
    "# Set title and labels\n",
    "plt.title(\"Mandelbrot set\")\n",
    "ax.set_xlabel(\"Re(c)\")\n",
    "ax.set_ylabel(\"Im(c)\")"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}