{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Roll\nThis example shows how to use the :py:class:`pylops.Roll` operator.\n\nThis operator simply shifts elements of multi-dimensional array along a\nspecified direction a chosen number of samples.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport matplotlib.pyplot as plt\n\nimport pylops\n\nplt.close('all')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Let's start with a 1d example. We make a signal, shift it by two samples\nand then shift it back using its adjoint. We can immediately see how the\nadjoint of this operator is equivalent to its inverse.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nx = 10\nx = np.arange(nx)\n\nRop = pylops.Roll(nx, shift=2)\n\ny = Rop*x\nxadj = Rop.H*y\n\nplt.figure()\nplt.plot(x, 'k', lw=2,  label='x')\nplt.plot(y, 'b', lw=2, label='y')\nplt.plot(xadj, '--r', lw=2, label='xadj')\nplt.title('1D Roll')\nplt.legend()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We can now do the same with a 2d array.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "ny, nx = 10, 5\nx = np.arange(ny*nx).reshape(ny, nx)\n\nRop = pylops.Roll(ny*nx, dims=(ny, nx), dir=1, shift=-2)\n\ny = Rop*x.ravel()\nxadj = Rop.H*y\n\ny = y.reshape(ny, nx)\nxadj = xadj.reshape(ny, nx)\n\nfig, axs = plt.subplots(1, 3, figsize=(10, 2))\nfig.suptitle('Roll for 2d data', fontsize=14, fontweight='bold', y=1.15)\naxs[0].imshow(x, cmap='rainbow', vmin=0, vmax=50)\naxs[0].set_title(r'$x$')\naxs[0].axis('tight')\naxs[1].imshow(y, cmap='rainbow', vmin=0, vmax=50)\naxs[1].set_title(r'$y = R x$')\naxs[1].axis('tight')\naxs[2].imshow(xadj, cmap='rainbow', vmin=0, vmax=50)\naxs[2].set_title(r'$x_{adj} = R^H y$')\naxs[2].axis('tight')"
      ]
    }
  ],
  "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.6.12"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}