---
jupytext:
  formats: ipynb,md:myst,py:light
  text_representation:
    extension: .md
    format_name: myst
    format_version: 0.13
    jupytext_version: 1.13.8
kernelspec:
  display_name: Python 3
  language: python
  name: python3
---

```{code-cell}
:tags: [hide-cell]

import mmf_setup; mmf_setup.nbinit()
import os
from pathlib import Path
FIG_DIR = Path(mmf_setup.ROOT) / '../Docs/_build/figures/'
os.makedirs(FIG_DIR, exist_ok=True)
import logging; logging.getLogger("matplotlib").setLevel(logging.CRITICAL)
%matplotlib inline
import numpy as np, matplotlib.pyplot as plt
try: from myst_nb import glue
except: glue = None
```

(sec:GreensFunctions)=
# 10. Green's Functions

The coverage of Green's functions in {cite}`Arfken:2013` is quite reasonable.  Here are
some highlights presented from a different perspective, analogous to that in
{cite}`Hassani:2013`.  I highly recommend comparing these two approaches -- learning to
translate from vectors and matrices to differential equations is an extremely valuable skill.

The general idea is to find particular solutions to a linear differential equation.
Recall that this is a functional equivalent of solving a linear system:
\begin{gather*}
  \mathcal{L} y(x) = f(x), \qquad
  \mat{L}\ket{y} = \ket{f}.
\end{gather*}
Green's functions are the functional equivalent of inverting $\mat{L}$:
\begin{gather*}
  \mathcal{L}G(x, y) = \delta(x-y), \qquad \mat{L}\mat{G} = \mat{1}\\
  y(x) = \int G(x, y)f(y)\d{y}, \qquad \ket{f} = \mat{G}\ket{f}.
\end{gather*}
:::::{admonition} $G(x,y) = G(x-y)$ as a [Toeplitz matrix][].
:class: dropdown
Green's functions are often presented in the form
\begin{gather*}
  \mathcal{L}G(x) = \delta(x)
\end{gather*}
which obscures the matrix nature of $G$.  This can be done when $\mathcal{L}$ is
independent of $x$ -- i.e., translationally invariant.  In this case
\begin{gather*}
  G(x, y) = G(x-y).
\end{gather*}
This corresponds to a [Toeplitz matrix][] or diagonal-constant matrix $\mat{G}$ where
the entries depend only on their distance from the diagonal.  I.e. the diagonal has one
constant value, the next diagonal has another constant value etc.
\begin{gather*}
  [\mat{G}]_{ab} = g(a - b).
\end{gather*}
[Toeplitz matrices][Toeplitz matrix] have many nice properties, including the fact that
they are diagonalized by plane waves
\begin{gather*}
  \braket{m|f_n} = e^{2\pi I m n/N}.
\end{gather*}
This is why Fourier techniques are so useful for finding Green's functions.
:::{doit} Show that $\mat{G}\ket{f_m} = \ket{f_m}\lambda_m$ if $\mat{G}$ is a [Toeplitz matrix][].
:class: dropdown
*(Incomplete)*
:::
While this often simplifies things, it is best to keep in mind the nature of Green's
functions as matrices, not as vectors.
:::::

```{code-cell}
:tags: [margin, hide-input]
beta = 0.4
w0 = 0.5
ws = wp, wm = 1j*beta + np.sqrt(w0**2 - beta**2)*np.array([1, -1])
eps = 0.01
fig, ax = plt.subplots()
ax.spines[:].set_visible(False)

r = 0.05
th = 0.2
z0 = -1+0.01j
z1 = wm.real+0.01j + r*np.exp(1j*(-th - np.pi/2)).real
z2 = wm + r*np.exp(1j*(-th - np.pi/2))
z23 = wm + r*np.exp(1j*(-np.linspace(th, 2*np.pi - th)-np.pi/2))
z3 = wm + r*np.exp(1j*(+th - np.pi/2))
z4 = wm.real+0.01j + r*np.exp(1j*(+th - np.pi/2)).real
z5 = wp.real+0.01j + r*np.exp(1j*(-th - np.pi/2)).real
z6 = wp + r*np.exp(1j*(-th - np.pi/2))
z67 = wp + r*np.exp(1j*(-np.linspace(th, 2*np.pi - th)-np.pi/2))
z7 = wp + r*np.exp(1j*(+th - np.pi/2))
z8 = wp.real+0.01j + r*np.exp(1j*(+th - np.pi/2)).real
z9 = 1.0+0.01j

zs = np.concatenate([[z0, z1, z2], z23, [z3, z4, z5, z6], z67, [z7, z8, z9]])
ax.plot(ws.real, ws.imag, 'xk')
ax.plot(zs.real, zs.imag,'-C0')
ax.axhline([0], ls='-', lw=0.2, c='k')
ax.axvline([0], ls='-', lw=0.2, c='k')
ax.set(xticks=[], yticks=[], xlim=(-1,1), ylim=(-0.1, 0.6), aspect=1);
```
:::{margin}
Contour used to obtain the acausal (advanced) Green's function for a damped harmonic
oscillator.
:::
:::::{doit} Damped Harmonic Oscillator $\mathcal{L}=\partial_{tt}+2\beta\partial_{t}+\omega_0^2$.

