# $\int \frac{\tan^2 x}{1+x^2} dx$

Motivated by this integral, which does not evaluate in Mathematica, I'm wondering if there is some trick or substitution that will solve the indefinite integral.

My motivation is to find integrals that Mathematica cannot solve "directly," and learn from this community tricks, insights, and alternate approaches in which the problem can be solved. Yes... this often involves some mathematical (rather than coding) insight, but there too, sometimes the mathematical insight leads to a coding insight, for instance some clever coded variable substitution, or casting in some transform space, or...

Integrate[Tan[x]^2/(1+x^2),x]

• maple gives partial result !Mathematica graphics but Risch can't solve the last integral shown which is int(-4*I*x/((x^2 + 1)^2*(exp(x*I)^2 + 1)), x) if you can solve this, then it is solved. But none of the CAS'es I tried could solve it. Dec 11, 2022 at 20:48
• Hey David, perhaps this link will interest you. It's related to the integration, not in Mma though
– bmf
Dec 12, 2022 at 1:15
• The definite integral over $[0,\pi/4]$, yielding $0.156503245699...$, gives no matches for analytic formulas in AskConstants 5.0. Dec 12, 2022 at 8:37
• For the fun of approximations, I wrote something in math.stackexchange.com/questions/2138866/… Dec 12, 2022 at 15:21
• I used the software FRICAS as it can decide whether a function is non elementary. As I understand it displays an error message when it can not determine if the integral is elementary and returns an unevaluated formal integral if it has proved that the integral is not elementary. It returned the integral unevaluated so the integral can not be constructed from functions like Exp,Log, Tan. It does not say whether it can be constructed from special functions like Fresnel Integrals or error functions. Dec 17, 2022 at 6:14

We can convert the integral into a direct sum by using the series expansion of the tangent: $$\tan^2(x)=\sum_{k=1}^{\infty} \frac{2\,(2^{2k+2}-1)\, (2k+1)\, \zeta(2k+2)}{\pi^{2k+2}} x^{2 k}$$ in terms of the Riemann zeta function (or alternatively in terms of the Bernoulli numbers through $$\zeta(2k+2)=\frac{(-1)^k 2^{2k+1} \pi^{2k+2} B_{2k+2}}{(2 k+2)!}$$).

The integral in question becomes $$\int\frac{\tan^2(x)}{1+x^2}dx =\sum_{k=1}^{\infty} \frac{2\,(2^{2k+2}-1)\, (2k+1)\, \zeta(2k+2)}{\pi^{2k+2}} \int\frac{x^{2 k}}{1+x^2}dx$$

which we can integrate component-by-component,

Integrate[x^(2 k) / (1 + x^2), x]
(*    (x^(1 + 2 k) Hypergeometric2F1[1, 1/2 + k, 3/2 + k, -x^2])/(1 + 2 k)    *)


getting the sum $$\int\frac{\tan^2(x)}{1+x^2}dx =\sum_{k=1}^{\infty} \frac{2\,(2^{2k+2}-1)\, \zeta(2k+2)}{\pi^{2k+2}} x^{2k+1} {_2}F_1\left(1,k+\frac{1}{2};k+\frac{3}{2};-x^2\right).$$

This sum converges rapidly: For large $$k$$, $$\zeta(2k+2)\approx1$$ and the hypergeometric function is $${_2}F_1\left(1,k+\frac12;k+\frac32;-x^2\right)\approx\frac{1}{1+x^2}$$, which makes the $$k^{\text{th}}$$ term approximately $$\frac{2\,(2^{2k+2}-1)\, \zeta(2k+2)}{\pi^{2k+2}} x^{2k+1} {_2}F_1\left(1,k+\frac{1}{2};k+\frac{3}{2};-x^2\right) \approx \left(\frac{2x}{\pi}\right)^{2k+2}\frac{2}{x(1+x^2)},$$ which becomes exponentially smaller with $$k\to\infty$$ as long as $$|x|<\frac{\pi}{2}$$.

Let's try out the case of definite integration over $$[0,\frac{\pi}{4}]$$:

NIntegrate[Tan[x]^2/(1 + x^2), {x, 0, π/4}]
(*    0.1565032456995724    *)

With[{x = π/4},
Sum[2(2^(2k+2)-1) Zeta[2k+2]/π^(2k+2) x^(2k+1) *
Hypergeometric2F1[1, k+1/2, k+3/2, -x^2], {k, 20}]] // N
(*    0.1565032456994413    *)


#### side note on hypergeometric functions

The hypergeometric function can also be written as a Hurwitz–Lerch transcendent or as an incomplete beta function: $${_2}F_1\left(1,k+\frac12;k+\frac32;z\right) =\left(k+\frac12\right) \Phi \left(z,1,k+\frac12\right) =\frac{k+\frac12}{z^{k+1/2}} B_z\left(k+\frac12,0\right)$$ Of these, the Hurwitz–Lerch transcendent is probably the simplest to evaluate in practice because it reduces to a finite sum: $$\Phi \left(z,1,k+\frac12\right) = \frac{2}{z^{k+1/2}}\tanh^{-1}\sqrt{z}-\sum_{s=1}^k \frac{1}{\left(k+\frac12-s\right)z^s}$$

With this simplification the result can be written in terms of $$\arctan$$ and $$\zeta$$ only:

With[{x = π/4},
Sum[2 (-1)^k (2^(2k+2)-1) (2k+1) π^(-2k-2) Zeta[2k+2] *
(ArcTan[x] - Sum[((-1)^q x^(2q+1))/(2q+1), {q, 0, k-1}]),
{k, 20}]] // N
(*    0.1565032456994413$$$$    *)
`
• Excellent work ($+1$). So we have an asymptotic expansion, but alas not a closed-form solution... even for such an apparently "simple" integrand. Oh well. Dec 12, 2022 at 19:07
• Most "special functions" are nothing but someone failing to find a closed-form function and simply defining a new one. If this integral is sufficiently useful we can do it too! Dec 12, 2022 at 19:58
• Closed-form functions, in this case elementary are different. It is hard to describe, but follows from group theory that those are much more simple. Dec 14, 2022 at 3:53
• I was just talking about Risch algorithm. It is very hard to find (elementary, non-elementary case is not interesting) function that does not have an elementary antiderivative. Not only are they rare (almost everywhere concepts in measure theory) but it is VERY hard or even provably impossible (if you include Abs in elementary functions class) to solve. In fact methods to solve it (and extend Risch) are developed even now. Dec 14, 2022 at 23:05
• @userrandrand Liouville proved theorems about integrals like $\int P(x)e^{Q(x)}\,dx$ that show $e^x/x$ does not have an elementary integral and which can be applied to $(\sin x)/x$. The functions $P$ and $Q$ must satisfy a constraint in order for an elementary integral to exist. Now $P(x) \tan^2 x$ seems a related type but also quite different. I haven't tried to prove it does not have an elementary integral, but it's highly unlikely given how long it's been on MSE and here. Look up "Integration in finite terms" and "differential algebra" for more background. Dec 15, 2022 at 14:36