<commit_before>{
 "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
}<commit_msg>Improve a notebook.<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Mandelbrot set"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook is also available on [nbviewer.jupyter.org](http://nbviewer.jupyter.org/url/download.tuxfamily.org/jdhp/notebook/notebook/mandelbrot.ipynb)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Definition"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Soit la suite $\\{z_i\\}$ de nombres complexes defini par\n",
    "\n",
    "$$\n",
    "z_{i+1} = z^2_i + c\n",
    "$$\n",
    "\n",
    "avec $z_0 = 0$ et avec $c \\in \\mathbb C$ une constante fixée.\n",
    "\n",
    "L'ensemble de Mandelbrot est l'ensemble de tous les nombres $c$ pour lesquels cette suite converge ;\n",
    "la suite tend vers l'infini pour les nombres $c$ n'appartenant pas à l'ensemble de Mandelbrot.\n",
    "\n",
    "Ci-dessous, l'ensemble de Mandelbrot est représenté graphiquement dans le plan complexe (plus la suite diverge vite plus le point est claire).\n",
    "\n",
    "Référence: *Toutes les mathématiques et les bases de l'informatique*, <NAME>, Dunod, p.696"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## A python implementation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The following script is also available there:\n",
    "https://raw.githubusercontent.com/jeremiedecock/snippets/master/python/matplotlib/mandelbrot/mandelbrot.py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First, lets import some packages."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "import matplotlib\n",
    "matplotlib.rcParams['figure.figsize'] = (8, 8)\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": "markdown",
   "metadata": {},
   "source": [
    "Then lets define the Mandelbrot set."
   ]
  },
  {
   "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 = 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": "markdown",
   "metadata": {},
   "source": [
    "Finally, lets display the Mandelbrot set in the complex plane."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "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",
    "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",
    "# (nice color maps: summer, magma, gist_gray, gist_yarg, gist_heat, Blues, coolwarm, copper)\n",
    "ax.imshow(data, extent=[xgrid.min(), xgrid.max(), ygrid.min(), ygrid.max()], interpolation=\"bicubic\", cmap=cm.Blues)\n",
    "\n",
    "# Set title and labels\n",
    "plt.title(\"Mandelbrot set\")\n",
    "ax.set_xlabel(\"Re(c)\")\n",
    "ax.set_ylabel(\"Im(c)\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It can also be represented in 3 dimensions to illustrate the iterative process."
   ]
  },
  {
   "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(\"Iterations\");"
   ]
  }
 ],
 "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
}

{'fraction_non_alphanumeric': 0.2259446825087651, 'fraction_numerical': 0.012076353720296066, 'mean_word_length': 2.624779385810095, 'pattern_counts': {'":': 128, '<': 10, '<?xml version=': 0, '>': 4, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '300258', 'n_tokens_mistral': 4308, 'n_tokens_neox': 4217, 'n_words': 795}