Find the general Green's function for the damped harmonic oscillator.  Recall that
$\mathcal{L}$ is independent of time (invariant under shifts of time), so we can write
$G(t, \tau) = G(t-\tau)$, thus find the general homogeneous solutions $h_1(t)$ and
$h_2(t)$ such that
\begin{gather*}
  G(t) = \begin{cases}
    h_{1}(t), & t < 0\\
    h_{2}(t), & t > 0
  \end{cases}, \qquad
  \mathcal{L}G(t) = \delta(t).
\end{gather*}
Note that there are many such solutions since $G(t) + h(t)$ is also a valid Green's
function.  Find the causal Green's function where $G_{+}(t < 0) = 0$.

:::{solution}
Here we solve the slightly simpler case without damping where $\beta = 0$.  The
homogeneous solutions are simply $e^{\pm \I \omega_0 t}$ so we have
\begin{gather*}
  G(t) = \begin{cases}
    a_{+}e^{\I \omega_0 t} + a_{-}e^{-\I \omega_0 t}, & t < 0,\\
    b_{+}e^{\I \omega_0 t} + b_{-}e^{-\I \omega_0 t}, & t > 0.
  \end{cases}
\end{gather*}
$G(t)$ must be continuous at $t=0$, so $a_+ + a_- = b_+ + b_-$.  Likewise, the
derivatives must be discontinuous with a unit step so that we obtain $\delta(t)$:
\begin{gather*}
    \I\omega_0(b_{+} - b_{-}) - \I\omega_0(a_{+} - a_{-}) = 1.
\end{gather*}
We can express these conditions as $\braket{D_0|A} = 0$ and $\braket{D_1|A} = 1$
in terms of the following vectors
\begin{gather*}
  \ket{D_0} = \begin{pmatrix}
    +1 \\ +1 \\ -1 \\ -1\\
  \end{pmatrix}, \qquad
  \ket{D_1} = -\I\omega_0 \begin{pmatrix}
    -1 \\ +1 \\ +1 \\ -1\\
  \end{pmatrix}, \qquad
  \ket{A} = 
  \begin{pmatrix}
    a_+\\
    a_-\\
    b_+\\
    b_-\\
  \end{pmatrix}.
\end{gather*}
Noting that
\begin{gather*}
  \braket{D_0|D_0} = 4, \qquad
  \braket{D_1|D_1} = 4\omega^2, \qquad
  \braket{D_0|D_1} = 0,
\end{gather*}
we can write the solution as
\begin{gather*}
  \ket{A} = \frac{\ket{D_1}}{\braket{D_1|D_1}} + \ket{B}
\end{gather*}
where $\ket{B}$ is any vector orthogonal to the subspace $\{\ket{D}_0, \ket{D}_1\}$.
This subspace is easily constructed by inspection, but could be found using the
Gram-Schmidt procedure (QR decomposition):
\begin{gather*}
  \ket{A} = \frac{1}{4\I\omega_0}
  \begin{pmatrix}
    -1 \\ +1 \\ +1 \\ -1
  \end{pmatrix}
  +
  \alpha
  \begin{pmatrix}
    1 \\ 1 \\ 1 \\ 1
  \end{pmatrix}
  +
  \beta
  \begin{pmatrix}
    +1 \\ -1 \\ +1 \\ -1
  \end{pmatrix}.
