{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Symmetrize\n\nThis example shows how to use the :py:class:`pylops.Symmetrize`\noperator which takes an input signal and returns a symmetric signal\nby pre-pending the input signal in reversed order. Such an operation can be\ninverted as we will see in this example.\n\nMoreover the :py:class:`pylops.Symmetrize` can be used as *preconditioning*\nto any inverse problem where we are after inverting for a signal that we\nwant to ensure is symmetric. Refer to `sphx_glr_gallery_plot_wavest.py`\nfor an example of such a type.\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": [
        "Sop = pylops.Symmetrize(nt)\ny = Sop * x\nxadj = Sop.H * y\nxinv = Sop / y\n\nplt.figure(figsize=(7, 3))\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.plot(xinv, \"--m\", lw=3, label=r\"$x_{inv} = F^{-1} y$\")\nplt.title(\"Symmetrize 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, 6\nx = np.outer(np.arange(nt), np.ones(nx))\n\nSop = pylops.Symmetrize((nt, nx), axis=0)\ny = Sop * x\nxadj = Sop.H * y\nxinv = Sop / y.ravel()\nxinv = xinv.reshape(Sop.dims)\n\nfig, axs = plt.subplots(1, 3, figsize=(7, 3))\nfig.suptitle(\n    \"Symmetrize in 2nd direction for 2d data\", fontsize=14, fontweight=\"bold\", y=0.95\n)\naxs[0].imshow(x, cmap=\"rainbow\", vmin=0, vmax=9)\naxs[0].set_title(r\"$x$\")\naxs[0].axis(\"tight\")\naxs[1].imshow(y, cmap=\"rainbow\", vmin=0, vmax=9)\naxs[1].set_title(r\"$y=Fx$\")\naxs[1].axis(\"tight\")\naxs[2].imshow(xinv, cmap=\"rainbow\", vmin=0, vmax=9)\naxs[2].set_title(r\"$x_{adj}=F^{-1}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))\nSop = pylops.Symmetrize((nt, nx), axis=1)\n\ny = Sop * x\nxadj = Sop.H * y\nxinv = Sop / y.ravel()\nxinv = xinv.reshape(Sop.dims)\n\n# sphinx_gallery_thumbnail_number = 3\nfig, axs = plt.subplots(1, 3, figsize=(7, 3))\nfig.suptitle(\n    \"Symmetrize in 2nd direction for 2d data\", fontsize=14, fontweight=\"bold\", y=0.95\n)\naxs[0].imshow(x, cmap=\"rainbow\", vmin=0, vmax=9)\naxs[0].set_title(r\"$x$\")\naxs[0].axis(\"tight\")\naxs[1].imshow(y, cmap=\"rainbow\", vmin=0, vmax=9)\naxs[1].set_title(r\"$y=Fx$\")\naxs[1].axis(\"tight\")\naxs[2].imshow(xinv, cmap=\"rainbow\", vmin=0, vmax=9)\naxs[2].set_title(r\"$x_{adj}=F^{-1}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
}