{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [],
   "source": [
    "using ApproxFun, SingularIntegralEquations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$\n",
    "\\def\\E{{\\rm e}}\n",
    "\\def\\I{{\\rm i}}\n",
    "\\def\\D{{\\rm d}}\n",
    "$$\n",
    "\n",
    "## Master Solutions (2018)\n",
    "\n",
    "### Problem 1.2\n",
    "\n",
    "Following the mastery material, we know that we can re-cast the Cauchy transform in terms of a two-sheeted version of the Cauchy transform. In particular, define\n",
    "$$\n",
    "\\begin{align*}\n",
    "C^1 u(z) &= {C u(z) + i \\sqrt{-z} C[u/\\sqrt{\\diamond}](z) \\over 2} \\\\\n",
    "C^2 u(z) &= {C u(z) - i \\sqrt{-z} C[u/\\sqrt{\\diamond}](z) \\over 2} \n",
    "\\end{align*}\n",
    "$$\n",
    "This satisfies a 2-sheeted version of Plemelj, where the sheets are cut along $[0,\\infty)$. From sheet 1 to sheet 2 we have the right jump:\n",
    "$$ \n",
    "C_+^1 u(x) - C_-^2 u(x) = u(x)\n",
    "$$\n",
    "From sheet 2 back to sheet 1 we have continuity/analyticity:\n",
    "$$\n",
    "C_+^2 u(x) - C_-^1 u(x) = 0\n",
    "$$\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "true"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = Fun(0..100)\n",
    "u = exp(-x)\n",
    "C1(u,z) = (cauchy(u,z) + im*sqrt(-z) * cauchy(u/sqrt(x), z))/2\n",
    "C2(u,z) = (cauchy(u,z) - im*sqrt(-z) * cauchy(u/sqrt(x), z))/2\n",
    "\n",
    "C1(u,0.1+eps()*im) - C2(u,0.1-eps()*im)  ≈ u(0.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.3703261575747092e-14 + 0.0im"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C2(u,0.1+eps()*im) - C1(u,0.1-eps()*im)  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By summing over all inverses of the map $z \\mapsto z^{3/2}$ we can recover the Cauchy transform, with some care on which sheet we map to. In the upper-half plane, $z^{2/3}$ and $\\E^{4 \\pi \\I \\over 3} z^{2/3}$ are still on the first sheet as the phase has not crossed $2\\pi$, while the third inverse $\\E^{8 \\pi \\I \\over 3} z^{2/3}$ lives on the second sheet. Therefore, for $g(x) = f(x^3/2)$ we have\n",
    "$$\n",
    "C f(z) = C_1 g (z^{2/3}) +  C_1 g (\\E^{4 \\pi \\I \\over 3} z^{2/3})  + C_2 g(\\E^{8 \\pi \\I \\over 3} z^{2/3})\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "true"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = exp(-x)\n",
    "C_ex = 0.27264293583876564 - 0.20980563924551332im # \"exact\" Cf(0.1+0.1im) from Mathematica\n",
    "z = 0.1+0.1im\n",
    "C_ex ≈ C1(g, z^(2/3)) + C1(g, exp(4π*im/3)z^(2/3)) + C2(g, exp(8π*im/3)z^(2/3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As we pass to the lower half plane over the negative real axis, the second root passes to the second sheet. However, simealtaneously the roots swap plasses: for $x < 0$ we have for $\\lambda_1(z) := z^{2/3}$, $\\lambda_2(z) := \\E^{4 \\I \\pi/3} z^{2/3}$, $\\lambda_3(z) = \\E^{8 \\I \\pi /3} z^{2/3}$:\n",
    "$$\n",
    "\\begin{align*}\n",
    "\\lambda_1^+(x) = x_+^{2/3} &= \\E^{4 \\I \\pi/3} x_-^{2/3} = \\lambda_2^-(x) \\\\\n",
    " \\lambda_2^+(x) =\\E^{4 \\I \\pi/3}  x_+^{2/3} &= \\E^{8 \\I \\pi/3} x_-^{2/3} = \\lambda_3^-(x) \\\\\n",
    "\\lambda_3^+(x) = \\E^{8 \\I \\pi/3}  x_+^{2/3} &= \\ x_-^{2/3} = \\lambda_1^-(x)\n",
    "\\end{align*}\n",
    "$$\n",
    "\n",
    "and we therefore get:\n",
    "$$\n",
    "C f(z) = C_1 g (\\E^{4 \\I \\pi/3} z^{2/3}) +  C_2 g (\\E^{8 \\pi \\I \\over 3} z^{2/3})  + C_2 g( z^{2/3})\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "true"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C_ex_m = -0.27264293583876564 - 0.20980563924551332im # \"exact\" Cf(0.1-0.1im) from Mathematica\n",
    "z = 0.1-0.1im\n",
    "C_ex_m ≈ C1(g, exp(4π*im/3)z^(2/3)) + C2(g, exp(8π*im/3)*z^(2/3))  + C2(g, z^(2/3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In other words, in terms of $g$ and $h(x) = g(x)/\\sqrt(x)$ we have:\n",
    "$$\n",
    "C f(z) = {1 \\over 2} \\begin{cases} \n",
    "    C g (\\lambda_1(z)) + \\I \\sqrt{-\\lambda_1(z)} C h(\\lambda_1(z)) +      C g (\\lambda_2(z)) + \\I \\sqrt{-\\lambda_2(z)}  C h(\\lambda_2(z)) +      C g (\\lambda_3(z)) - \\I \\sqrt{-\\lambda_3(z))}  C h(\\lambda_3(z))) & \\Im z > 0 \\\\\n",
    "    C g (\\lambda_1(z)) - \\I \\sqrt{-\\lambda_1(z)} C h(\\lambda_1(z)) +      C g (\\lambda_2(z)) + \\I \\sqrt{-\\lambda_2(z)}  C h(\\lambda_2(z)) +      C g (\\lambda_3(z)) - \\I \\sqrt{-\\lambda_3(z))}  C h(\\lambda_3(z))) & \\Im z < 0 \\\\\n",
    "    \\end{cases}\n",
    "$$\n",
    "We can verify this satisfies Plemel.\n",
    "1. It decays at infinity because each of the Cauchy transforms decay\n",
    "2. It has weaker than pole singularities Because $C h(z)$ has at worse a $|z|^{-1/2}$ singularity, hence $\\sqrt{-z} C h(z^{2/3})$ has at worse a $|z|^{1/6}$ singularity\n",
    "3. It is analytic off $[0,\\infty)$. For $x < 0$ we have, noting that $\\lambda_2(x)^{2/3} = \\E^{4 \\I \\pi /3} * \\E^{2 \\I \\pi /3} |x|^{2/3} = |x|^{2/3}$ is right on the branch cut of $\\sqrt{-z}$:\n",
    "$$\n",
    "\\begin{align*}\n",
    "2C_+ f(x) &=     C g (\\lambda_1^+(x)) + \\I \\sqrt{-\\lambda_1^+(x)} C h(\\lambda_1^+(x)) +      C g (\\lambda_2^+(x)) + \\I \\sqrt{-\\lambda_2^+(x)}  C h(\\lambda_2^+(x)) +      C g (\\lambda_3^+(x)) - \\I \\sqrt{-\\lambda_3^+(x))}  C h(\\lambda_3^+(x))) \\\\\n",
    "&  =  C g (\\lambda_2^-(x)) + \\I \\sqrt{-\\lambda_2^-(x)} C h(\\lambda_2^-(x)) +      C g (\\lambda_3^-(x)) - \\I \\sqrt{-\\lambda_3^-(x)}  C h(\\lambda_3^-(x)) +      C g (\\lambda_1^-(x)) - \\I \\sqrt{-\\lambda_1^-(x))}  C h(\\lambda_1^-(x))) \\\\\n",
    "&= 2 C_- f(x)\n",
    "\\end{align*}\n",
    "$$\n",
    "4. It has the right jump on $[0,\\infty)$: for $x>0$ we have, noting only $\\lambda_1(x)$ is in $[0,\\infty)$ and other terms cancel:\n",
    "$$\n",
    "2C_+ f(x) - 2C_-f(x) = (C^+ - C^-) g(\\lambda_1(x)) + \\sqrt{x} C h(\\lambda_1(x)) = 2g(x^{2/3}) = 2f(x)\n",
    "$$\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 1.2\n",
    "\n",
    "We specialize the above for $f(x) = \\E^{-x^{2/3}}$ so that $g(x) = \\E^{-x}$ and $h(x) = \\E^{-x} / \\sqrt{x}$. From Problem Sheet 3, Problem 4.2 we have for $0 < \\alpha < 1$\n",
    "$$\n",
    "C[\\diamond^\\alpha \\E^{-\\diamond}](z) = {1 \\over \\Gamma(-\\alpha)} {(-z)^\\alpha \\E^{-z} \\Gamma(-\\alpha, - z) \\over\n",
    "  \\E^{-\\I\\pi\\alpha} - \\E^{\\I\\pi\\alpha}}\n",
    "$$\n",
    "Integrating by parts we can relate $\\Gamma(a,z)$ to $\\Gamma(a+1,z)$, that is:\n",
    "$$\n",
    "\\Gamma(a,z) = \\int_z^\\infty \\zeta^{a-1} \\E^{-\\zeta} \\D \\zeta = {1 \\over a} \\int_z^\\infty {\\D \\over \\D \\zeta}[\\zeta^{a}] \\E^{-\\zeta} \\D \\zeta = -{z^a \\E^{-z} \\over a} + {1 \\over a} \\int_z^ \\infty \\zeta^a \\E^{-\\zeta} \\D \\zeta = {\\Gamma(a+1,z) - z^a \\E^{-z} \\over a}\n",
    "$$\n",
    "This allows  us to deduce the above formula also works for $ \\alpha = -1/2$, in other words (using $\\Gamma(1/2) = \\sqrt{\\pi})$,\n",
    "$$\n",
    "C h(z) = C[\\diamond^{-1/2} \\E^{-\\diamond}](z) = {1 \\over \\sqrt{\\pi}} {(-z)^(-1/2) \\E^{-z} \\Gamma(1/2, - z) \\over 2 \\I}\n",
    "$$\n",
    "\n",
    "For $\\alpha = 0$, we use the alternative for $x < 0$\n",
    "$$\n",
    "\\Gamma(0,x_+) - \\Gamma(0,x_-) = -\\oint {\\E^{-\\zeta} \\over \\zeta} \\D \\zeta = -2 \\pi \\I\n",
    "$$\n",
    "implying that\n",
    "$$\n",
    "C g(z) = C[\\E^{-\\diamond}](z) = {\\E^{-z} \\Gamma(0,-z) \\over - 2 \\pi \\I}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "@webio": {
   "lastCommId": null,
   "lastKernelId": null
  },
  "kernelspec": {
   "display_name": "Julia 1.1.0",
   "language": "julia",
   "name": "julia-1.1"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.1.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}