\end{gather*}
To find the causal Green's function, we want $a_+ = a_- = 0$.  This requires $b_+ = -b_-
= b$ and then $2\I \omega_0 b = 1$
\begin{gather*}
  G_{+}(t) = \Theta(t)\frac{e^{\I\omega_0 t} - e^{-\I\omega_0 t}}{2\I\omega_0}
           = \Theta(t)\frac{\sin(\omega_0 t)}{\omega_0}.
\end{gather*}

The full solution can be found most simply using Fourier techniques and contour
integrals as follows.  Express
\begin{gather*}
  G(t) = \mathcal{F}^{-1}(\tilde{G}_\omega) 
       = \int \frac{\d{\omega}}{2\pi} e^{\I \omega t} \tilde{G}_{\omega}, \qquad
  \delta(t) = \mathcal{F}^{-1}(1) = \int \frac{\d{\omega}}{2\pi} e^{\I \omega t}.
\end{gather*}
Then, taking the Fourier transform of $\mathcal{L}G = \delta$ gives
\begin{gather*}
  \mathcal{F}(\mathcal{L}G) = (-\omega^2 + 2\I \beta \omega +
  \omega_0^2)\tilde{G}_\omega = 1, \\
  \tilde{G}_{\omega} = \frac{-1}{(\omega - \omega_{-})(\omega - \omega_{+})}, \qquad
  \omega_{\pm} = \I \beta \pm \sqrt{\omega_0^2 - \beta^2}.
\end{gather*}
The Green's function can be computed by a contour integral
\begin{gather*}
  G(t) = \mathcal{F}^{-1}(\tilde{G}_\omega) = 
  \int_{-\infty}^{\infty}\frac{\d{\omega}}{2\pi}
  \frac{-e^{\I \omega t}}{(\omega - \omega_{-})(\omega - \omega_{+})}
  =
  \oint_{C}\frac{\d{\omega}}{2\pi \I}
  \frac{-\I e^{\I \omega t}}{(\omega - \omega_{-})(\omega - \omega_{+})}
