Tuesday, January 10, 2012

Hypersphere from points on surface

If you want to compute the center and radius of a hypersphere in $\mathbb{R}^n$, you need $n+1$ points ($p_i$ for $i=1$ to $n+1$) to fully specify it.  The $j^{th}$ coordinate of the $i^{th}$ point is $p_{ij}$.  The center point is $m$ with scalar radius $r$.

We'll consider two formulae:

    1.  $\sum_{j=1}^n(q_j-m_j)^2 - r^2 = 0$ (any point $q$ for which this is true is a point on the surface of the sphere).
And:
     2.  $\sum_{j=1}^n(aq_j^2 + b_jq_j) + c = 0$ (where $a$, $b_j$, and $c$ are scalar coefficients).

For reasons that are not entirely clear to me (but this discussion on finding a sphere from 4 points deserves the credit), we can write this equation as the determinant of a matrix:

$\left| \begin{array}{ccccc}
\sum_{j=1}^n q_j^2 & q_1 & \cdots & q_n & 1\\
\sum_{j=1}^n p_{1j}^2 & p_{11} & \cdots & p_{1n} & 1 \\
\vdots & & \ddots & & \vdots \\
\sum_{j=1}^n p_{(n+1)j}^2 & p_{(n+1)1} & \cdots & p_{(n+1)n} & 1
\end{array}\right| = 0$

When we find this determinant in terms of all the $q_j$s, it will be in the form of Eqn. 2 above.  Since we want to extract the center point and radius, we'll make Eqn. 1 meet us half-way by expanding the squared part to get:

    3.  $\sum_{j=1}^n(q_j^2-2q_jm_j+m_j^2) - r^2 = 0$

In this form, it's clear that the $q_j^2$ components don't have coefficient $a$.  So we divide that into the $b_j$ and $c$ components of Eqn. 2:
    4.  $\sum_{j=1}^{n+1}(q_j^2 + \frac{b_j}{a}q_j) + \frac{c}{a} = 0$

Then we can make all of the pieces fit into Eqn. 3:
$\begin{align}
-2m_j &= \frac{b_j}{a}\\
\sum_{j=1}^{n+1}m_j^2 - r^2 &= \frac{c}{a}
\end{align}$

And from there, it's straightforward to find $m_j$ and $r$. 

 
Now, if you happen to have more than $k>n+1$ points, what I want to do is find the fit that minimizes $\sum_{i=1}^k\left|\sum_{j=1}^n(p_{ij}-m_j)^2 - r^2\right|$, which is to say, the sum of squared distances from the surface of the sphere. 

I can't seem to find a way to do that without using a numerical, non-linear optimization approach (not too far removed from, guess and check to see if a nearby solution is better than what you've found so far).  If anyone happened to stumble upon this page and knew of a fancier method, that would be a nice comment to leave for posterity.  Or if you can explain why the determinant thing works, we'd all appreciate that too.