{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7c1065dd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import pyerrors as pe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "20f67709",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.style.use('./base_style.mplstyle')\n",
    "plt.rc('text', usetex=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5764fd0",
   "metadata": {},
   "source": [
    "We can load data from preprocessed pickle files which contain a list of `pyerror` `Obs`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c49ff771",
   "metadata": {},
   "outputs": [],
   "source": [
    "correlator_data = pe.load_object('./data/correlator_test.p')  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae93c7c2",
   "metadata": {},
   "source": [
    "With this list a `Corr` object can be initialised"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "33a8fdec",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_correlator = pe.correlators.Corr(correlator_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5f954607",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x0/a\tCorr(x0/a)\n",
      "------------------\n",
      "8\t548(13)\n",
      "9\t433(11)\n",
      "10\t343.1(8.6)\n",
      "11\t273.2(6.6)\n",
      "12\t217.5(5.6)\n",
      "13\t172.9(4.9)\n",
      "14\t137.6(4.6)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "my_correlator.print([8, 14])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b00d670b",
   "metadata": {},
   "source": [
    "The `show` method can display the correlator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b71529d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 10496 characters]\n",
      "text/plain": [
       "<Figure size 640x395.55 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "my_correlator.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c659557e",
   "metadata": {},
   "source": [
    "## Manipulating correlators"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "416cf39a",
   "metadata": {},
   "source": [
    "`Corr` objects can be shifted"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e8d65dd5",
   "metadata": {},
   "outputs": [],
   "source": [
    "shifted_correlator = my_correlator.roll(20)\n",
    "shifted_correlator.tag = r'Correlator shifted by $x_0/a=20$'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "634dd613",
   "metadata": {},
   "source": [
    "Or symmetrised"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "127a661d",
   "metadata": {},
   "outputs": [],
   "source": [
    "symmetrised_correlator = my_correlator.symmetric()\n",
    "symmetrised_correlator.tag = 'Symmetrised correlator'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d733872",
   "metadata": {},
   "source": [
    "And we can compare different `Corr` objects by passing `comp` to the `show` method"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8e264aed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 34140 characters]\n",
      "text/plain": [
       "<Figure size 640x395.55 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "shifted_correlator.show(comp=symmetrised_correlator, logscale=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "232e88af",
   "metadata": {},
   "source": [
    "## Effective mass"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "83dc751c",
   "metadata": {},
   "source": [
    "The effective mass of the correlator can be obtained by calling the `m_eff` method"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c686f7e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "m_eff = symmetrised_correlator.m_eff()\n",
    "m_eff.tag = 'Effective mass'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a9d13b2",
   "metadata": {},
   "source": [
    "We can also use the priodicity of the lattice in order to obtain the cosh effective mass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5acde8cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "periodic_m_eff = symmetrised_correlator.m_eff('periodic')\n",
    "periodic_m_eff.tag = 'Cosh effective mass'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c658b000",
   "metadata": {},
   "source": [
    "We can compare the two and see how the standard effective mass deviates form the plateau at the center of the lattice"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "1d6ea22a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 28376 characters]\n",
      "text/plain": [
       "<Figure size 640x395.55 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "periodic_m_eff.show([4,47], comp=m_eff, ylabel=r'$am_\\mathrm{eff}$')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3762e68",
   "metadata": {},
   "source": [
    "Arithmetic operations and mathematical functions are also overloaded for the `Corr` class. We can compute the difference between the two variants of the effective mass as follows."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e56d164c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 15772 characters]\n",
      "text/plain": [
       "<Figure size 640x395.55 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "difference_m_eff = np.abs(periodic_m_eff - m_eff)\n",
    "difference_m_eff.show([0, 47], logscale=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "472ab97b",
   "metadata": {},
   "source": [
    "## Derivatives"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d99414fe",
   "metadata": {},
   "source": [
    "We can obtain derivatives of correlators in the following way"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "03007f8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "first_derivative = symmetrised_correlator.deriv()\n",
    "first_derivative.tag = 'First derivative'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "c0311739",
   "metadata": {},
   "outputs": [],
   "source": [
    "second_derivative = symmetrised_correlator.second_deriv()\n",
    "second_derivative.tag = 'Second derivative'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "165550d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 24856 characters]\n",
      "text/plain": [
       "<Figure size 640x395.55 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "symmetrised_correlator.show([5, 20], comp=[first_derivative, second_derivative], y_range=[-500, 1300])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff177781",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