\end{gather*}
where the contour is closed along a circular arc going to infinity.  To ensure that the
additional contributions vanish (justifying the last equality), we must close the
contour up if $t > 0$ and down if $t<0$ to ensure that the exponential factor decays
(see Jordan's lemma).

If $0 < \beta$, then both poles like in the upper half plane, so the integral is zero if
$t<0$ and the sum of both residues if $t>0$:
\begin{gather*}
  G(t) = \Theta(t)\left(
    \frac{-\I e^{\I \omega_- t}}{\omega_- - \omega_+} +
    \frac{-\I e^{\I \omega_+ t}}{\omega_+ - \omega_-}\right)
    = \Theta(t) \frac{e^{\I \omega_+ t} - e^{\I \omega_- t}}{\I(\omega_+ - \omega_-)}\\
    = \Theta(t) \frac{e^{-\beta t}\sin(\bar{\omega}t)}{\bar\omega}, \qquad
    \bar{\omega} = \sqrt{\omega_0^2 - \beta^2},
\end{gather*}
which approaches our previous solution as $\beta \rightarrow 0$.

If we strictly have $\beta = 0$, then the two poles like on the real axis.  In this
case, we must choose how we go around them.  There are four choices here: going below
both gives the causal (retarded) Green's function as above.  Going over both gives the
acausal (advanced) Green's function that is zero for $t>0$.  Going over one and under
the gives a Green's function that is non-zero for all times.  These mixed forms are
often better suited for perturbation theory and play a key role in computing Feynman
diagrams.

Does the previous calculation mean that there is only one Green's function if $\beta >
0$?  No: there are always others we can find by suitably deforming the contour as shown
on the right.
:::
:::::

Although formally $\mat{G} = \mat{L}^{-1}$, the formalism presented this way works even
if $\mat{L}$ is singular.  In particular, we are generally faced with a problem where
$\mathcal{L} h(x) = 0$ has homogeneous solutions.

## Self-Adjoint Operators
We now explain {cite}`Arfken:2013` Eq. (10.18) (recast slightly):
\begin{gather*}
  G(x, y) = A\begin{cases}
    h_{1}(x)h_{2}(y), &  x < y,\\
    h_{2}(x)h_{1}(y), &  x > y,\\
  \end{cases} \tag{10.18}
\end{gather*}
for a second-order self-adjoint operator $\mathcal{L}$ where $h_1(x)$ is the homogeneous
solution satisfying the left boundary condition and $h_2(x)$ is a homogeneous solution
satisfying the right boundary condition.  The argument is simply that for $x \neq y$,
the functional dependence on $x$ must be a homogeneous solution satisfying the boundary
conditions, and $\mat{G} = \mat{G}^\dagger$ hence $G(x, y) = G(y, x)^*$.  *(The form here
assumes that $h_{i}$ are real.)*

The constant $A$ is found by ensuring that the discontinuity at $x=y$ gives the
delta-function.  Note that in your solution for the causal Green's function $G_+(t)$ fo
the Harmonic oscillator above, one cannot express $G(t, \tau) = G_{+}(t-\tau) =
\Theta(t-\tau)\sin\bigl(\omega_0(t-\tau)\bigr)/\omega_0$ in this form: this lack of
symmetry is because the operator $\mathcal{L}$ is not self-adjoint with the specified
boundary conditions.

:::::{admonition} Details
:class: dropdown
This follows from the following arguments, made in terms of linear algebra but tagged
with the corresponding equation in {cite}`Arfken:2013`.  We start with the definition of
the Green's function:
\begin{gather*}
  \mat{L}\mat{G} = \mat{1}. \tag{10.6}
\end{gather*}
Since $\mat{L}$ is Hermitian, it has a complete set of orthonormal eigenstates
\begin{gather*}
  \mat{L}\ket{n} = \ket{n}\lambda_n, \qquad
  \braket{m|n} = \delta_{mn}. \tag{10.10}
\end{gather*}
This means we can express both $\mat{G}$ and $\mat{1}$ in the basis
\begin{gather*}
  \mat{G} = \sum_{nm}\ket{n}g_{nm}\bra{m}. \tag{10.11}\\
  \mat{1} = \sum_{m}\ket{m}\!\bra{m}. \tag{10.12}
\end{gather*}
Inserting these into (10.6) we have
\begin{gather*}
  \sum_{nm}\mat{L}\ket{n}g_{nm}\bra{m} = \sum_{nm}\ket{n}\lambda_n g_{nm}\bra{m}
  = \sum_{m}\ket{m}\!\bra{m}. \tag{10.13}
\end{gather*}
Compute the matrix elements of this expression $\braket{n|\cdots|m}$ (rename the inner
dummy indices while you do this) to find *(no summation convention)*
\begin{gather*}
  \lambda_n g_{nm} = \delta_{nm}, \qquad  g_{nm} = \frac{\delta_{nm}}{\lambda_n},\\
  \mat{G} = \sum_{n}\ket{n}\frac{1}{\lambda_n}\bra{n}. \tag{10.14}\\
  \mat{G} = \mat{G}^\dagger. \tag{10.15}
\end{gather*}
Thus, we see that $\mat{G}$ is also Hermitian.
:::::



[Toeplitz matrix]: <https://en.wikipedia.org/wiki/Toeplitz_matrix>
[Fuch's theorem]: <https://en.wikipedia.org/wiki/Fuchs's_theorem>
[method of undetermined coefficients]: <https://en.wikipedia.org/wiki/Method_of_undetermined_coefficients>
[delta function]: <https://en.wikipedia.org/wiki/Dirac_delta_function>
[Riemann-Stieltjes Integral]: <https://en.wikipedia.org/wiki/Riemann%E2%80%93Stieltjes_integral>
[Heaviside step function]: <https://en.wikipedia.org/wiki/Heaviside_step_function>
[distribution]: <https://en.wikipedia.org/wiki/Distribution_(mathematics)>
[variation of parameters]: <https://en.wikipedia.org/wiki/Variation_of_parameters>
[Wronskian]: <https://en.wikipedia.org/wiki/Wronskian>
[osculating]: <https://en.wikipedia.org/wiki/Osculating_curve>
[integrating factor]: <https://en.wikipedia.org/wiki/Integrating_factor>
[envelope]: <https://en.wikipedia.org/wiki/Envelope_(mathematics)>
