{
  "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\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import torch\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.gridspec as pltgs\n\nimport pylops_gpu\n\nplt.close('all')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Let's define an identity operator $\\mathbf{I}$ with same number of elements for data\n$N$ and model $M$.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "N, M = 5, 5\nx = torch.arange(M, dtype=torch.int)\nIop = pylops_gpu.Identity(M, dtype=torch.int)\n\ny = Iop * x\nxadj = Iop.H * y\n\ngs = pltgs.GridSpec(1, 6)\nfig = plt.figure(figsize=(7, 3))\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(0.35, 0.5, '=', horizontalalignment='center',\n        verticalalignment='center', size=40, fontweight='bold')\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)"
      ]
    },
    {
      "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 = torch.arange(M, dtype=torch.int)\nIop = pylops_gpu.Identity(N, M, dtype=torch.int)\n\ny = Iop*x\nxadj = Iop.H*y\n\nprint('x = %s ' % x)\nprint('I*x = %s ' % y)\nprint('I\\'*y = %s ' % 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 = torch.arange(M, dtype=torch.int)\nIop = pylops_gpu.Identity(N, M, dtype=torch.int)\n\ny = Iop * x\nxadj = Iop.H * y\n\nprint('x = %s ' % x)\nprint('I*x = %s ' % y)\nprint('I\\'*y = %s ' % xadj)"
      ]
    }
  ],
  "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.6.12"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}