{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\nConvolution and frontier of the Mandelbrot set\n================================================\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Convolution with a 2D kernel\n---------------------------------\nBefore we begin, a reminder of the 2D convolution between two matrix can be useful.\nIn our case, we will use a $3\\times 3$ kernel. So, the convolution of a kernel with a matrix is defined as the\nsum of the conter-row-wise by row-wise product of the elements ie the last element of the kernel is multiplied by the first of the matrix,\nthe penultimate of the kernel (at the left of the last) is multiplied by the seconde one of the matrix (at the right of the first) and so\non from the antepenultimate to the first one. In a formula we have :\n\n\\begin{align}\\begin{bmatrix} k_1 & k_2 & k_3 \\\\ k_4 & k_5 & k_6 \\\\ k_7 & k_8 & k_9 \\\\ \\end{bmatrix} \\star\n    \\begin{bmatrix} c_1 & c_2 & c_3 \\\\ c_4 & c_5 & c_6 \\\\ c_7 & c_8 & c_9 \\\\ \\end{bmatrix} =\n      k_1 c_9 + k_2c_8 + \\dots + k_9c_1\\end{align}\n\nThere are mutliple ways to get the edges of a shape using this method. We chose to use a kernel with only $-1$ on its borders\nand a value $k_5=8=-\\sum_{i=1,\\ i\\neq 5}^9 k_i$. We also need to pad the image in order to get all the pixels in it, and not\nforget the borders. Let's take a look at the result.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import chaoseverywhere as chaos\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom scipy.signal import convolve2d\n\nmandel = chaos.Mandelbrot_disp(-.5, 0, 1.5).mandelbrot()\nkernel_edge_detect = np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]])\npad_mandel = np.pad(mandel, ((1, 1), (1, 1)), \"maximum\")\nbound = convolve2d(pad_mandel, kernel_edge_detect, mode='valid').astype(bool) * mandel\n\nplt.imshow(bound, cmap='bone')\nplt.axis('off')\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "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.7.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}