Integral $\int_0^\infty \frac{x^{2k}}{x^2+1}dx$ I am little bit confused by the following integral:
$$\int_0^\infty \frac{x^{2k}}{x^2+1}dx,$$
which according to WA is equal to
$$\int_0^\infty \frac{x^{2k}}{x^2+1}dx=\frac{\pi}{2}\sec(\pi k),\quad \text{for}\ \operatorname{Re}(k)>-\frac{1}{2}.$$
However, by plugging $k=1,2,...$, to RHS, this should be equal to $\frac{\pi}{2}(-1)^k$.
On the other side, plugging $k=1,2,...$ to LHS, these integrals should not exist. Since I could not derive that result, I would like to know what is going on here? Maybe WA evaluates is wrongly? Thanks for any hint.
 A: Here is my approach by applying complex analysis by considering the function: $\displaystyle f(z) = \frac{z^{2k}}{z^2+1} $ with the contour $C$ which consists of a semicircle has the origin as its center, and the real line from $[ - R, R]$. We choose the branch $(0, \pi)$ for our branch cut.
$\hspace{5.5cm}$
I will briefly sketch the computation as below. Since $\displaystyle-\frac{1}{2}<\Re(k)<\frac{1}{2}$, one has the integral of the small circle go toward $0$. Moreover, we have the limit for the upper bound of the integral of the big circle:
$$\lim_{R\to \infty} \frac{  R^{2k+1}}{R^2+1}= \lim_{R\to \infty} \frac{  (2k+1)R^{2k}}{2R}= 0$$
This infers that the integral of the big circle will go to 0 as $R \to \infty$. Hence, we are left with:
$$ \int_{C} f(z)dz =\left(\int_{green}+\int_{red}\right) f(z)dz=\left (1-e^{4\pi i k}\right)\int_{0}^{\infty} \frac{x^{2k}}{x^2+1}\mathrm{d}x$$
And by Residue theorem:
$$ \int_{C} f(z)dz  = 2\pi i \operatorname{Res}_{z=\pm i} f(z) = 2\pi i \cdot\left( \frac{e^{k\pi i}}{2i}-\frac{e^{-k\pi i}}{2i}\right)=\pi \cdot\left(e^{k\pi i}-e^{-k\pi i}\right)$$
Lastly, we obtain:
$$\int_{0}^{\infty} \frac{x^{2k}}{x^2+1}\mathrm{d}x = \frac{\pi}{\left(e^{k\pi i}+e^{-k \pi i}\right)} = \frac{\pi}{2\cos(k\pi)}$$
A: In Mathematica (version 12.1),
Integrate[x^(2 k)/(x^2 + 1), {x, 0, Infinity}]

generates the output
ConditionalExpression[1/2 \[Pi] Sec[k \[Pi]], -(1/2) < Re[k] < 1/2]

which is
$$\frac{1}{2} \pi  \sec (\pi  k)\text{ if }-\frac{1}{2}<\Re(k)<\frac{1}{2}.$$
This is clearly an issue with Wolfram Alpha's evaluation.
A: For $0<\Re(s)<1$, we have
$$
\int_0^\infty{t^{s-1}\over t+1}\mathrm dt={\pi\over\sin(\pi s)}\tag1
$$
When we substitute $t=x^2$ in the OP's integral, we have
$$
\int_0^\infty{x^{2k}\over x^2+1}\mathrm dx=\frac12\int_0^\infty{t^{k-1/2}\over t+1}\mathrm dt=\frac12\int_0^\infty{t^{(k+1/2)-1}\over t+1}\mathrm dt
$$
Plugging $s=k+1/2$ into (1), we have
$$
\int_0^\infty{x^{2k}\over x^2+1}\mathrm dx={\pi\over2\cos(k\pi)}
$$
for $-1/2<\Re(k)<1/2$. RHS of (1) is oftentimes used to prove $\Gamma(s)\Gamma(1-s)=\pi\csc(\pi s)$, so it is possible that WA uses Gamma function in evaluating (1).
A: Letting $\displaystyle \frac{1}{t}=x^{m}+1$, then $
\displaystyle d x=\frac{1}{m} \left(\frac{1}{t}-1\right)^{\frac{1}{m}-1}\left(-\frac{1}{t^{2}}\right) d t.
$
Consequently
$$
\begin{aligned}
\int_{0}^{\infty} \frac{x^{r}}{x^{m}+1} d x &=-\int_{1}^{0} \frac{\left(\frac{1}{t}-1\right)^{\frac{r}{m}}}{\frac{1}{t}} \frac{1}{m t^{2}}\left(\frac{1}{t}-1\right)^{\frac{1}{m}-1} d t \\
&=\frac{1}{m} \int_{0}^{1} \frac{(1-t)^{\frac{r}{m}}(1-t)^{\frac{1}{m}-1}}{t^{\frac{r+1}{m}}} d t \\
&=\frac{1}{m} \int_{0}^{1} t^{-\frac{r+1}{m}}(1-t)^{\frac{r+1}{m}-1} d t \\
&=\frac{1}{m} B\left(1-\frac{r+1}{m}, \frac{r+1}{m}\right)
\end{aligned}
$$
By the property of Beta function,
$$
B(x, y)=\frac{\Gamma(x) \Gamma(y)}{\Gamma(x+y)},
$$
where $\operatorname{Re}(x)>0$ and $\operatorname{Re}(y)>0,$
we have
$$
\int_{0}^{\infty} \frac{x^{r}}{x^{m}+1} d x=\frac{\Gamma\left(1-\frac{r+1}{m}\right) \Gamma\left(\frac{r+1}{m}\right)}{m\Gamma(1)}
$$
Using the Euler’s Reflection Theorem,
$$
\Gamma(1-z) \Gamma(z)=\pi \csc (\pi z),
$$
where $z\notin Z$,
we can now conclude that
$$\boxed{\int_{0}^{\infty} \frac{x^{r}}{x^{m}+1} d x=\frac{\pi}{m} \csc \frac{(r+1) \pi}{m}}$$
where $m>r+1>0$.
In particular, when $r=2k$ and $m=2$,
$$\int_{0}^{\infty} \frac{x^{2k}}{x^{2}+1} d x=\frac{\pi}{2} \csc \frac{(2k+1) \pi}{2}=\frac{\pi}{2} \sec (k\pi)$$
However, the property of Beta function,
$$
B(x, y)=\frac{\Gamma(x) \Gamma(y)}{\Gamma(x+y)},
$$
holds for $\operatorname{Re}(x)>0$ and $\operatorname{Re}(y)>0,$
if and only if $1-\frac{2 k+1}{2}>0 \Leftrightarrow k<\frac{1}{2}$.
Therefore for any $k<\frac{1}{2}$,
$$\int_{0}^{\infty} \frac{x^{2k}}{x^{2}+1} d x=\frac{\pi}{2} \csc \frac{(2k+1) \pi}{2}=\frac{\pi}{2} \sec (k\pi) \tag*{(*)}  $$
but doesn’t hold for all $k\geq \frac{1}{2}, $ which explains why we can’t put $k=1,2,3, \cdots$ into $(*)$.
