# How can I teach Mathematica to break divergent integrals into terms?

For instance, I get the following expression:

$$\int_0^{\infty } \left(\frac{e^2 x}{e^{2 x+2}-e^{2 x}}-\frac{x}{e^{2 x+2}-e^{2 x}}-\frac{e^2}{2 \left(e^{2 x+2}-e^{2 x}\right)}+\frac{3}{2 \left(e^{2 x+2}-e^{2 x}\right)}+\frac{1}{2}\right) \, dx+\frac{1}{2-2 e^2}$$

I want it to be automatically simplified to $$\int_0^\infty \frac12 dx$$, because all other terms cancel each other (can be verified).

Or $$\int_0^{\infty } \left(\frac{3 e^2 x^2}{6 e^x-12 e^{x+1}+6 e^{x+2}}-\frac{6 e x^2}{6 e^x-12 e^{x+1}+6 e^{x+2}}+\frac{3 x^2}{6 e^x-12 e^{x+1}+6 e^{x+2}}-\frac{9 e^2 x}{6 e^x-12 e^{x+1}+6 e^{x+2}}+\frac{24 e x}{6 e^x-12 e^{x+1}+6 e^{x+2}}-\frac{15 x}{6 e^x-12 e^{x+1}+6 e^{x+2}}+x+\frac{e^2}{6 e^x-12 e^{x+1}+6 e^{x+2}}-\frac{8 e}{6 e^x-12 e^{x+1}+6 e^{x+2}}+\frac{13}{6 e^x-12 e^{x+1}+6 e^{x+2}}\right) \, dx+\frac{1}{3}-\frac{1}{1-2 e+e^2}$$

should be simplified to $$\int_0^\infty x dx$$.

Or $$\frac{12 e^4 \int_0^{\infty } \left(2 e^{-2 x} x+2 x+\frac{2}{e^{2 x+2}-e^{2 x}}-\frac{e^2}{e^{4 x}+e^{4 x+2}}+\frac{1}{e^{4 x}+e^{4 x+2}}+1\right) \, dx}{12 e^4-12}-\frac{12 \int_0^{\infty } \left(2 e^{-2 x} x+2 x+\frac{2}{e^{2 x+2}-e^{2 x}}-\frac{e^2}{e^{4 x}+e^{4 x+2}}+\frac{1}{e^{4 x}+e^{4 x+2}}+1\right) \, dx}{12 e^4-12}-\frac{e^4}{12 e^4-12}-\frac{18 e^2}{12 e^4-12}-\frac{5}{12 e^4-12}$$

should be simplified to $$\frac16+2\int_0^\infty x dx+\int_0^\infty 1 dx$$.

Is there a way to make Mathematica doing so?

P.S. Input form for the first example:

1/(2 - 2*E^2) +
Integrate[
1/2 + 3/(2*(-E^(2*x) + E^(2 + 2*x))) -
E^2/(2*(-E^(2*x) + E^(2 + 2*x))) -
x/(-E^(2*x) + E^(2 + 2*x)) + (E^2*x)/(-E^(2*x) +
E^(2 + 2*x)), {x, 0, Infinity}]


For the second example:

-(1/3) + 1/(1 - 2*E + E^2) +
Integrate[(13*E^x)/(6 - 12*E + 6*E^2) - (8*E^(1 + x))/(6 - 12*E +
6*E^2) +
E^(2 + x)/(6 - 12*E + 6*E^2) -
x + (15*E^x*x)/(6 - 12*E + 6*E^2) - (24*E^(1 + x)*x)/(6 - 12*E +
6*E^2) +
(9*E^(2 + x)*x)/(6 - 12*E + 6*E^2) + (3*E^x*x^2)/(6 - 12*E +
6*E^2) -
(6*E^(1 + x)*x^2)/(6 - 12*E + 6*E^2) + (3*E^(2 + x)*x^2)/(6 -
12*E + 6*E^2), {x, 0, Infinity}]


For the third example:

-(5/(-12 + 12*E^4)) - (18*E^2)/(-12 + 12*E^4) - E^4/(-12 + 12*E^4) -
(12*Integrate[
1 + 2/(-E^(2*x) + E^(2 + 2*x)) + 1/(E^(4*x) + E^(2 + 4*x)) -
E^2/(E^(4*x) + E^(2 + 4*x)) +
2*x + (2*x)/E^(2*x), {x, 0, Infinity}])/(-12 + 12*E^4) +
(12*E^4*
Integrate[
1 + 2/(-E^(2*x) + E^(2 + 2*x)) + 1/(E^(4*x) + E^(2 + 4*x)) -
E^2/(E^(4*x) + E^(2 + 4*x)) +
2*x + (2*x)/E^(2*x), {x, 0, Infinity}])/(-12 + 12*E^4)


Full code example:

f[x_] := 1 + Exp[-2 x]
g[x_] := 1

omb := D[Sum[
Refine[DifferenceDelta[Integrate[Integrate[f[t], {t, 0, y}], y],
y]*DifferenceDelta[Integrate[Integrate[g[t], {t, 0, y}], y], y],
y > 0], {y, 0, x - 1}], x]
Func := D[omb, x]; Const := omb /. x -> 0
Inactivate[
Integrate[f[x], {x, 0, Infinity}]\[CenterDot]Integrate[
g[x], {x, 0, Infinity}], Integrate] ==
FullSimplify[Const + Integrate[Func, {x, 0, Infinity}]] //
ExpandAll // Quiet

• Please include the corresponding Mathematica code in copy and paste form (InputForm) Dec 19, 2021 at 13:47
• @BobHanlon I do not understand, what should I include. Dec 19, 2021 at 13:54
• Why not acting with FullSimplify on the integrand? Are there some surface terms leftover? Dec 19, 2021 at 13:58
• At a minimum, the Mathematica code for the integrands. You could also include the integrals with Inactive[Integrate] Dec 19, 2021 at 14:01
• @FilipeMiguel the terms under integral cancel with terms outside integral. Dec 19, 2021 at 14:06

