# Integration and Parallel Computing in Mathematica

In mathematica, how would I use parallel computing (or paralleltable) to compute the integrals of $x^2$, $x$, $3x$ from $x=0$ to $10$ just like in the first answer of http://superuser.com/questions/315337/how-to-make-commands-in-mathematica-8-use-all-cores? After entering the three functions into a table, I'm not sure how to tell Mathematica to integrate the functions. I understand that parallel computing is not the most efficient method, but once I realize how to do this, I can extend it to a much more complicated series of functions.

The motivation of this question is that I have a complicated function (see below) that takes a very, very long time to integrate (after two hours, the function still had not integrated). I'm hoping to break this function up into a group of slightly less complicated functions and utilize parallel computing to compute the integrals.

If I can't do this, is there a way to let mathematica use all four cores on my computer to compute the integral, but not using parallel computing?

Thank you.

(Sin[q - x] ((0.476497 - 0.401956 I) - (0.476497 + 0.598044 I) Tanh[
0.933024 (q - x)]) +
Cos[q - x] ((0.401956 + 0.504489 I) + (0.598044 + 0.504489 I) Tanh[
1.06598 (q - x)])) (Sin[
q + x] ((0.476497 - 0.401956 I) - (0.476497 - 0.598044 I) Tanh[
0.933024 (q + x)]) +
Cos[q + x] ((0.401956 - 0.504489 I) + (0.598044 + 0.504489 I) Tanh[
1.06598 (q + x)]))


Note: I plan to integrate x between -10 and 10 where p and q are arbitrary variables so I can then plot the result as a contour map.

-
maybe a duplicate with mathematica.stackexchange.com/questions/1620/… –  Stefan Jul 8 '13 at 18:24
Can I do numerical integration when p and q are arbitrary variables? –  user85503 Jul 8 '13 at 19:49
Where is p? I only see q. –  Rolf Mertig Jul 9 '13 at 21:04

For NIntegrate, you can get a significant speed-up using the method option

Method -> {Automatic, "SymbolicProcessing" -> 0}


If you add this to your calculation, the integrand can be used as written in your question without modification, and it can be dropped right into the Plot as well:

Plot[Re[
NIntegrate[
(Sin[q - x] ((0.476497 -
0.401956 I) - (0.476497 + 0.598044 I) Tanh[
0.933024 (q - x)]) +
Cos[q - x] ((0.401956 +
0.504489 I) + (0.598044 + 0.504489 I) Tanh[
1.06598 (q - x)])) (Sin[
q + x] ((0.476497 -
0.401956 I) - (0.476497 - 0.598044 I) Tanh[
0.933024 (q + x)]) +
Cos[q + x] ((0.401956 -
0.504489 I) + (0.598044 + 0.504489 I) Tanh[
1.06598 (q + x)])),
{x, -10., 10.},
Method -> {Automatic, "SymbolicProcessing" -> 0}]
],
{q, -10., 10.},
PlotPoints -> 10
] // AbsoluteTiming


This timing is for the integration and the Plot, and it's less than a second.

-
always something i preach...that's why i appreciate you write about it (+1) –  Stefan Jul 9 '13 at 22:53
I didn't know about this "SymbolicProcessing" -> 0 trick ... works brilliantly for complicated examples where we don't want symbolic output. Any downsides that we should be aware of? –  wolfies Jul 10 '13 at 5:21
Regarding downsides, I'd say you'll be safe using this option, and I would only leave it out if NIntegrate throws warnings or errors, because then symbolic processing might help massage the integrand to avoid potential numerical problems. –  Jens Jul 11 '13 at 3:57

I do not think that you can integrate your function analytically. However, if you just want to get a plot, the standard way to do so is:

int[qq_?NumberQ] :=
NIntegrate[(Sin[
q - x] ((0.476497 -
0.401956 I) - (0.476497 + 0.598044 I) Tanh[
0.933024 (q - x)]) +
Cos[q - x] ((0.401956 +
0.504489 I) + (0.598044 + 0.504489 I) Tanh[
1.06598 (q - x)])) (Sin[
q + x] ((0.476497 -
0.401956 I) - (0.476497 - 0.598044 I) Tanh[
0.933024 (q + x)]) +
Cos[q + x] ((0.401956 -
0.504489 I) + (0.598044 + 0.504489 I) Tanh[
1.06598 (q + x)])), {x, -10., 10.}];
Plot[Re[int[q]], {q, -10., 10.}, PlotPoints -> 10] // AbsoluteTiming