\documentclass[nojss]{jss}

\usepackage{dsfont}
\usepackage{bbm}
\usepackage{amsfonts}
\usepackage{wasysym}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% declarations for jss.cls %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%% just as usual
\author{Robin K. S. Hankin}
\title{The residue theorem from a numerical perspective}
%\VignetteIndexEntry{The residue theorem from a numerical perspective}
%% for pretty printing and a nice hypersummary also set:
%% \Plainauthor{Achim Zeileis, Second Author} %% comma-separated
\Plaintitle{The residue theorem from a numerical perspective}

%% an abstract and keywords
\Abstract{A short vignette illustrating Cauchy's integral theorem
  using numerical integration}
\Keywords{Residue theorem, Cauchy formula, Cauchy's integral formula,
  contour integration, complex integration, Cauchy's theorem}


\Address{
  Robin K. S. Hankin\\
  Auckland University of Technology\\
  2-14 Wakefield Street\\
  Auckland\\
  New Zealand\\
  E-mail: \email{hankin.robin@gmail.com}
}


%% need no \usepackage{Sweave.sty}
\begin{document}


<<requirepackage,echo=FALSE,print=FALSE>>=
require(elliptic,quietly=TRUE)
@ 


In this very short vignette, I will use contour integration to evaluate
\begin{equation}
  \int_{x=-\infty}^{\infty}\frac{e^{ix}}{1+x^2}\,dx
  \end{equation}
using numerical methods.  This document is part of the \pkg{elliptic}
package~\citep{hankin2006}. 

If $f$ is meromorphic, the residue theorem tells us that the integral
of~$f$ along any closed nonintersecting path, traversed anticlockwise,
is equal to~$2\pi i$ times the sum of the residues inside it.


\begin{figure}[htbp]
  \begin{center}
    \includegraphics{semicircular_path.pdf}
    \caption{Contour\label{semicircular_path} integration path
      from~$(-R,0)$ to~$(R,0)$ along the real axis, followed by a
      semicircular return path in the positive imaginary half-plane.
      Poles of~$e^{ix}/\left(1+x+2\right)$ symbolised by explosions}
  \end{center}
\end{figure}

To evaluate the integral above, we define
$f(z)=\frac{e^{iz}}{1+z^2}$. Then we take a semicircular path~$P$ from
$-R$ to $+R$ along the real axis, then following a semicircle in the
upper half plane, of radius $R$ to close the loop
(figure~\ref{semicircular_path}).  Now we make~$R$ large.  Then~$P$
encloses a pole at~$i$ [there is one at $-i$ also, but this is outside
  $P$, so irrelevent here] at which the residue is~$-i/2e$.  Thus

\begin{equation}
  \oint_P f(z)\,dz=2\pi i\cdot(-i/2e) = \pi/e
\end{equation}
  
along~$P$; the contribution from the semicircle tends to zero
as~$R\longrightarrow\infty$; thus the integral along the real axis is
the whole path integral, or~$\pi/e$.

We can now reproduce this result analytically.  First, choose $R$:

<<chooseR>>=
R <- 400
@ 


And now define a path~$P$.  First, the semicircle:

<<definesemi>>=
u1     <- function(x){R*exp(pi*1i*x)}
u1dash <- function(x){R*pi*1i*exp(pi*1i*x)}
@ 

and now the straight part along the real axis:

<<straightpart>>=
u2     <- function(x){R*(2*x-1)}
u2dash <- function(x){R*2}
@ 

And  define the function:

<<>>=
f <- function(z){exp(1i*z)/(1+z^2)}
@ 

Now carry out the path integral.  I'll do it explicitly, but note that
the contribution from the first integral should be small:


<<ansapp>>=
answer.approximate <-
    integrate.contour(f,u1,u1dash) +
    integrate.contour(f,u2,u2dash) 
@ 


And compare with the analytical value:

<<compareans>>=
answer.exact <- pi/exp(1)
abs(answer.approximate - answer.exact)
@ 

Now try the same thing but integrating over a triangle instead of a
semicircle, using {\tt integrate.segments()}.  Use a path $P'$ with
base from $-R$ to $+R$ along the real axis, closed by two straight
segments, one from $+R$ to $iR$, the other from $iR$ to $-R$:

 
<<>>=
abs(integrate.segments(f,c(-R,R,1i*R))- answer.exact)
@  
 

Observe how much better one can do by integrating over a big square
instead:


<<useabigsquare>>=
abs(integrate.segments(f,c(-R,R,R+1i*R, -R+1i*R))- answer.exact)
@ 


\subsection*{The residue theorem for function evaluation}

If $f(\cdot)$ is holomorphic within~$C$, Cauchy's
residue theorem states that
\begin{equation}
  \oint_C\frac{f(z)}{z-z_0} = f(z_0).
  \end{equation}

Function \code{residue()} is a wrapper that takes a function~$f(z)$
and integrates~$f(z)/\left(z-z_0\right)$ around a closed loop which
encloses~$z_0$.  We can test this numerically by evaluating $\sin(1)$:

<<residuetest>>=
f <- function(z){sin(z)}
numerical <- residue(f,z0=1,r=1)
exact <- sin(1)
abs(numerical-exact)
@ 

which is unreasonably accurate, IMO.



\bibliography{elliptic}
\end{document}
