{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Identity\nThis example shows how to use the :py:class:`pylops.Identity` operator to transfer model\ninto data and viceversa.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.gridspec as pltgs\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nimport pylops\n\nplt.close(\"all\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Let's define an identity operator $\\mathbf{Iop}$ with same number of\nelements for data and model ($N=M$).\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "N, M = 5, 5\nx = np.arange(M)\nIop = pylops.Identity(M, dtype=\"int\")\n\ny = Iop * x\nxadj = Iop.H * y\n\ngs = pltgs.GridSpec(1, 6)\nfig = plt.figure(figsize=(7, 4))\nax = plt.subplot(gs[0, 0:3])\nim = ax.imshow(np.eye(N), cmap=\"rainbow\")\nax.set_title(\"A\", size=20, fontweight=\"bold\")\nax.set_xticks(np.arange(N - 1) + 0.5)\nax.set_yticks(np.arange(M - 1) + 0.5)\nax.grid(linewidth=3, color=\"white\")\nax.xaxis.set_ticklabels([])\nax.yaxis.set_ticklabels([])\nax = plt.subplot(gs[0, 3])\nax.imshow(x[:, np.newaxis], cmap=\"rainbow\")\nax.set_title(\"x\", size=20, fontweight=\"bold\")\nax.set_xticks([])\nax.set_yticks(np.arange(M - 1) + 0.5)\nax.grid(linewidth=3, color=\"white\")\nax.xaxis.set_ticklabels([])\nax.yaxis.set_ticklabels([])\nax = plt.subplot(gs[0, 4])\nax.text(\n    0.35,\n    0.5,\n    \"=\",\n    horizontalalignment=\"center\",\n    verticalalignment=\"center\",\n    size=40,\n    fontweight=\"bold\",\n)\nax.axis(\"off\")\nax = plt.subplot(gs[0, 5])\nax.imshow(y[:, np.newaxis], cmap=\"rainbow\")\nax.set_title(\"y\", size=20, fontweight=\"bold\")\nax.set_xticks([])\nax.set_yticks(np.arange(N - 1) + 0.5)\nax.grid(linewidth=3, color=\"white\")\nax.xaxis.set_ticklabels([])\nax.yaxis.set_ticklabels([])\nfig.colorbar(im, ax=ax, ticks=[0, 1], pad=0.3, shrink=0.7)\nplt.tight_layout()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Similarly we can consider the case with data bigger than model\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "N, M = 10, 5\nx = np.arange(M)\nIop = pylops.Identity(N, M, dtype=\"int\")\n\ny = Iop * x\nxadj = Iop.H * y\n\nprint(f\"x = {x} \")\nprint(f\"I*x = {y} \")\nprint(f\"I'*y = {xadj} \")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "and model bigger than data\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "N, M = 5, 10\nx = np.arange(M)\nIop = pylops.Identity(N, M, dtype=\"int\")\n\ny = Iop * x\nxadj = Iop.H * y\n\nprint(f\"x = {x} \")\nprint(f\"I*x = {y} \")\nprint(f\"I'*y = {xadj} \")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Note that this operator can be useful in many real-life applications when for example\nwe want to manipulate a subset of the model array and keep intact the rest of the array.\nFor example:\n\n   .. math::\n      \\begin{bmatrix}\n              \\mathbf{A} \\quad  \\mathbf{I}\n              \\end{bmatrix}\n      \\begin{bmatrix}\n              \\mathbf{x_1}  \\\\\n              \\mathbf{x_2}\n      \\end{bmatrix} = \\mathbf{A} \\mathbf{x_1} + \\mathbf{x_2}\n\nRefer to the tutorial on *Optimization* for more details on this.\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
}