{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Flip along an axis\n\nThis example shows how to use the :py:class:`pylops.Flip`\noperator to simply flip an input signal along an axis.\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. Define an input signal composed of\n``nt`` samples\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nt = 10\nx = np.arange(nt)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We can now create our flip operator and apply it to the input\nsignal. We can also apply the adjoint to the flipped signal and we can\nsee how for this operator the adjoint is effectively equivalent to\nthe inverse.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "Fop = pylops.Flip(nt)\ny = Fop * x\nxadj = Fop.H * y\n\nplt.figure(figsize=(3, 5))\nplt.plot(x, \"k\", lw=3, label=r\"$x$\")\nplt.plot(y, \"r\", lw=3, label=r\"$y=Fx$\")\nplt.plot(xadj, \"--g\", lw=3, label=r\"$x_{adj} = F^H y$\")\nplt.title(\"Flip in 1st direction\", fontsize=14, fontweight=\"bold\")\nplt.legend()\nplt.tight_layout()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Let's now repeat the same exercise on a two dimensional signal. We will\nfirst flip the model along the first axis and then along the second axis\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nt, nx = 10, 5\nx = np.outer(np.arange(nt), np.ones(nx))\nFop = pylops.Flip((nt, nx), axis=0)\ny = Fop * x\nxadj = Fop.H * y\n\nfig, axs = plt.subplots(1, 3, figsize=(7, 3))\nfig.suptitle(\n    \"Flip in 1st direction for 2d data\", fontsize=14, fontweight=\"bold\", y=0.95\n)\naxs[0].imshow(x, cmap=\"rainbow\")\naxs[0].set_title(r\"$x$\")\naxs[0].axis(\"tight\")\naxs[1].imshow(y, cmap=\"rainbow\")\naxs[1].set_title(r\"$y = F x$\")\naxs[1].axis(\"tight\")\naxs[2].imshow(xadj, cmap=\"rainbow\")\naxs[2].set_title(r\"$x_{adj} = F^H y$\")\naxs[2].axis(\"tight\")\nplt.tight_layout()\nplt.subplots_adjust(top=0.8)\n\n\nx = np.outer(np.ones(nt), np.arange(nx))\nFop = pylops.Flip(dims=(nt, nx), axis=1)\ny = Fop * x\nxadj = Fop.H * y\n\n# sphinx_gallery_thumbnail_number = 3\nfig, axs = plt.subplots(1, 3, figsize=(7, 3))\nfig.suptitle(\n    \"Flip in 2nd direction for 2d data\", fontsize=14, fontweight=\"bold\", y=0.95\n)\naxs[0].imshow(x, cmap=\"rainbow\")\naxs[0].set_title(r\"$x$\")\naxs[0].axis(\"tight\")\naxs[1].imshow(y, cmap=\"rainbow\")\naxs[1].set_title(r\"$y = F x$\")\naxs[1].axis(\"tight\")\naxs[2].imshow(xadj, cmap=\"rainbow\")\naxs[2].set_title(r\"$x_{adj} = F^H y$\")\naxs[2].axis(\"tight\")\nplt.tight_layout()\nplt.subplots_adjust(top=0.8)"
      ]
    }
  ],
  "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
}