Evaluating an Integral by Residue Theorem Its been awhile since I have taken complex analysis and I am wondering how to solve the following integral when $a>0, \ a=0,$ and $a<0$ for $$\int^{\infty}_0\frac{\cos ax+x\sin ax}{1+x^2}dx.$$
I know I can use the residue theorem thus  $$\int^{\infty}_0\left(\frac{\cos ax}{1+x^2}+\frac{x\sin ax}{1+x^2}\right)dx.$$ 
 A: First note that the integral may be written as
$$\frac12 \int_{-\infty}^{\infty} dx \frac{\cos{a x}}{1+x^2} - \frac12 \frac{d}{da} \int_{-\infty}^{\infty} dx \frac{\cos{a x}}{1+x^2}  $$
So consider the contour integral
$$\oint_{C(a)} dz \frac{e^{i a z}}{1+z^2} $$
where $C(a)$ is a semicircle of radius $R$ in the upper half plane when $a\gt 0$ and in the lower half plane when $a \lt 0$.  For example, when $a \gt 0$, the integral is
$$\int_{-R}^R dx \frac{e^{i a x}}{1+x^2} + i R \int_0^{\pi} d\phi \, e^{i \phi} \frac{e^{i a R \cos{\phi}} e^{-a R \sin{\phi}}}{1+R^2 e^{i 2 \phi}} $$
As $R \to \infty$, the magnitude of the second integral is bounded by
$$\frac{2 R}{R^2-1} \int_0^{\pi/2} d\phi \, e^{-2 a R \phi/\pi} = \frac{\pi}{a (R^2-1)} \left ( 1-e^{-a R} \right )$$
which clearly vanishes in this limit.  On the other hand, the contour integral is also equal to $i 2 \pi$ times the residue at the pole $z=i$, so that
$$\int_{-\infty}^{\infty} dx \frac{\cos{a x}}{1+x^2} = i 2 \pi \frac{e^{-a}}{2 i} = \pi \, e^{-a} \quad (a\gt 0)$$
(Here we used the symmetry of the integrand to get the original integral back.)  For $a \lt 0$, we close in the lower half plane (or simply use the evenness of the cosine) and find that
$$\int_{-\infty}^{\infty} dx \frac{\cos{a x}}{1+x^2} = \pi \, e^{-|a|} $$
We then take the derivative of this integral (separately for the cases $a \gt 0$ and $a \lt 0$) find that the original integral is
$$\int_0^{\infty} dx \frac{\cos{a x} + x \sin{a x}}{1+x^2} = \frac{\pi}{2} \left (1+\operatorname{sgn}{a} \right )e^{-|a|} = \pi \theta(a) e^{-|a|}$$
where $\theta$ is the Heaviside step function, which is equal to $1/2$ when $a=0$.
A: Alternatively, you could consider $ \displaystyle f(z) = \frac{e^{iaz}}{z-i}$.
Then for $a >0$ we can use Jordan's lemma to conclude $$\int_{-\infty}^{\infty} \frac{e^{iax}}{x-i} \ dx = 2 \pi i \ \text{Res}[f(z),i] = 2 \pi i e^{-a}. $$
But $$ \begin{align} \int_{-\infty}^{\infty} \frac{e^{iax}}{x-i} \ dx &= \int_{-\infty}^{\infty} \frac{\Big(\cos(ax) + i \sin(ax) \Big)(x+i)}{x^{2}+1} \ dx \\ &= \int_{-\infty}^{\infty} \frac{x \cos (ax) - \sin(ax) + i \Big(\cos(ax) + x \sin(ax) \Big)}{x^{2}+1} \ dx . \end{align}$$
And equating the imaginary parts on both sides of the equation we get $$\int_{0}^{\infty} \frac{\cos (ax) + x \sin(ax)}{x^{2}+1} \ dx =  \pi e^{-a}. $$
Now if $a<0$, $ \displaystyle \int f(z) \ dz$ will vanish along the lower half of the circle $|z|=R$ as $R \to \infty$.
And since $f(z)$ is analytic in the lower half-plane, the integral in question evaluates to zero.
A: First of all for $a=0$ you have
$$\int^{\infty}_{0}\Big(\frac{\cos(ax)}{1+x^2}+\frac{x\sin(ax)}{1+x^2}\Big)\,dx=\int^{\infty}_{0}\frac{1}{1+x^2}\,dx=\arctan(x)\Big|^{\infty}_0=\frac{\pi}{2}$$
In general for $a\neq 0$ we use
$$\cos(ax)=\frac{e^{iax}+e^{-iax}}{2}$$
and $$\sin(ax)=\frac{e^{iax}-e^{-iax}}{2i}$$
hence your integral can be rewritten as
$$\int^{\infty}_{0}\Big(\frac{\cos(ax)}{1+x^2}+\frac{x\sin(ax)}{1+x^2}\Big)\,dx=\int^{\infty}_{0}\Big(\frac{e^{iax}+e^{-iax}}{2(1+x^2)}+\frac{x(e^{iax}-e^{-iax})}{2i(1+x^2)}
\Big)\,dx$$
Substitute $z=ix$ to get
$$\int^{i\infty}_{0}\Big(\frac{e^{az}+e^{-az}}{2(1-z^2)}-\frac{z(e^{az}-e^{-az})}{2(1-z^2)}
\Big)\frac{dz}{i}=\frac{1}{4}\int^{i\infty}_{-i\infty}\Big(\frac{e^{az}+e^{-az}}{1-z^2}-\frac{z(e^{az}-e^{-az})}{1-z^2}
\Big)\frac{dz}{i}$$
Let 
$$\mathcal{I}=\frac{1}{4}\int^{i\infty}_{-i\infty}\Big(\frac{e^{az}+e^{-az}}{1-z^2}-\frac{z(e^{az}-e^{-az})}{1-z^2}
\Big)\frac{dz}{i}=\frac{1}{4}\int^{i\infty}_{-i\infty}\Big(\frac{(1-z)+(1+z)e^{-2az}}{1-z^2}\Big)\cdot e^{az}\frac{dz}{i}$$
The last integral can be partitioned into two integrals as follows
$$\mathcal{I}=\int^{i\infty}_{-i\infty}\frac{1}{4(1+z)}\cdot e^{az}\frac{dz}{i}+\int^{i\infty}_{-i\infty}\frac{1}{4(1-z)}\cdot e^{-az}\frac{dz}{i}$$
Substituting $z\equiv-z$ for the second integral yields
$$\mathcal{I}=\int^{i\infty}_{-i\infty}\frac{1}{4(1+z)}\cdot e^{az}\frac{dz}{i}-\int^{-i\infty}_{i\infty}\frac{1}{4(1+z)}\cdot e^{az}\frac{dz}{i}=\frac{1}{2i}\int^{i\infty}_{-i\infty}\frac{1}{1+z}\cdot e^{az}\,dz$$
Now we will do an analysis of how to evaluate the very last integral. Firstly consider
the following contour integral 
$$\mathcal{J}=\oint_{\gamma}\frac{e^{az}}{1+z}\,dz$$
where $\Re(a)>0$ and $\gamma$ is the contour which consists of the imaginary axis and half disk on the left-half complex plane. The orientation we choose to be counter-clockwise. We can now partition the contour integral into its component parts, one along the imaginary axis and the other along the half disk on the left-half plane keeping the chosen orientation, i.e.
$$\oint_{\gamma}\frac{e^{az}}{1+z}\,dz=\oint_{\Gamma_{R}}\frac{e^{az}}{1+z}\,dz+\int^{+iR}_{-iR}\frac{e^{az}}{1+z}\,dz$$
where the first part is along the half disk and the other along the imaginary axis with end points $-iR$ and $+iR$. The integral along the half disk can be estimated as follows by using the ML estimate i.e.
$$\Big|\oint_{\Gamma_{R}}\frac{e^{az}}{1+z}\,dz\Big|=\Big|\int^{3\pi/2}_{\pi/2}\frac{e^{aRe^{i\theta}}}{1+Re^{i\theta}}Rie^{i\theta}\,d\theta\Big|\leq\int^{3\pi/2}_{\pi/2}\frac{|e^{aRe^{i\theta}}|}{|1+Re^{i\theta}|}|Rie^{i\theta}||\,d\theta|\leq \frac{e^{-aR}}{R-1}R\cdot \pi\sim e^{-aR}\to 0$$
as $R\to\infty$. Thus in the limit the only contribution comes from the second integral i.e.
$$\lim_{R\to\infty}\oint_{\gamma}\frac{e^{az}}{1+z}\,dz=\lim_{R\to\infty}\Big(\oint_{\Gamma_{R}}\frac{e^{az}}{1+z}\,dz+\int^{+iR}_{-iR}\frac{e^{az}}{1+z}\,dz\Big)$$$$=\lim_{R\to\infty}\oint_{\Gamma_{R}}\frac{e^{az}}{1+z}\,dz+\lim_{R\to\infty}\int^{+iR}_{-iR}\frac{e^{az}}{1+z}\,dz=\lim_{R\to\infty}\int^{+iR}_{-iR}\frac{e^{az}}{1+z}\,dz$$
On the other hand the integrand of the contour integral is analytic on the domain defined by its contour except for a simple pole at $z=-1$ with residue $e^{-a}$. Appealing to Cauchy Residue Theorem we obtain
$$\mathcal{J}=\oint_{\gamma}\frac{e^{az}}{1+z}\,dz=2\pi i\cdot\text{Res}\{\frac{e^{az}}{1+z},-1\}=2\pi i e^{-a}$$
As $R\to\infty$ the analytic properties of the integrand don't change so the residue is the same. Hence 
$$\lim_{R\to\infty}\int^{+iR}_{-iR}\frac{e^{az}}{1+z}\,dz=2\pi i e^{-a}$$
(This is related to Laplace transform of $\frac{1}{1+z}$ with transform variable $a$ such that $\Re{(a)}>0$
$$\mathcal{L}^{-1}(\frac{1}{1+z})(a)=\lim_{T\to\infty}\frac{1}{2\pi i}\int^{\sigma+iT}_{\sigma-iT}\frac{1}{1+z}\cdot e^{az}\,{dz}=\frac{1}{2\pi i}\int^{\sigma+i\infty}_{\sigma-i\infty}\frac{1}{1+z}\cdot e^{az}\,{dz}=e^{-a}$$
with $\sigma=0$)
Having evaluated $\mathcal{J}$ we can now find $\mathcal{I}$ as 
$$\mathcal{I}=\frac{1}{2i}\mathcal{J}=\pi e^{-a}$$
For $\Re(a)<0$ we set $a=-b$ with $\Re(b)>0$ and $\mathcal{I}$ can be rewritten as
$$\mathcal{I}=\frac{1}{2i}\int^{i\infty}_{-i\infty}\frac{1}{1+z}\cdot e^{az}\,dz=\frac{1}{2i}\int^{i\infty}_{-i\infty}\frac{1}{1-z}\cdot e^{bz}\,dz$$ 
We have used in the last step the substitution $z\equiv-z$. Consider 
$$\mathcal{J}=\oint_{\gamma}\frac{e^{bz}}{1-z}\,dz$$
where $\gamma$ is the same contour as above. However the integrand is analytic everywhere on the domain defined by the contour so Cauchy Residue Theorem will tell us that 
$$\mathcal{J}=\oint_{\gamma}\frac{e^{bz}}{1-z}\,dz=0$$
As the radius of the half disk increases i.e. $R\to\infty$ the analytic properties of the integrand don't change so the contour integral evaluates to zero. But in the limit you can show by the same arguments as above (ML estimate) that the only contribution if there would be any would come from the integral on the imaginary axis i.e.
$$\lim_{R\to\infty}\oint_{\gamma}\frac{e^{bz}}{1-z}\,dz=\lim_{R\to\infty}\int^{+iR}_{-iR}\frac{e^{bz}}{1-z}\,dz$$
Together with the result from Cauchy we obtain
$$\mathcal{J}=\lim_{R\to\infty}\int^{+iR}_{-iR}\frac{e^{bz}}{1-z}\,dz=0$$
On the other hand 
$$\mathcal{I}=\frac{1}{2i}\mathcal{J}=0$$
So in conclusion we have 
$$
\mathcal{I} = \left\{
        \begin{array}{ll}
            0 & \quad \Re(a)<0 \\
            \frac{\pi}{2} & a=0\\
            \pi e^{-a} & \Re(a)>0
        \end{array}
    \right.
$$
