intersection of hypercube and hypersphere
There is a number of similar questions already (e.g. this one), but as far as I can see, none quite cuts it for me.
In $n$-dimensional euclidean space, a hypercube $H$ with side lengths $2A$ is centered around the origin. So is a hypersphere $S$ with radius $x$. What is the fraction of volume of the hypercube $H$ that is also inside the hypersphere $S$, that is, what is the volume of $H\cap S$?
As calculating the fraction with respect to the hypercube is trivial by just dividing by its volume in the end, it boils down to calculating the volume of the intersection. My first idea was to separate three different cases:
- If $x<A$ the hypersphere is fully contained in the hypercube. Then, the volume is simply the volume of the hypersphere, for which there are analytical formulae.
- If $x^2> n \cdot A^2$, the hypercube is fully contained in the hypersphere. In this case, the volume is simply that of the hypercube, that is, $(2A)^n$.
- For intermediate values of $x$, the intersection is given as the volume of the hypersphere minus $2n$ hyperspherical caps, for which there is also a closed form solution (e.g. here)
After my calculation consistently gave wrong results, I was forced to admit that the case (3) is more difficult than I thought, because as soon as the opening angle of the hypercaps is larger than $\pi/4$, they start to intersect along the edges of the hypercube, whereas the corners are still outside the intersection volume. For $n=3$, this can be seen in this graphic, which was generated by wolframalpha.
Thus, the solution proposed in (3) double-counts these volumes.
I can't seem to come up with general solution to calculate this, because counting (and calculating) the intersection areas is very tedious.
Is there any closed-form, analytic solution available for this problem?
Solution 1:
The complexity here comes from the fact that in $n$ dimensions there are $n-1$ types of extended boundaries of the hypercube (in which $1,2,\ldots,n-1$ coordinates are maxed-out at $\pm A$). So, while in $3$ dimensions there are only edges and faces, the nomenclature of "caps" and "corners" does not capture the behavior in higher dimensions. The hypersphere starts intersecting the boundaries of type $j$ when its radius reaches $A\sqrt{j}$, and only fully contains them when its radius exceeds $A\sqrt{n}$, so we expect the final formula to be non-smooth at $n$ different radii.
However, we can find a reasonably simple recursive form. Let $V_n(R)$ be the volume of the intersection in $n$ dimensions when the hypersphere has radius $R$ and the hypercube has side length $2$. Then $$ V_n(R)=\int_{x_1=-1}^{+1}\int_{x_2=-1}^{+1}\cdots\int_{x_n=-1}^{+1}I\left[\sum_{i=1}^{n}x_i^2 < R^2\right]dx_1 dx_2 \cdots dx_n, $$ where $I(\Phi)$ is $1$ when $\Phi$ is true and $0$ otherwise. The integrand is nonzero only when $|x_1|<R$, in which case we have $$ I\left[\sum_{i=1}^{n}x_i^2 < R^2\right]=I\left[\sum_{i=2}^{n}x_i^2 < R^2-x_1^2\right]; $$ so $$ V_n(R)=\int_{x=-\min(1,R)}^{+\min(1,R)}V_{n-1}\left(\sqrt{R^2-x^2}\right)dx. $$ The base of the recursion is $V_0(R)=1$; or, if the $0$-dimensional volume seems too contrived, $V_1(R)=2\min(1,R)$.
Solution 2:
Not sure if this will be heplful (it seems to get very messy), but one can set up the integrals for the volumes of a 'corner' (in the case that $H\backslash S$ is disconnected)
$$\int_{\sqrt{R^2-(n-1)A^2}}^{A}\int_{\sqrt{R^2-(n-2)A^2-x_1^2}}^A...\int_{\sqrt{R^2-A^2-x_1^2-...-x_{n-2}^2}}^A\int_{\sqrt{R^2-x_1^2-...-x_{n-1}^2}}^A~dx_ndx_{n-1}dx_{n-2}...dx_1.$$
Let the volume of this be $C(n,R,A)$ as there are $2^n$ such 'corners' all with equal volume, so your intersection's volume is
$$2^n(A^n-C(n,R,A)).$$
Perhaps the formula $$C(n,R,A)=\int_{\sqrt{R^2-(n-1)A^2}}^A C(n-1, \sqrt{R^2-x_1^2}, A)~dx_1$$
could be useful to inductively compute the integrals for these cases...