# Definite Integral. Perfomance problem

I have a notebook in Mathematica 4. I'm trying to convert it to use in Mathematica 9. One of the problem is the long computation of definite integral. In Mathematica 4 the result of the following code is ~18.4 and ~0.66 seconds, but the same code in Mathematica 4 gives me 0.266 and 0.031 !

Why?

upd. The code itself:

B[i_,j_]:=Integrate[(x-i)^i x^j,{x,-5,5}];
Timing[Sum[B[i,1],1,10];]


The real program is much more complex. This is just the small part of it.

• Are you sure the delay is needed? I would use the undelayed assignment = and no := – Coolwater Nov 13 '13 at 14:12
• Please, provide the actual code rather than an image. – Sektor Nov 13 '13 at 14:22
• @coolwater the delay is certainly needed for the NIntegrate because anything except the integration variable should be numerical. It might not be necessary for the symbolical integration. – Sjoerd C. de Vries Nov 13 '13 at 15:51

One can calculate this integral analytically

Integrate[(x - i)^i x^j, {x, -5, 5}, Assumptions -> {(i | j) ∈ Integers, j > 0}]

ConditionalExpression[(1/(1 + j))
5^(1 + j) (-i)^i (Hypergeometric2F1[-i, 1 + j, 2 + j, 5/i] +
Hypergeometric2F1[-i, 1 + j, 2 + j, -(5/i)] (Cos[j π] + I Sin[j π])), i < -5]


Mathematica returns the result for i < -5 but it also correct for positive i.

Now you can define the function

b[i_, j_] := 1/(1 + j) 5^(1 + j) (-i)^i (Hypergeometric2F1[-i, 1 + j, 2 + j, 5/i] +
Hypergeometric2F1[-i, 1 + j, 2 + j, -(5/i)] (Cos[j π] + I Sin[j π]));


It is ~1000 times faster:

Sum[B[i, 1], {i, 1, 20}] // AbsoluteTiming

{1.224481, -28464057995788622073916233379750/693}

Sum[b[i, 1], {i, 1, 20}] // AbsoluteTiming

{0.001921, -28464057995788622073916233379750/693}

• What version of Mathematica are you using? I get a different result with V9.0.1. – m_goldberg Nov 13 '13 at 23:00
• @m_goldberg Also V9.0.1, on Linux. What is your result? – ybeltukov Nov 13 '13 at 23:05
• Integrate[(x - i)^i x^j, {x, -5, 5}, Assumptions -> {(i | j) \[Element] Integers, j > 0}] gives ConditionalExpression[ 5^(1 + j) (-i)^ i Gamma[1 + j] (E^(I j \[Pi]) Hypergeometric2F1Regularized[-i, 1 + j, 2 + j, -(5/i)] + Hypergeometric2F1Regularized[-i, 1 + j, 2 + j, 5/i]), i < -5]. This is on OS X. – m_goldberg Nov 13 '13 at 23:09
• @m_goldberg It is equivalent answer since by definition Hypergeometric2F1Regularized[a,b,c,z] is Hypergeometric2F1[a,b,c,z]/Gamma[c]. However it is strange that the form of the answer is different in different OSs. – ybeltukov Nov 13 '13 at 23:28
• Even more interesting is that Integrate[(x - i)^i x^j, {x, -5, 5}, Assumptions -> {(i | j) \[Element] Integers, j > 0, i > 0}] gives ConditionalExpression[-((8 5^(6+j) (15+(-1)^j (7+2 j) (135+2 j (7+j) (17+j (7+j)))))/((1+j) (2+j) (3+j) (4+j) (5+j) (6+j))),i==5], which is also "equivalent", but for only one value of i. – m_goldberg Nov 13 '13 at 23:43

This is just the working out of the full details of the approach suggested by Michael E2 in his comment on ybeltukov's answer.

b[i_, j_] :=
Evaluate @
With[{
f =
((Integrate[(x - i)^i x^j, x] /.
Times[coeff_, hyper_Hypergeometric2F1] :>
Simplify[coeff, i ∈ Integers && x > i] hyper) /.
x :> #) &
},
f - f[-5]]
Timing[Sum[b[i, 1], {i, 100}] // N]

{0.020519, -5.40851*10^202}