{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Zero\n\nThis example shows how to use the :py:class:`pylops.basicoperators.Zero` operator.\nThis operators simply zeroes the data in forward mode and the model in adjoint mode.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.gridspec as pltgs\n\nimport pylops\n\nplt.close('all')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Let's define an zero operator $\\mathbf{0}$ 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 = np.arange(M)\nZop = pylops.basicoperators.Zero(M, dtype='int')\n\ny = Zop*x\nxadj = Zop.H*y\n\ngs = pltgs.GridSpec(1, 6)\nfig = plt.figure(figsize=(7, 3))\nax = plt.subplot(gs[0, 0:3])\nax.imshow(np.zeros((N, N)), cmap='rainbow', vmin=-M, vmax=M)\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])\nim = ax.imshow(x[:, np.newaxis], cmap='rainbow', vmin=-M, vmax=M)\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', vmin=-M, vmax=M)\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], 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 = np.arange(M)\nZop = pylops.Zero(N, M, dtype='int')\n\ny = Zop*x\nxadj = Zop.H*y\n\nprint('x = %s' % x)\nprint('0*x = %s' % y)\nprint('0\\'*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 = np.arange(M)\nZop = pylops.Zero(N, M, dtype='int')\n\ny = Zop*x\nxadj = Zop.H*y\n\nprint('x = %s' % x)\nprint('0*x = %s' % y)\nprint('0\\'*y = %s' % xadj)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Note that this operator can be useful in many real-life applications when for\nexample we want to manipulate a subset of the model array and keep intact the\nrest of the array. For example:\n\n   .. math::\n      \\begin{bmatrix}\n              \\mathbf{A} \\quad  \\mathbf{0}\n              \\end{bmatrix}\n      \\begin{bmatrix}\n              \\mathbf{x_1}  \\\\\n              \\mathbf{x_2}\n      \\end{bmatrix} = \\mathbf{A} \\mathbf{x_1}\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.6.12"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}