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.
Integrate[r, {z, h, R}, {θ, 0, 2 π}, {r, 0, Sqrt[R^2 - z^2]}]
$\endgroup$