Using Cauchy's Integral Theorem to evaluate integral? I'm going through Stein's Complex Analysis, and I'm a bit confused at one of the classical examples of using Cauchy's theorem to evaluate an integral. The example is:
$$\int_0^{\infty}\frac{1-\cos{x}}{x^2}dx = \frac{\pi}{2}$$
The book says (and I'll add my thoughts & questions in bold as they come up):
Here we consider the function $f(z) = (1 - e^{iz})/z^2$, and we integrate over the indented semicircle in the upper half-plane positioned on the $x$-axis, as shown in the figure below:

Why precisely do we consider the function $f(z) = (1-e^{iz})/z^2$? I get that $e^{ix} = \cos{x} + i\sin{x}$ and $\cos{z} = (e^{iz}-e^{iz})/2$, but how precisely do we get $f(z) = (1 - e^{iz})/z^2$ from this?
Why precisely are we integrating over the indented semicircle in the upper half-plane positioned on the $x$-axis? As a follow-up, are we creating a hole around 0 because the integral cannot be evaluated at x = 0?
[back to book]
If we denote $\gamma_{\epsilon}^+$ and $\gamma_R^+$ the semicircles of radii $\epsilon$ and $R$ with negative and positive orientations respectively, Cauchy's theorem gives:
$$\int_{-R}^{-\epsilon}\frac{1-e^{ix}}{x^2}dx + \int_{\gamma_{\epsilon}^+}\frac{1-e^{iz}}{z^2}dz + \int_{\epsilon}^R\frac{1-e^{ix}}{x^2}dx + \int_{\gamma_R^+}\frac{1-e^{iz}}{z^2}dz = 0$$
First we let $R \rightarrow \infty$ and observe that:
$$\mid\frac{1-e^{iz}}{z^2}\mid \ \leq \ \frac{2}{\mid z\mid^2}$$
so the integral over $\gamma_R^+$ goes to 0. Therefore:
$$\int_{\mid x \mid \geq \epsilon}\frac{1-e^{ix}}{x^2}dx = -\int_{\gamma_{\epsilon}^+}\frac{1-e^{iz}}{z^2}dz$$
Why does letting $R \rightarrow \infty$ lead to the above inequality? What does $R \rightarrow \infty$ mean in the scope of the diagram, and why does the integral over $\gamma_R^+$ go to zero? Not really sure how the last equality is formulated as well...
[back to book]
Next, note that:
$$f(z) = \frac{iz}{z^2} + E(z)$$
where $E(z)$ is bounded as $z \rightarrow 0$, while on $\gamma_{\epsilon}^+$ we have $z = \epsilon e^{i\theta}$ and $dz = i\epsilon e^{i \theta}d\theta$. Thus, 
$$\int_{\gamma_{\epsilon}^+}\frac{1 - e^{iz}}{z^2}dz \rightarrow \int_{\pi}{0}(-ii)d\theta = -\pi$$ as $\epsilon \rightarrow 0$. Taking real parts then yields:
$$\int_{-\infty}^{\infty}\frac{1-\cos{x}}{x^2}dx = \pi$$
Since the integrand is even, the desired formula is proved.
I don't quite follow the first part of this, but I think more importantly, how does solving all of this help us solve the original integral? What exactly am I taking the "real parts" of?
Sorry, I know it's a lot, but any sort of walkthrough would be appreciated.
 A: I wrote this post to make all of the minutia clear.  The big picture is more important, though.  See Mike's answer for that.
The general idea is this: we can define a holomorphic function whose real part looks just like the desired integrand on the real line, and we have some strong tools for integrating holomorphic functions, so maybe we can use information about this holomorphic function's integral to deduce information about our original integral.
In this case, we see that the real part of $f(z)=\frac{{1}-e^{iz}}{z^2}$ is precisely $\frac{1-\cos{x}}{x^2}$ if $z=x$ is real.  That's why we consider $f(z)$.  Its real part looks just like the function that we want to integrate.  So we note that $$\int_{-\infty}^{\infty} \frac{{1}-e^{iz}}{z^2} \,dz = \int_{-\infty}^\infty \frac{1-(\cos{x}+i\sin{x})}{x^2} \,dx = \int_{-\infty}^{\infty} \frac{1-\cos{x}}{x^2} \,dx + i \int_{-\infty}^{\infty} \frac{-\sin{x}}{x^2} \,dx.$$  So, in particular, we have: $$\Re (\int_{-\infty}^{\infty} f(z) \,dz) = \int_{-\infty}^{\infty} \Re(f(z)) \,dz = \int_{-\infty}^{\infty} \frac{1-\cos{x}}{x^2} \,dx =2\int_{0}^{\infty} \frac{1-\cos{x}}{x^2} \,dx .$$  This means that if we can just figure out what $\int_{-\infty}^{\infty} f(z)$ is, then we can just take its real part, and that must be twice the integral you want to evaluate.
To figure out what $\int_{-\infty}^{\infty} f(z)$ is, we'd like to use the strength of Cauchy's theorem.  Cauchy's theorem says that if we integrate over the given igloo, we've got to get zero, no matter what $R$ and $\epsilon$ are.  We choose the igloo because we have to avoid the "problem" point at 0, where $f$ isn't holomorphic, and because we need a closed loop for Cauchy.  We'll recover the integral on the real line from $-\infty$ to $\infty$ as we let $R$ get huge and $\epsilon$ get small.
As you note, from Cauchy's theorem, we get: $$\int_{-R}^{-\epsilon}\frac{1-e^{ix}}{x^2}dx + \int_{\gamma_{\epsilon}^+}\frac{1-e^{iz}}{z^2}dz + \int_{\epsilon}^R\frac{1-e^{ix}}{x^2}dx + \int_{\gamma_R^+}\frac{1-e^{iz}}{z^2}dz = 0.$$  Rearranging, this means that $$\int_{-R}^{-\epsilon}\frac{1-e^{ix}}{x^2}dx  + \int_{\epsilon}^R\frac{1-e^{ix}}{x^2}dx = - \int_{\gamma_{\epsilon}^+}\frac{1-e^{iz}}{z^2}dz - \int_{\gamma_R^+}\frac{1-e^{iz}}{z^2}dz. \quad\quad (*)$$ Now, as we let $R$ and $\epsilon$ do their thing, the left side of this equation is going to tend to $\int_{-\infty}^{\infty} f(z) dz$.  So we just need to figure out what the right side is doing.  To do this, we'll consider what happens as we vary our diagram, taking $R$ to be really big first and then taking $\epsilon$ to be small.  Since the above equalities didn't depend on our choices of $R$ and $\epsilon$, varying the diagram with big $R$ or small $\epsilon$ won't cause any problems.
As $R$ runs off to infinity, we have: $$\left|\int_{\gamma_R^+}\frac{1-e^{iz}}{z^2}dz\right| \le \int_{\gamma_R^+} \left|\frac{1-e^{iz}}{z^2}\right||dz| \le \int_{\gamma_R^+} \frac{2}{R^2} |dz| = \pi R \frac{2}{R^2} = \frac{2\pi}{R} \rightarrow 0,$$ so the $\int_{\gamma_R^+}\frac{1-e^{iz}}{z^2}$ term on the right hand side of $(*)$ goes to zero in the limit.  So, letting $R$ go to infinity in $(*)$, we get $$\int_{-\infty}^{-\epsilon}\frac{1-e^{ix}}{x^2}dx  + \int_{\epsilon}^\infty\frac{1-e^{ix}}{x^2}dx = - \int_{\gamma_{\epsilon}^+}\frac{1-e^{iz}}{z^2}dz, \quad\quad\quad (**)$$ which is just a rephrasing of the equality you asked about.
And now we just need to know what happens to the right side of this equation as $\epsilon$ gets small.
For this, we'll see what happens if we just take the power series representation for the $e^{iz}$ term in $f$ about zero.  We have:
$$f(z) = \frac{1-e^{iz}}{z^2} = \frac{1 - \sum_{n=0}^\infty\frac{(iz)^n}{n!}}{z^2} = -\frac{\sum_{n=1}^\infty \frac{(iz)^n}{n!}}{z^2} = -\frac{iz}{z^2} +\sum_{n=2}^\infty \frac{(iz)^{n-2}}{n!}.$$
Let $E(z) = \sum_{n=2}^\infty \frac{(iz)^{n-2}}{n!}$ (the second term on the right hand side above).  Notice that if $\epsilon<1$, then on $\gamma_{\epsilon^+}$, we have $$|E(z)| \le \sum_{n=2}^\infty \frac{|iz|^{n-2}}{n!} \le \sum_{n=2}^\infty \frac{1}{n!} \le e,$$ so $E(z)$ stays bounded as $\epsilon$ shrinks, and we see that if $\epsilon<1$, $$\left| \int_{\gamma_{\epsilon^+}} E(z) dz\right|  \le \int_{\gamma_{\epsilon^+}} |E(z)| |dz| \le \int_{\gamma_{\epsilon^+}} e |dz| = \pi \epsilon e \rightarrow 0 \text{ as }\epsilon \rightarrow 0.$$
Thus, \begin{align*}\lim_{\epsilon \rightarrow 0^+} \int_{\gamma_{\epsilon^+}} f(z) dz &= \lim_{\epsilon \rightarrow 0^+} \int_{\gamma_{\epsilon^+}} -\frac{iz}{z^2} dz + \lim_{\epsilon \rightarrow 0^+} \int_{\gamma_{\epsilon^+}} E(z) dz\\ &= \lim_{\epsilon \rightarrow 0^+} \int_{\gamma_{\epsilon^+}} - \frac{i}{z} dz + 0\\ &= \lim_{\epsilon \rightarrow 0^+} -i \int_\pi^0 i dz\\ &= -\pi.\end{align*}
Combining with $(**)$ and letting $\epsilon$ go to zero, we finally have:  $$\int_{-\infty}^\infty f(z) dz = \pi.$$  Taking the real parts of both sides gives $$\int_{-\infty}^\infty \Re f(z) dz = 2 \int_0^\infty \frac{1-\cos{x}}{x^2} dx = \Re\pi = \pi.$$
So $\int_0^\infty \frac{1-\cos{x}}{x^2} dx = \frac {\pi}{2}$.
A: I'll try to answer your questions:


*

*It is common to use such functions in such problems. The reason for that is that the real of this function gives you excatly the integrand, and more importantly - e^iz is bounded. which is crucial. Morever, if you try to use (1-cosz)/z^2 instead, you won't be able to prove that it's integral on the upper circle reaches zero.

*The reason that the integral over the upper circle reaches zero is that, by the ML evaluation, this integral's absolut value is bounded by pi*R/R^2=pi/R which tends to zero.

*All this proccess helps to solve the problem, since we break it to a sum of several problems, which are easier to solve - you prove that most of the integrals are zero or tend to zero, so you are left with one integral over the lower circle, which can be evaluated by the remainder theorem.
The thing in such problems is, to find a suitable path (this one is an Igloo, there are others such as half a circle, pizza and so on), that helps you to break your problem to several sub-problems, that can be solved in complex-analysis matters.
