Question on contour integrals. $\int_C \frac{1}{1+z^2} dz$ without using ML estimation! NOTE: I went though every every previous post on contour integration (here and on youtube) looking for a resolution to this question, so please do NOT mark this question as already answered.
Let $C$ be the contour $z=|r|>1$ be the upper half of a circle or radius r centered at the origin, $(Im(z)\geq 0)$, so not closed), oriented counterclockwise and suppose you want to integrate $$\int_C \frac{1}{1+z^2} dz = \int_0^\pi \frac{i r e^{ i \theta}}{1+ r^2 e^{2 i \theta}}d\theta.$$
where the integral on the righthand side of the equality has the parametrization $z=r e^{ i \theta}, dz =i r e^{ i \theta}d\theta$.
To be clear, I want show this contour integral goes to zero directly as r goes to infinity without using ML estimation or any estimation theorem. Now according to ML, $|\int_C \frac{1}{1+z^2} dz|\leq  \pi r/(r^2-1)$ (more details someone else wrote below) which goes to $0$ as $r\rightarrow \infty$, which you will find in the solution of every other related problem.  In summary, using ML, $\int_C \frac{1}{1+z^2} dz \rightarrow 0$ as $r\rightarrow \infty$.
But according to Mathematica:
Integrate[ r I Exp[I x]/(1 + (r Exp[I x])^2), {x, 0, Pi}] = $ \int_0^\pi \frac{i r e^{ i \theta}}{1+ r^2 e^{2 i \theta}}d\theta= -2 \arctan(r)$.
This does not go to zero as $r\rightarrow \infty$, but instead $-2 \arctan(r)\rightarrow -\pi$.
I also calculated it to be $-\pi$  below in a way that avoids expressions using the complex version of $Arctan(\cdot)$ by doing partial fraction decomposition.
$$\int_C \frac{1}{1+z^2}dz = \frac{i}{2}\int_C \frac{dz}{z+i}-\frac{i}{2}\int_C \frac{dz}{z-i}=\frac{i}{2} \int_0^\pi \frac{r i e^{i\theta}}{r e^{i\theta}+i}d\theta -\frac{i}{2} \int_0^\pi \frac{r i e^{i\theta}}{r e^{i\theta}-i}d\theta $$
$$= \frac{i}{2} \int_{r+i}^{-r+i} \frac{du}{u} -\frac{i}{2} \int_{r-i}^{-r-i} \frac{du}{u}.$$
From here, we can see that we can use the principal branch $\log(z)$ for each individual integral where $-\pi<Arg(z)<\pi$.
So write $r+i = R e^{i \eta}$ where  $0<\eta < \pi/2$.  Then $r-i = R e^{- i \eta}, -r-i = R e^{i (-\pi+\eta)},  -r+i = R e^{i (\pi-\eta)}$ give the angles corresponding to the principal branch
This gives $$\int_C \frac{1}{1+z^2}dz=\frac{i}{2} (\log(-r+i)-\log(r+i) - (\log(-r-i)-\log(r-i))) \\= \frac{i}{2}(i(  (\pi-\eta -\eta)- (-\pi+\eta-(-\eta) ))) =  2\eta - \pi.$$
Then as $r\rightarrow \infty$,  $\eta\rightarrow 0$, giving the answer $-\pi$, which agrees with -2 arctan(r) that wolfram gives which also goes to $-\pi$ as $r\rightarrow \infty$.
Can anyone spot where I am going wrong?
 A: I think the problem is hidden in parametrizing the integrals and then coming back and doing complex variable substitutions to go back to contour integrals.  That seems kind of dodgy ($\theta$ is a real variable, but $u$ is complex).  Instead I'd suggest thinking about the branch points of the original integrand and integrals obtained from the partial fraction decomposition and the contour deformations involved to get from your starting point to end point.  For the branch points of $\int_C \frac{1}{1+z^2}dz$,  we have $i$ and $-i$.  If we make the line segment between them a branch cut we should avoid problems down the road.  For $\frac{i}{2}\int_C \frac{dz}{z+i}$ we should think about deforming the upper half circle to the line segment from $r$ to $-r$.  Immediately we have a problem with the branch cut stopping us.  Even if we say what the hey $i$ isn't a branch point of $\frac{i}{2}\int_C \frac{dz}{z+i}$ and go ahead, we cannot shift the contour on $\frac{i}{2}\int_C \frac{dz}{z-i}$ to $r$ to $-r$ without passing through the branch point of the integrand at $i$.  If you think about it, that's what your substitutions have done.  You need a more careful analysis of the behavior of the integrals involving the branch points which is why you have an answer off by something like $\frac{i}{2}\cdot 2\pi i$
