Group Abstract Group Abstract

Message Boards Message Boards

Can you parallelize a numerically solved Integral?

Posted 4 years ago
POSTED BY: sorabella91
3 Replies

If functions are listable like Cos, using Range together with Total usually gets met the fastest results.

In[1]:= f[x_] := Cos[x]; (*function*)
n = 75;

I2[f_, a_, b_, n_] := (h = (b - a)/n; 
  Sum[f[a + (i - 1/2)*h]*h, {i, 1, n}])

I2R[f_, a_, b_, n_] := With[{h = (b - a)/n},
  Total[f[a + Range[0.5, n - 0.5, 1.] h] h]
  ]

{t1, v1} = RepeatedTiming@I2[f, -1.0, 1, n];
{t2, v2} = RepeatedTiming@I2R[f, -1.0, 1, n];
{t1/t2, v1 === v2}

Out[6]= {16.0959, True}
POSTED BY: Martijn Froeling
Posted 4 years ago
POSTED BY: Updating Name

It is possible that Sum will do symbolic processing under the hood. You might forestall that by using NSum or perhaps a different construct such as Total[Table[...]]. Only if these fail to deliver adequate speed would I look into coarse-grained parallelizing of the computation.

POSTED BY: Daniel Lichtblau
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard