# Integration gives ComplexInfinity when it shouldn't

A simple example of my problem is that

Assuming[k ∈ Reals, Integrate[Exp[l]/((1 + Exp[l])*Exp[k*l]), l]] /. {k -> 2}


gives ComplexInfinity while

Integrate[Exp[l]/((1 + Exp[l])*Exp[2*l]), l]


gives the correct answer. I would like to get the correct answer in the first case as well. Any suggestions?

I'm using Mathematica 6 on a Windows XP PC.

## Failed attempts to bypass the problem

1. Defining Riemann integration as a separate function using Sum and Limit
2. Using Assumptions in preamble, putting Assuming[] around Integrate, using Integrate[f,x,Assumptions->{x,params}\in Reals]
3. Using FunctionInterpolation on the function to be integrated before applying the (definite) integral
4. Expanding the function to be integrated with Series before integrating

Have to hand it to Mathematica, at least it is consistent.

-

The reason this happens, is because Hypergeometric2F1[1, 1 - k, 2 - k, -E^z] is infinity at $k=2$

Assuming[Element[k, Integers] And k > 0,Integrate[Exp[z]/((1 + Exp[z])*Exp[k*z]), z]]


Hypergeometric2F1[1, 1 - k, 2 - k, -E^z] /. k -> 2


When you wrote

Integrate[Exp[z]/((1 + Exp[z])*Exp[2*z]), z]


Then M went through a different path, since now $k=2$ and it did not need to use Hypergeometric2F1 for the result.

May be relevant, in the help, it says under possible issues for Hypergeometric2F1 see the assumptions on variables used. When they are assumed to be positive integers vs. default complex numbers.

I thought by using assumptions that $k$ is positive integer then this might not make Integrate use Hypergeometric2F1 and go through the same path it did for $k=2$, but for some reason, it still uses this function. May be I did not use the assumptions for Integrate correctly. Just want to add that this is a little strange, but sometimes I feel sorry for Integrate, as I read it is the most complex super function in M, over half million lines of code. So, sometimes it seems to take the wrong fork on the road?

-
Thank you, this info helps me in the right direction. Any workarounds for the ComplexInfinity problem would be appreciated. I'm only working with real numbers, would like to force Mathematica to stick to them. –  Sander Heinsalu Mar 11 '13 at 1:05
Maybe a more precise question would be: how to force the dummy variable of integration to be real. Non-working code Assuming[{k, l} \[Element] Reals, Integrate[Exp[l]/((1 + Exp[l])*Exp[k*l]), l]] /. {k -> 2} –  Sander Heinsalu Mar 11 '13 at 1:13

The difference in the integrands is fundamentally important. The presence of $k$ makes a difference, apparently, in the way the integration algorithm arrives at an antiderivative. (For instance, if the ratio of the exponents in Exp[k l] and Exp[l], i.e., k l / l = k, is rational, then the antiderivative can be expressed in terms of elementary functions; otherwise it seems not.)

A way that seems it might be convenient rests on recognizing the nature of the problem. Define a function to separate out the cases when k is a rational number or not:

myIntegral[k : _Integer | _Rational] :=
myIntegral[k] = Integrate[Exp[l]/((1 + Exp[l])*Exp[k*l]), l];
myIntegral[k_?NumericQ] :=
Integrate[Exp[l]/((1 + Exp[l])*Exp[k*l]), l];


Then, if you want, you can wrap both in a piecewise expression:

integral = Piecewise[{
{myIntegral[k], k \[Element] Rationals},
{Integrate[ Exp[l]/((1 + Exp[l])*Exp[k*l]), l], True}}]

integral /. k -> 2
(* -E^-l + Log[1 + E^-l] *)


There may be other ways of separating the cases that suit your intended use better.

Comment on the default antiderivative

I don't have access to v6, so just as a check, here is what I get (with or without Assuming[...):

int0 = Integrate[Exp[l]/((1 + Exp[l])*Exp[k*l]), l]
(* (E^((1 - k) l) Hypergeometric2F1[1, 1 - k, 2 - k, -E^l])/(1 - k) *)


This is valid antiderivative, but it does diverge as k -> 2. You could use a definite integral to get something convergent:

intDef = Integrate[Exp[t]/((1 + Exp[t])*Exp[k*t]), {t, 0, l}]
(* ConditionalExpression[(-1)^k Beta[-1, 1 - k, 0] -
E^(-k l) (-E^l)^k Beta[-E^l, 1 - k, 0], E^l <= 0] *)


Unfortunately, it generates a condition that requires l to be imaginary. Sometimes these conditions are sufficient but unnecessary, a by-product of the algorithm, and the result is valid generically. That is the case here, which one can check. Alternatively, you can hope for the best -- it's a well-behaved integrand -- and ask for no conditions to be generated:

intNoCond = Integrate[Exp[t]/((1 + Exp[t])*Exp[k*t]), {t, 0, l},
GenerateConditions -> False]
(* (1/(2 (-1 + k)))E^(-k l) (-2 E^l Hypergeometric2F1[1, 1 - k, 2 - k, -E^l] +
E^(k l) (-1 + k) (PolyGamma[0, 1/2 - k/2] - PolyGamma[0, 1 - k/2])) *)


In either case, if you substitute k->2, the answer is Indeterminate:

{intDef, intNoCond} /. k -> 2
(* {ConditionalExpression[Indeterminate, E^l <= 0], Indeterminate} *)


And Limit will not yield up the answer, either. Oh dear. I would like to stress that the answers above all yield valid antiderivatives, although you cannot plug k=2 in directly. While they can be used, they won't suit your purposes.

-

Your function appears to have singularities at integer values:

DiscretePlot[Integrate[Exp[l]/((1 + Exp[l])*Exp[k*l]), l] /. l -> 1,
{k, 1, 5, 0.01}] // Quiet


Are you sure of your expected result?

-
The plot of the integrals strangely contains negative values. All the Exp functions are positive, so the function under the integral is positive everywhere. How can the integral then be negative? –  Sander Heinsalu Mar 11 '13 at 0:50
When $l=1$ it is clear there are no singularities at positive integral values of $k$: MMA appears to have erred here. But it's not entirely its fault: by choosing an indefinite integral, user2137029, you have left it up to Mathematica to choose an arbitrary Complex starting point. Remember that the indefinite integral is determined only up to an additive (Complex) constant, so it is invalid to conclude the indefinite integral must be positive! You can control this integration better by explicitly setting a starting point, such as $0$ or $\infty$: you will like the result much better. –  whuber Mar 11 '13 at 1:17
@whuber I think I should delete my answer since I erred. Would you consider expanding that comment into an answer? –  Mr.Wizard Mar 11 '13 at 1:18
You did not err in plotting the answer MMA gave. I have provided a comment because I cannot fully diagnose the problem; I can only give a workaround. It would be nice to know more about how MMA chooses a starting point for the indefinite version of Integrate. –  whuber Mar 11 '13 at 1:21