{
  "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"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\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()\nplt.tight_layout()"
      ]
    },
    {
      "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(dims=(ny, nx), axis=1, shift=-2)\n\ny = Rop * x\nxadj = Rop.H * y\n\nfig, axs = plt.subplots(1, 3, figsize=(10, 4))\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\")\nplt.tight_layout()"
      ]
    }
  ],
  "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.9.15"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}