7
$\begingroup$

How to speed up this code? It's unbelievably slow!!! My solution with pen and paper is faster.

Clear[R, h];
Integrate[
 Integrate[
  Integrate[1, {z, h, Sqrt[R^2 - x^2 - y^2]}, 
   Assumptions -> Element[y, Reals]]
  , {y, -Sqrt[R^2 - h^2 - x^2], Sqrt[R^2 - h^2 - x^2]}, 
  Assumptions -> Element[x, Reals]]
 , {x, -Sqrt[R^2 - h^2], Sqrt[R^2 - h^2]}, 
 Assumptions -> 
  Element[R, Reals] && R > 0 && Element[h, Reals] && 0 < h < R, 
 GenerateConditions -> False]

$\frac{1}{3}\pi (h-R)^2 (h+2 R)$

It's a triple integral over a spherical cap.

I have got the solution, but... I can't understand why Mathematica is so slow.

$\endgroup$
  • $\begingroup$ @corey I use 3 Integrals, because of the documentation (It´s bad). And the results are different. With 1 Integral I can´t use the result as function(but it´s fast :-) ) and With 3 Integrals of go weel except the slow. With one integral I have problems with the Assumptions. $\endgroup$ – Mika Ike Dec 30 '16 at 12:49
  • 1
    $\begingroup$ Why not use cylindrical coordinates? Integrate[r, {z, h, R}, {θ, 0, 2 π}, {r, 0, Sqrt[R^2 - z^2]}] $\endgroup$ – Simon Woods Dec 30 '16 at 21:44
10
$\begingroup$

A timing analysis. Each integration was performed on a fresh kernel.

The OP's code:

Integrate[
  Integrate[
   Integrate[1, {z, h, Sqrt[R^2 - x^2 - y^2]}, Assumptions -> Element[y, Reals]],
    {y, -Sqrt[R^2 - h^2 - x^2], Sqrt[R^2 - h^2 - x^2]}, Assumptions -> Element[x, Reals]],
     {x, -Sqrt[R^2 - h^2], Sqrt[R^2 - h^2]}, Assumptions -> Element[R, Reals] && R > 0 && Element[h, Reals] && 0 < h < R, 
  GenerateConditions -> False] // AbsoluteTiming

{326.12, 1/3 π (h - R)^2 (h + 2 R)}

Reformulating it is much faster:

Integrate[1,
 {x, -Sqrt[R^2 - h^2], Sqrt[R^2 - h^2]},
 {y, -Sqrt[R^2 - h^2 - x^2], Sqrt[R^2 - h^2 - x^2]},
 {z, h, Sqrt[R^2 - x^2 - y^2]}, 
  Assumptions -> {x, y, z} ∈ Reals && R > 0 && 0 < h < R] // AbsoluteTiming

{80.5937, 1/3 π (h - R)^2 (h + 2 R)}

It could be sped up with Boole; there should be no difference between x > h, y > h and z > h due to the symmetry, but:

Integrate[
  Boole[x^2 + y^2 + z^2 < R^2 && x > h], {x, -Infinity, Infinity},
   {y, -Infinity, Infinity}, {z, -Infinity, Infinity}, 
    Assumptions -> R > 0 && 0 < h < R] // AbsoluteTiming

{11.8003, 1/3 π (h - R)^2 (h + 2 R)}

Integrate[
  Boole[x^2 + y^2 + z^2 < R^2 && y > h], {x, -Infinity, Infinity}, 
   {y, -Infinity, Infinity}, {z, -Infinity, Infinity}, 
    Assumptions -> R > 0 && 0 < h < R] // AbsoluteTiming

{106.909, 1/3 π (h - R)^2 (h + 2 R)}

Integrate[
  Boole[x^2 + y^2 + z^2 < R^2 && z > h], {x, -Infinity, Infinity},
   {y, -Infinity, Infinity}, {z, -Infinity, Infinity}, 
    Assumptions -> R > 0 && 0 < h < R] // AbsoluteTiming

{86.4646, 1/3 π (h - R)^2 (h + 2 R)}

EDIT: The timing differences in the last three integrals are not due to changing the names of the variables var (i.e., $x\leftrightarrow y\leftrightarrow z$ in the var > h part), as could be suspected at first glance, but due to the order of integration; in Integrate[f[x,y,z], {x, ....}, {y, ....}, {z, ....}] the integration over z is performed first, and over x last. The first (among the last three) integral is the fastest as the integration over the vertical direction through the cap is performed last $-$ i.e., one sums the stacked full circles. In other cases, the consecutive integrations are performed over regions not so regular, which requires MMA to take more time.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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