You might use Distribute[Integrate[Expand[f], {x, 0, Infinity}]], but it seems easier to distribute with Map:

Check[ (* omit Check if not desired *)
res = 1/(2 - 2*E^2) +
Integrate[
1/2 + 3/(2*(-E^(2*x) + E^(2 + 2*x))) -
E^2/(2*(-E^(2*x) + E^(2 + 2*x))) -
x/(-E^(2*x) + E^(2 + 2*x)) + (E^2*x)/(-E^(2*x) +
E^(2 + 2*x)), {x, 0, Infinity}],
(** Here's where we distribute Integrate: **)
res /.
HoldPattern[Integrate[f_, x__]] :> With[{terms = Expand[f]},
Integrate[#, x] & /@ terms /; MatchQ[terms, _Plus]],
(**                                       **)
Integrate::idiv] // Simplify

(* Integrate[1/2, {x, 0, Infinity}] *)


Update:

One needs the full linearity of integrals to deal with the OP's problem, and Integrate does not do that. Using linearExpand from another answer we get:

Clear[linearExpand];
linearExpand[e_] :=
e //. {int : Inactive[Integrate][_Plus, _] :> Distribute[int],
Inactive[Integrate][integrand_Times, dom : {x_, _, _} | x_] :>
With[{dependencies =
InternalDependsOnQ[#, x] & /@ List @@ integrand},
Pick[integrand, dependencies, False]*
Inactive[Integrate][Pick[integrand, dependencies, True], dom]]};

linearExpand[Inactive[Integrate][Func // Expand, {x, 0, Infinity}]] //
Activate // Simplify

(*
1/(2*(-1 + E^2)) +
(1/2)*Integrate[1, {x, 0, Infinity}] +
2*Integrate[x, {x, 0, Infinity}]
*)

• Ah, this seems to work (I didn't trust Distribute not to evaluate Integrate before distributing, and maybe I still shouldn't?): res /. HoldPattern[i : Integrate[f_, x__]] :> With[{terms = Expand[f]}, Distribute[Unevaluated@Integrate[terms, x]] /; MatchQ[terms, _Plus]] Dec 19, 2021 at 16:48
• Distribute by itself works fine with the result but does not work in the code. Dec 19, 2021 at 19:53
• @Anixx I don't know what you mean by "does not work in the code" since it gives me the same result that you said was desired in the OP (except the second example, in which the code is different than the TeX). I guess I'm not sure what "Distribute by itself" means, since I can imagine it being applied correctly or incorrectly. Dec 19, 2021 at 20:32
• The following code gives the desired answer:FullSimplify[5/(12 - 12*E^2) + E^2/(12 - 12*E^2) + Distribute[Integrate[1/2 + 3/(2*(-E^(2*x) + E^(2 + 2*x))) - E^2/(2*(-E^(2*x) + E^(2 + 2*x))) + 2*x - x/(-E^(2*x) + E^(2 + 2*x)) + (E^2*x)/(-E^(2*x) + E^(2 + 2*x)), {x, 0, Infinity}]]] The problem happens when the integrand is a variable. Inserting "Evaluate" does not help. Dec 19, 2021 at 20:37
• This seems to work the way I thought you wanted: Distribute[Integrate[x, {x, 0, Infinity}]]. (Distribute actually does nothing when the integrand is a variable, since the Head of the integrand is not Plus.) Dec 19, 2021 at 21:27

The first example is fairly easy, the others need more work...

Clear["Global*"]

conv = a_. + b_. * Inactive[Integrate][integrand_, iter_List] :>
Simplify[
a + Simplify[
b*Integrate[#, iter] & /@ (integrand // FullSimplify // Expand)]];

Off[Integrate::idiv]

1/(2 - 2*E^2) +
Inactive[Integrate][
1/2 + 3/(2*(-E^(2*x) + E^(2 + 2*x))) - E^2/(2*(-E^(2*x) + E^(2 + 2*x))) -
x/(-E^(2*x) + E^(2 + 2*x)) + (E^2*x)/(-E^(2*x) + E^(2 + 2*x)), {x, 0,
Infinity}] /. conv


Although it is not clear what this integral tells you that the original didn't: the integral is divergent.

Well, the trick was to use Distribute outside of Integrate and ExpandAll and FullSimplify inside:

FullSimplify[Const + Distribute[Integrate[ExpandAll[FullSimplify[Func]],
{x, 0, Infinity}]]] // Quiet


The both FullSimplifys are needed here, as well as ExpandAll inside Integrate.

Full code:

f[x_] := 1 + Exp[-2 x]
g[x_] := 1

omb := D[Sum[
Refine[DifferenceDelta[Integrate[Integrate[f[t], {t, 0, y}], y],
y]*DifferenceDelta[Integrate[Integrate[g[t], {t, 0, y}], y], y],
y > 0], {y, 0, x - 1}], x]
Func := D[omb, x]; Const := omb /. x -> 0
Inactivate[
Integrate[f[x], {x, 0, Infinity}]\[CenterDot]Integrate[
g[x], {x, 0, Infinity}], Integrate] ==
FullSimplify[
Const + Distribute[
Integrate[
ExpandAll[FullSimplify[Func]], {x, 0, Infinity}]]] // Quiet


And the result: $$\int _0^{\infty }\left(1+e^{-2 x}\right)dx\cdot \int _0^{\infty }1dx=\int_0^{\infty } \frac{1}{2} \, dx+\int_0^{\infty } 2 x \, dx-\frac{1}{12}$$