A: The mistake was here.
From here, we can see that we can use the principal branch $\log(z)$ for each individual integral where $-\pi<Arg(z)<\pi$.
Explanation of what went wrong
$\int_C \frac{1}{1+z^2}dz = \frac{i}{2}\int_C \frac{dz}{z+i}-\frac{i}{2}\int_C \frac{dz}{z-i}=\frac{i}{2} \int_0^\pi \frac{r i e^{i\theta}}{r e^{i\theta}+i}d\theta -\frac{i}{2} \int_0^\pi \frac{r i e^{i\theta}}{r e^{i\theta}-i}d\theta$
$= \frac{i}{2} \int_{r+i}^{-r+i} \frac{du}{u} -\frac{i}{2} \int_{r-i}^{-r-i} \frac{du}{u}.$
For the first integral $\frac{i}{2} \int_0^\pi \frac{r i e^{i\theta}}{r e^{i\theta}+i}d\theta = \frac{i}{2} \int_{r+i}^{-r+i} \frac{du}{u}$
Note that with $u= r e^{i\theta}+i$ gives $u$ range of values along the circle $|u|= \sqrt{r^2+1}$ from $\theta=0, u= r+i$ to $\theta=\pi, u= -r+i$  We still need to figure out if we are traversing counterclockwise (contour stays above the real axis) or clockwise. We can determine this by checking a value of $\theta$ between 0 and $\pi$, and $\pi/2$ is the obvious choice. Notice that when $\theta=\pi/2, u= 2i$, so we can see we are taking the counterclockwise contour, which doesn't go through the principal branch of log(u) since it entirely above the real axis. So using the principal branch Log() is fine here!
Now, let's now look at the other integral, where the mistake was made
$\frac{i}{2} \int_0^\pi \frac{r i e^{i\theta}}{r e^{i\theta}-i}d\theta=\frac{i}{2} \int_{r-i}^{-r-i} \frac{du}{u}$.
This time the starting point and ending point are slightly below the real axis. Using the logic in the previous paragraph, when $\theta=\pi/2, u= r e^{i \theta/2}-i= ri-i = (r-1)i$.  From here we see that for $r>1$, we are STILL taking the counter clockwise contour, meaning we have to cross the negative real axis, which means taking the principal branch Log(u) won't work!
However, if r<1, then taking the Principal branch is just fine as the contour is then the clockwise one that doesn't cross the negative real axis!  In other words, the wrong computation I originally had above would holds for r<1, as wolfram alpha predicted, but wolfram mathematica did not!  What we can see here choosing the principal branch in both situation gives a contour (with $r<1$) that can be deformed to  $\int_{\infty}^{-\infty} \frac{dx}{1+x^2} = -\pi$ by essentially flattening out this small circle to the real line while simultaneously stretching it out along the real axis.
Corrected Solution
*In both contour integrals above, we can take the antiderivative to be $log(u)$ with the branch cut $-\pi/2 \leq Arg(u)\leq 3\pi/2$ along the neative imaginary axis. Note: we don't have to use the same branch cut for both contour integrals, it is merely more convenient to do so.
So with $R=\sqrt{r^2+1}$, write $r+i = R e^{i \eta}$ where  $0<\eta < \pi/2$.  Then $r-i = R e^{- i \eta}, -r-i = R e^{i (\pi+\eta)},  -r+i = R e^{i (\pi-\eta)}$ give the angles corresponding to the principal branch
This gives $$\int_C \frac{1}{1+z^2}dz=\frac{i}{2} (\log(-r+i)-\log(r+i) - (\log(-r-i)-\log(r-i))) \\= \frac{i}{2}(i(  (\pi-\eta -\eta)- (\pi+\eta-(-\eta) ))) =  2\eta $$
Then as $r\rightarrow \infty$,  $\eta\rightarrow 0$, giving the answer $0$
A: The issue is that you treat $i$ as a real number in your calculation for the integral. What you should do is write the real and imaginary parts of your integrands:
$$\int_0^\pi\frac{ire^{i\theta}}{1+r^2e^{i2\theta}}d\theta=\int_0^\pi\frac{ire^{i\theta}}{1+r^2e^{i2\theta}}\frac{1+r^2e^{-i2\theta}}{1+r^2e^{-i2\theta}}d\theta=\int_0^\pi\frac{ire^{i\theta}+ir^3e^{-i\theta}}{1+2r^2\cos(2\theta)+r^4}d\theta$$
A: \begin{alignat*}{1}
\left|\int_C \frac{1}{1+z^2} dz \right| & = \left| \int_0^\pi \frac{i r e^{ i \theta}}{1+ r^2 e^{2 i \theta}}d\theta \right| \\
  & \le \int_0^\pi \left| \frac{ire^{ i \theta}}{1 + r^2 e^{2 i \theta}} \right| d\theta \\
  & = \int_0^\pi  \frac{\left| ire^{ i \theta} \right|}{\left| 1 + r^2 e^{2 i \theta}\right|}  d\theta \\
  & \le \int_0^\pi  \frac r{r^2 - 1}  d\theta  \quad \textrm{ since } \ \left| 1 + r^2 e^{2 i \theta}\right| \ge \left|r^2 e^{2 i \theta}\right|-1 = r^2 - 1 \\
  & = \frac{\pi r}{r^2 - 1} \to 0 \ \textrm{ as } \ r\to\infty. 
\end{alignat*}
We assumed that $r>1$.
