{ "metadata": { "name": "", "signature": "sha256:d531183e20f48decbbf35fcd1810c9d984cf8f72958c958781bf0c6b1998a37a" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Mandelbrot Sets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----------------" ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Imports" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "from pylab import cm\n", "\n", "%matplotlib inline" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Settings" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Establish the area of the plot to look at. The \n", "# Mandelbrot set is a fractal defined to exist in\n", "# the set of imaginary numbers.\n", "#\n", "# You can see the whole set when you \"zoom out\" so that:\n", "#\n", "# real axes go from -2 to 1\n", "# imaginary axes from -1 to 1\n", "#\n", "# For this notebook the view is defined by two points. The \n", "# \"upper left\" and the \"lower right\". To see the whole set \n", "# you would use an upper of -2 + 1j and a lower of -1 + 1j.\n", "\n", "# The view is defined by two complex numbers.\n", "g_view = {\"upper\": -2 + 1j, \"lower\": 1 - 1j}\n", "#g_view = {\"upper\": 0+0j, \"lower\": 1 - 1j}\n", "\n", "# The resolution of the image in pixels\n", "# The larger the image, the more time it will take to calculate\n", "g_resolution = 320\n", "\n", "# The maximum iterations. The more iterations, the longer it will\n", "# take to calculate the mandelbrot set.\n", "g_max_iterations = 128" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Functions" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def complex_range(upper, lower, resolution):\n", " \"\"\"Yields an iterable range of pixel points and complex numbers\"\"\"\n", " step_y = (upper.imag - lower.imag) / (resolution - 1)\n", " step_x = (lower.real - upper.real) / (resolution - 1)\n", " for x in range(resolution):\n", " real = upper.real + (x * step_x)\n", " for y in range(resolution):\n", " yield x, y, complex(real, lower.imag + (y * step_y))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "def test_point(point, max_iterations):\n", " \"\"\"Test a point for inclusion in Mandelbrot set.\n", " 1.0 means it is in the set. Anything else is out.\"\"\"\n", " zn = complex(0,0)\n", " c = point\n", " for n in range(1, max_iterations + 1):\n", " zn = zn * zn + c\n", " if abs(zn) >= 2:\n", " return 1 - (float(n) / max_iterations)\n", " return 1.0" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "def aspect_ratio(upper, lower):\n", " return (upper.imag - lower.imag) / (lower.real - upper.real)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Generate an Image" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Calculate the mandelbrot\n", "i = np.array([[0]*g_resolution]*g_resolution, dtype=float)\n", "for x, y, c in complex_range(g_view[\"upper\"], g_view[\"lower\"], g_resolution):\n", " i[y][x] = test_point(c, g_max_iterations)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Show the mandelbrot\n", "plt.figure(figsize=(10,10))\n", "#plt.axis([g_view[\"upper\"].real, g_view[\"lower\"].real, g_view[\"lower\"].imag, g_view[\"upper\"].imag])\n", "img = plt.imshow(i, aspect=aspect_ratio(g_view[\"upper\"], g_view[\"lower\"])) #, cmap=cm.gray)\n", "\n", "# Try different colormaps\n", "#img.set_cmap('Greys')\n", "img.set_cmap('RdBu')\n", "#img.set_cmap('gray')\n", "#img.set_cmap('binary')\n", "#img.set_cmap('jet')\n", "#img.set_cmap('gist_rainbow')\n", "#img.set_cmap('hot')\n", "#img.set_cmap('afmhot')\n", "#img.set_cmap('RdGy')\n", "#img.set_cmap('RdYlGn')\n", "#img.set_cmap('RdYlBu')\n", "#img.set_cmap('Dark2')\n", "#img.set_cmap('hsv')\n", "\n", "#plt.colorbar()\n", "#img.write_png(\"mandelbrot.png\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "-------------------------" ] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Resources" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* [Benoit Mandelbrot](http://en.wikipedia.org/wiki/Benoit_Mandelbrot)\n", "* [Mandelbrot Set](http://en.wikipedia.org/wiki/Mandelbrot_set)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "--------------------" ] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Copyright Notice" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "

\n", " \n", " \"CC0\"\n", " \n", "
\n", " To the extent possible under law,\n", " \n", " Charles Stanhope\n", " has waived all copyright and related or neighboring rights to\n", " Mandelbrot Sets.\n", "This work is published from:\n", "\n", " United States.\n", "

\n", "" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }