{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Transpose\nThis example shows how to use the :py:class:`pylops.Transpose`\noperator. For arrays that are 2-dimensional in nature this operator\nsimply transposes rows and columns. For multi-dimensional arrays, this\noperator can be used to permute dimensions\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\")\nnp.random.seed(0)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Let's start by creating a 2-dimensional array\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "dims = (20, 40)\nx = np.arange(800).reshape(dims)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We use now the :py:class:`pylops.Transpose` operator to swap the two\ndimensions. As you will see the adjoint of this operator brings the data\nback to its original model, or in other words the adjoint operator is equal\nin this case to the inverse operator.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "Top = pylops.Transpose(dims=dims, axes=(1, 0))\n\ny = Top * x\nxadj = Top.H * y\n\nfig, axs = plt.subplots(1, 3, figsize=(10, 4))\nfig.suptitle(\"Transpose for 2d data\", fontsize=14, fontweight=\"bold\", y=1.15)\naxs[0].imshow(x, cmap=\"rainbow\", vmin=0, vmax=800)\naxs[0].set_title(r\"$x$\")\naxs[0].axis(\"tight\")\naxs[1].imshow(y, cmap=\"rainbow\", vmin=0, vmax=800)\naxs[1].set_title(r\"$y = F x$\")\naxs[1].axis(\"tight\")\naxs[2].imshow(xadj, cmap=\"rainbow\", vmin=0, vmax=800)\naxs[2].set_title(r\"$x_{adj} = F^H y$\")\naxs[2].axis(\"tight\")\nplt.tight_layout()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "A similar approach can of course be taken two swap multiple axes of\nmulti-dimensional arrays for any number of dimensions.\n\n"
      ]
    }
  ],
  "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
}