Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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 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.

share|improve this question
1  
Are you sure the delay is needed? I would use the undelayed assignment = and no := –  Coolwater Nov 13 '13 at 14:12
3  
Please, provide the actual code rather than an image. –  Sektor Nov 13 '13 at 14:22
1  
@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

2 Answers 2

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}
share|improve this answer
    
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 of ybelukon'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[5] - f[-5]]
Timing[Sum[b[i, 1], {i, 100}] // N]
{0.020519, -5.40851*10^202} 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.