\section{Algorithms and proofs}
\label{sec:alg-pro}
%%i%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

In this section of the Appendix, we develop the theoretical background
and prove the results of the article.  More precisely,
\begin{itemize}
\item We prove several intermediate results needed for some
  propositions used in the article. Intermediate results will be
  called \textit{lemmas} while the results quoted in the article will
  be called \textit{propositions}.
\item We precisely describe procedures \algline and \algsphere, as
  well as the procedures \algsphereinter and \algminsphereinter 
  used internally. We also explain the maths behind them.
\item We describe the full \algexact algorithm and prove its convergence.
\end{itemize}


\subsection{Lemmas and propositions}
%%ii-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%


With the centers $c_i$ and radii $R_i$  as  defined in Eqs. \ref{eq:sink-spec}, we have:
%%
\begin{lemma}[Freeness of the $\nabla f_i(x)$]
\label{lem:free_gradient}
For any $x$ in $R^d$ and for any $J \subset \{ 1, \dots, n \}$, the rank of the family $(\nabla f_i(x))_{i \in J}$  
is the dimension of the affine hull of $x$ and $(c_i)_{i \in J}$.

As a consequence, if the centers $c_i$ are in generic position, for any $x \in \Rd$ the family $(\nabla f_i(x))_{i \in I^0(x)}$ is free.
\end{lemma}

\begin{proof}
The rank of said family is the rank of the vectors $(x - c_i)_{i \in J}$, which is exactly the dimension of the affine hull of $x$ and $(c_i)_{i \in J}$.
For the second point, if $I^0(x)$ is empty (resp. a singleton) the result is trivial by convention (resp. because no radius is zero). Assuming $k = \#I^0(x) \geq 2$, $x$ belongs to the intersection $S_{I^0_{(x)}} = \bigcap_{i \in I^0(x)} S_i$ which is a $(d-k)$ dimensional sphere centered around a point $c_S$
of $\mathrm{Aff}((c_i)_{i \in I^0})$ orthogonal to the previous affine space, with radius $R_S$ - see Lemma \ref{lem:sphere_intersection} for the exact computations. As a consequence the dimension of the affine hull of $x$, $(c_i)_{i \in I^0(x)}$ is 1 plus the dimension of the affine hull of $(c_i)_{i \in I^0(x)}$ which is $k-1$ by genericity of the centers.  
\end{proof}

The following lemma describes the full-dimensional cells in any neighborhood of a point. It is illustrated in Fig. \ref{fig:neighboring_cells} below.

\begin{lemma}[Full-dimensional cells around a point]
\label{lem:neighboring_cells}
Assume that the family $\{ c_i\} $ lies in a generic position. 
Let $x \in \Rd$ be a point with $\#I^0(x) = k(x) > 0$. Then any neighborhood of $x$ encounters exactly $2^{k(x)}$ cells of full-dimension, and any such cell is determined by $I^+ = I^+(x) \cup A$, $I^- = I^-(x) \cup B$, where $A \cup B$ is a 2-partition of $I^0(x)$.
\end{lemma}

\begin{proof}
It is clear that the full dimensional cells possibly existing around $x$ are included in the one described in the proposition.

 Reciprocally, let $A \subset I^0(x)$ and $B = I^0(x) \setminus A$. Consider the set $U_{A}$ the cone of vectors such that $\scal{\nabla f_i(x)}{u} \geq 0$,$\scal{\nabla f_j(x)}{u} \leq 0$ for all $i \in A$, $j \in B$. This is the polar (or dual) cone to the cone $T_A$ generated by the vectors $(-\nabla f_i(x))_{i \in A}$ and $(\nabla f_j(x))_{j \in B}$; as such $U_A$ has non-empty interior if and only if $T_A \cap (-T_A) = \{ 0 \}$. This condition is satisfied (for every $A \subset I^0(x)$) as the family $(\nabla f_i(x))_{i \in I^0(x)}$ is free by the previous lemma. Any $u$ in the interior of $U_A$ satisfies $\scal{\nabla f_i(x)}{u} > 0$, $\scal{\nabla f_j(x)}{u} < 0$ for any $i \in A, j \in B$ and thus $I^+(x +tu) = I^+(x) \cup A$, $I^-(x +tu) = I^-(x) \cup B$ for $t > 0$ small enough.
\end{proof}

\begin{figure}[htb]% or !htb or H
\centerline{\includegraphics[width=.5\linewidth]{\wfig/fig_neighboring_cells.pdf}}
\caption{{\bf Neighboring cells.}
Each red dot has $I^0$ of cardinal two and $2^2$ neighboring cells of full dimension. In blue, points with $I^0$ of cardinal 1 have $2^1$ neighboring cells.}
\label{fig:neighboring_cells} 
\end{figure} 

Remark that $F$ is smooth at point $x$ if and only if $x$ does not lie on a sphere. The Clarke gradient of $F$ at $x$ will allow us to study $F$ in a neighborhood of $x$. Thanks to Prop. \ref{alg:LineDescent} it has the following explicit representation (Fig. \ref{fig:clarke-gradient} for an illustration), where $\nabla f_i(x)$ is denoted by $\nabla f_i$ to ease notation.

\begin{proposition}[Clarke gradient of $F$]
\label{prop:clarke-F}
For any $x \in \Rd$ we have :
\begin{equation}
\label{eq:clarke_F}
\clarke F(x) = \{ \nabla f_{I^+(x)} + \sum_{i \in I^0(x)} \lambda_i \nabla f_i, 0 \leq \lambda_i \leq 1\}.
\end{equation}
The minimum of $\lambda \mapsto \vvnorm{\nabla f_{I^+(x)} + \sum_{i \in I^0(x)} \lambda_i \nabla f_i}$ is attained at a unique point. 
\toblue We denote its coordinates by $(\alpha_i)_{i \in I^0(x)}$.
\end{proposition}

\begin{proof}
Recall that the Clarke gradient of a Lipschitz function is obtained as
the convex hull of limits of nearby gradients
\cite{clarke1997nonsmooth}.  Denote by $B$ the set on the
right-handside of Eq. \ref{eq:clarke_F}. $B$ is a \rred{box} whose
vertices are $\nabla f_{I^+(x) \cup A}(x) = \nabla f_{I^+(x)}(x) +
\nabla f_A(x)$ where $A$ ranges among the subsets of $I^0(x)$ (with
the convention that $\nabla f_{\emptyset} = 0$). These are also
exactly points determining the convex hull in the definition of
$\clarke F(x)$.

Recall from Lemma \ref{lem:free_gradient} that gradients $(\nabla
f_i)_{i \in I^0(x)}$ form a free family of vectors. It follows that
the map 
$\lambda \mapsto \vvnorm{\nabla f_{I^+(x)} + \sum_{i \in    I^0(x)} \lambda_i \nabla f_i}^2$ 
is strictly convex and thus admit only one minimizer on the convex set $[0,1]^n$.
\end{proof}


\begin{figure}[htb]% or !htb or H
\centerline{\includegraphics[width=.8\linewidth]{\wfig/illustr_degen.pdf}}
\caption{\toblue {\bf Clarke gradient and generalized gradient.}
The Clarke gradient (Eq. \ref{eq:clarke-qp})  as  a convex hull/box.
The generalized gradient is the projection of the origin onto that convex hull. 
{\bf (Left)} Example of Clarke gradient with $I^0= \{ 1,2\}$, and $I^0_{*} = \{ 1\}$. 
{\bf (Right)} Clarke gradient lying in a degenerate position--orthogonality between the red ($\nabla F_*$) and black ($\nabla f_1$) segments.
}
\label{fig:clarke-gradient} 
\end{figure} 

\toblue
Recall the definition of the sets $I^0_*(x), I^+_*(x), I^+_*(x)$ in
Eq. \ref{eq:IzIpIm}, from which one defines the cell
$\calC^*(x)$ of the arrangement containing $x$. We say that the
Clarke QP problem is non-degenerate when $I^0_*(x) = \{ i \in I^0(x),
0 < \alpha_i < 1 \}$ coincides with $ \{ i \in I^0(x), \scal{\gengrad  F(x)}{\nabla f_i(x)} = 0 \}$. 
%%
We measure the degeneracy of this problem with the quantity
\begin{equation}
%\mathrm{deg}( \clarke F(x) ) 
\degenparam{ \clarke F(x) }
:=  \min \{ \left | \scal{\gengrad F(x)}{\nabla f_i(x)} \right |, i \in I^0(x) \text{ such that } \alpha_i \in \{ 0, 1 \} \},
\end{equation}
which is zero if and only if the QP problem is degenerate. Note that
boxes of $\Rd$ are generically non-degenerate, and in practice our
algorihm never stumbles onto degenerate configurations for the Clarke
gradient.
%%
\begin{proposition}
\label{prop:qp-non-denenerate}
 If the Clarke QP problem is non-degenerate, the exact gradient flow
 of $F$ begins by a curve inside $\calC^*(x)$ on which the Clarke QP
 problem stays non-degenerate.
\end{proposition}

\toblack
\begin{proof}
We will show that for $t$ positive and small enough, the semiflow
trajectory $t \mapsto x(t)$ starting at $x$ lies in $\calC^*(x)$ by
showing separately that
\begin{itemize}
\item $x(t)$ stays in $S_{I^0_*(x)}$ for $t$ small enough and positive;
\item $I^+(x(t))= I^+_*(x)$ and $I^-(x(t)) = I^-_*(x)$ for $t$ small enough.  
\end{itemize}

\toblue
Remark that the coefficients $\alpha_i$ defining $\nabla F_*(x)$ are
obtained through the affine projection of $0$ onto the affine set
generated by $\nabla f_{I^+_*(x)} + \nabla f_i(x)$ where $i$ ranges
among $I^0_*(x)$ by the non-degenerate condition.  The
coordinates $\lambda_i(x)$ of the affine projections are obtained as
solutions of the following non-singular linear system:
\begin{equation}
\sum_{i \in I^0_*(x)} \lambda_i (x) \scal{\nabla f_i (x)}{ \nabla f_j (x)} = - \scal{\nabla f_{I^+_*(x)}}{ \nabla f_j (x)} \qquad \text{ for every } j \in I^0_*(x).
\end{equation}



Equivalently, letting $A$ be the full-rank matrix whose rows are
$(\nabla f_i(x))_{i \in I^0_*}$, $\lambda$ is a solution to
\begin{equation*}
\latrans{A(x)} A(x) \lambda(x) = - \latrans{A(x)} \nabla f_{I^+_*}(x),
\end{equation*}
and the corresponding generalized gradient is equal to 
$\phi(x) = A(x)\lambda(x) + \nabla f_{I^+_*(x)}(x)$. Since $x \mapsto A(x)$ is a
smooth map, the ODE $z'(t) = -\phi(z(t))$ starting with $z(0) = x$
admits a solution.

This solution coincides with the exact gradient flow for small $t$, as
by continuity we still have 
\begin{equation}
 0 < \lambda_i(z(t)) < 1,
\end{equation}
so that the point of least norm in the affine space generated by
vectors $\nabla f_{I^+_*(z(t))} + \nabla f_i(z(t))_{i \in I^0_*(z(t))
  \cup \{ \emptyset \}}$ coincides with the point of least norm within
the box generated by the same vectors.  This ensures
that for small positive $t$ and any $i \in I^0_*(z(t))$ we have
\begin{equation}
\scal{\nabla f_i(z(t))}{z'(t)} = 0,
\end{equation}
which warrants that $f_i(z(t))$ is constant and equal to zero, i.e., that the trajectory $z(t)$ stays on the intersection of
spheres $S_{I^0_*(x)}$.\\

Moreover, for $t$ small enough we have $I^-(z(t)) = I^-(x) \cup \{ i
\in I^0(x), \lambda_i(x) = 0 \}$. Indeed, if $\lambda_i(x) = 0$ with $i \in I^0(x)$, by virtue of the generalized gradient being the element of smaller norm in $\clarke F(x)$, we  have for every $s \in [0, 1]$ the inequality $\vvnorm{ \nabla_* F(x) + s \nabla f_i(x) }^2 \geq \vvnorm{ \nabla_* F(x)}^2$. As a function of $s$, the right derivative at zero must be positive so that:
\begin{equation*}
 \scal{-\nabla_*F(x)}{\nabla f_i(x)} \leq 0.  
\end{equation*}
%%
Since the Clarke gradient is non-degenerate, this previous quantity is
in fact negative. Seeing $z'(t) = -\phi(z(t))$ is continuous, the derivative of $f_i(z(t))$ is negative for $t$ small enough. The same holds
symmetrically, so that $I^+(z(t)) = I^+(x) \cup \{ i \in I^0(x),
\lambda_i(x) = 1 \}$. This proves that the Clarke gradient $\clarke
F(z(t))$ is the box generated by the previous vectors
$\nabla f_{I^+_*(x)}(x) + \nabla f_i(x)$, where $i$ ranges among
$I^0_*(x)$.
\end{proof}
\toblack

\subsection{Procedures and their justification}

\algname{\algline}
\begin{algorithm}[H]
\caption{Compute the first point along the semi-line $x+tu$, $t > 0$ changing cell. Compute the cell to which this point belongs.}
\label{alg:LineDescent}
\begin{algorithmic}
%%\Procedure{\algline}{}%params}
\Require{A set of centers $c_i$ and radii $R_i$, an initial point $x$, a directing vector $u$, sets of indices $I^+, I^0, I^-$.}
\Ensure{$z = x +tu$ is the first point along the semi-line $x+tu$, $t > 0$ changing cell. $I^+, I^0, I^-$ are the set of indices at $z$.} 
\State $t \gets + \infty$
%\State Compute $I^0(x), I^+(x), I^-(x)$
%donnés en entrée
\State ChangingIndices $\gets\emptyset$ \Comment{{\togray Set of indices in $I^+ \cup I^-$ to which $z$ belongs - usually a singleton.}}
\For{$i \in I^+ \cup I^-$}
 \State $t_i \gets$  Smallest positive solution to $\vvnorm{x + tu - c_i}^2 = R_i^2$ (with $t_i = \infty$ if no solution).
 \If{$t_i = t$ and $t_i \neq \infty$}
 	\State Add $i$ to ChangingIndices
 \ElsIf{$t_i < t$}
 	\State ChangingIndices $\gets \{ i \}$.
 	\State $t \gets t_i$.
 \EndIf
 \EndFor 
 \State Add elements of ChangingIndices to $I^0$, Remove elements from ChangingIndices off of $I^+$, $I^-$.
\State \Return{$z = x + tu$, $I^0$, $I^+$, $I^-$}
%%\EndProcedure
\end{algorithmic}
\end{algorithm}

The \algsphere procedure is less straightforward. It needs two subprocedures: \algsphereinter and \algminsphereinter. The first one is done by solving for a linear system.

\begin{lemma}[Computations in the \algsphereinter procedure.]
\label{lem:sphere_intersection}
Let $2 \leq j \leq d$ and let $S_1, \dots S_j$ be spheres in $\Rd$ with centers $c_1, \dots, c_j$ (in generic position) and radii $R_1, \dots, R_j$ intersecting transversely.
Then $S = \bigcap_{1 \leq i \leq j} S_i$ is manifold of dimension $d-j-1$ described as the intersection of a sphere of radius $R_S$ and center $c_S$ intersected with an affine subspace given by the $j-1$ equations
\begin{equation}
\scal{x}{(c_1 - c_i)} = K_i,
\end{equation}
for all $2 \leq i \leq j$ with $K_i := \frac{1}{2} \left (R_1^2 - \vvnorm{c_1}^2 - R_i^2 + \vvnorm{c_i}^2 \right )$.
Moreover, the center $c_S$ is the only convex combination of $c_1, \dots, c_j$ belonging to this affine subspace, and it is obtained as the solution of the linear system with $j$ equations and $j$ unknowns:
\begin{equation}
\label{eq:centre_sphere}
\left \{
\begin{array}{rlr}
\sum_{i} \lambda_i \scal{c_i}{(c_1 - c_k)} & = K_k & \text{ for every } 2 \leq k \leq j; \\
\sum_{i} \lambda_i & = 1. &
\end{array}
\right.
\end{equation}
 The radius $R_S^2$ verifies for every $i$ in $\{1, \dots, n\}$ 
\begin{equation}
\vvnorm{c_i - c_S}^2 + R_S^2 = R_i^2.
\end{equation}
\end{lemma}

\begin{proof}
Let $c_S$ be the solution of the Eq. \ref{eq:centre_sphere}. Denote by $A$ the affine space defined by equations (on $u$) $\scal{u}{(c_1 - c_k)} = K_k$ for every $2 \leq k \leq j$, to which belong any point of $S$ by subtracting equations $\vvnorm{x - c_i}^2 = R_i^2$ to $\vvnorm{x - c_1}^2 = R_1^2$. Then $c_S$ is the point at the intersection of $A$ and the affine hull of every $c_i$. For any $x$ in $A$ by the Pythagorean theorem one has $\vvnorm{x - c_i}^2 = \vvnorm{x - c_S + c_S - c_i}^2 =\vvnorm{x - c_S}^2 + \vvnorm{c_S- c_i}^2$. One can check that $R_i^2 - \vvnorm{c_i - c_S}^2$ is a positive number independent of $i$, whose square root we define to be $R_S$. Thus $x$ belongs to $S$ if and only if $x \in A$ and $\vvnorm{x - c_S}^2 = R_S^2$.
\end{proof}



\algname{\algsphereinter}
\begin{algorithm}[H]
\caption{
 Compute the center and radius of the $d- \#I$ dimensional sphere given by the intersection of transverse spheres $S_I$.}
\label{alg:SphereIntersections}
\begin{algorithmic}
%%\Procedure{name}{params}
\Require{A set of indices $I$ with corresponding centers $c_i$ and radii $R_i$}
%%, a center $c$ such that $\vvnorm{x-c}^2$ is to be minimized over $S$}
%\Ensure{$y \in S$ is the point where $x \mapsto \vvnorm{x - c}^2$ is minimal over $S$.}
\State $\lambda \gets$ solution of the system of Eqs. \ref{eq:centre_sphere}
\State $c_S \gets \sum_{i \in I} \lambda_i c_i$
\State $R_S \gets \sqrt{R_1^2 - \vvnorm{c_1 - c_S}^2}$
\State \Return{$c_S, R_S$}
%%\EndProcedure
\end{algorithmic}
\end{algorithm}

The second one requires a projection onto an affine hull.

\begin{lemma}[Computations in the \algminsphereinter procedure.]
Let $S = \cap_{1 \leq i \leq j} S_i$ be an intersection of $j$ spheres in $\Rd$ intersecting transversely with centers in a generic position. Let $g$ be a map of the form $x \mapsto \alpha \left ( \vvnorm{x -c}^2 - R^2 \right )$ with $c \notin S$. Then $g_{|S}$ is a smooth map whose differential vanishes on $S$ exactly at point $c_S \pm R_S \frac{b}{\vvnorm{b}}$ where $b := c - \pi(c)$, $\pi$ being the projection map onto the closest point in the affine subspace generated by the centers $c_1, \dots, c_j$. These points are the argmin and the argmax of $g_{|S}$.
\end{lemma}

\begin{proof}
Without loss of generality, take $\alpha = 1/2$ and $R=0$.
For any $x \in S$, $\nabla g_{|S}(x)$ is the orthogonal projection of $\nabla g(x) = x - c$ onto the tangent space $T_x S$. This space consists in a $(d-j)$-dimensional vector subspace of $\Rd$ defined by the following equations on $v$:
\begin{equation}
\scal{v}{(x - c_S)} = 0 \qquad  \text{ and } \qquad \scal{v}{(c_1 - c_i)} = 0 \text{ for all }2 \leq i \leq j.
\end{equation}
By the change of variable $x = c_S + R_Su$ and by the previous proposition, $S$ can be parameterized by $u$ ranging in the unit sphere orthogonal to all the $c_1 - c_j$. All in all $T_x S$ is the space orthogonal to $(c_1 - c_i)_{2 \leq i \leq j}, u$. The orthogonal projection onto such a space is zero if and only if it belongs to the space spanned by these vectors. Thus the differential vanishes in $x$ if and only if 
\begin{align*}
x - c = c_S + R_S u - c \in \Vect(c_1 - c_i, u).
\end{align*}
which after some manipulation and seeing that $c_S$ is a convex combination of every $c_i$, is equivalent to 
\begin{equation}
\label{eq:appartenance}
c - c_1 \in \Vect(c_1 - c_i, u).
\end{equation}
They are exactly two unit vectors orthogonal to every $c_1 - c_i$ such that the previous equation is verified. Indeed, write $c - c_1 = a + b$ where $a \in \Vect(c_1 - c_i)$ and $b$ is orthogonal to them, which is equivalent to $b$ being $c - \pi(c)$ as defined in the proposition. The previous equation \ref{eq:appartenance} is verified if and only if $u = \pm \frac{b}{\vvnorm{b}}$. 
The argmin and the argmax being two distinct critical points of $g_{|S}$, they are necessarily these two points.
\end{proof}


\algname{\algminsphereinter}
\begin{algorithm}
\caption{Compute the minimum $y$ of a function of the type $\vvnorm{x - c}^2$ on a transverse intersection of spheres $S = \cap_{i \in I} S_i$.}
\label{alg:MinSphereIntersections}

\begin{algorithmic}
%%\Procedure{name}{params}
\Require{A set of indices $I$ with corresponding centers $c_i$ and radii $R_i$, a center $c$ such that $\vvnorm{x-c}^2$ is to be minimized over the intersection $S$ of the spheres indexed in $I$}
%% \Ensure{$y \in S$ is the point where $x \mapsto \vvnorm{x - c}^2$ is minimal over $S$.}
\Ensure{$y = \argmin[x\in S] \vvnorm{x - c}^2$}
\State $(c_S, R_S) \gets$ $\algsphereinter{I}$
\State $\pi(c) \gets$ the projection of $c$ into the affine hull of $(c_i)_{i \in I}$.
\State $u \gets \frac{c - \pi(c)}{\vvnorm{c - \pi(c)}}$
\State $(y_1, y_2) \gets (c_S + R_Su,c_S - R_Su)$
\If{$\vvnorm{y_1  -c}^2 \geq \vvnorm{y_2 - c}^2$}
	\Return{$y_2$}
\Else \quad \Return{$y_1$}
\EndIf
%%\EndProcedure
\end{algorithmic}
\end{algorithm}


The following lemma shows how to compute the \algsphere procedure, that is, to compute when a geodesic $[x,y]$ starting from $x$ among an intersection of spheres changes cell. 
 
\begin{lemma}[Computations in the \algsphere procedure.]
Let $x,y$ be two non-antipodal points on a sphere of radius $R$ centered at $c$.
Then the change of signs of $f_i$ among the geodesic $[x,y]$ on the sphere are as follows.
\begin{itemize}
\item If $f_i(x)$ and $f_i(y)$ have different signs, there is exactly one point in $[x,y]$ where $f_i$ vanishes.
\item If $f_i(x)$ and $f_i(y)$ have same signs, there is either zero or two points in $[x,y]$ where $f_i$ vanishes.
\end{itemize}
Moreover, such intersection points are computable thanks to the following process. Put $\nu : x \mapsto \frac{x}{\vvnorm{x}}$
and let $u = \nu(x - c)$ and $v = \nu(y - c - (\scal{(y - c)}{u}) u)$, forming an orthonormal basis of $\mathrm{Vect}(x-c, y-c)$. Then $S_i$ intersects with $S \cap \mathrm{Aff}(c,x,y)$ at two points if and only if 
\begin{equation}
\label{eq:check}
\vvnorm{w}^2R^2 \geq D_i^2,
\end{equation} 
where $w^x_i = \scal{(c_i - c)}{u}$ and $w^y_i = \scal{(c_i - c)}{v}$, $w_i = w^x_i u + w^y_i v$ (and thus $\vvnorm{w_i}^2 = (w^x_i)^2 + (w^y_i)^2$), $D_i = \frac{1}{2} \left ( R_i^2 - \vvnorm{c_i - c}^2 - R^2 \right )$.
If Eq. \ref{eq:check} is verified, then these two points are of the form $C + su + tv$ with 
\begin{equation}
(s,t) = \frac{D_i}{\vvnorm{w}^2}(w^x_i, w^y_i) \pm \sqrt{R^2 - \frac{D_i^2}{\vvnorm{w_i}^2}}(-w^y_i, w^y_i).
\end{equation}
Furthermore, $c + su + tv$ lies on the geodesic $[x,y]$ on $S$ if and only if $\scal{(y-c)}{u} \leq s$ and $t \geq 0$.
\end{lemma}

\begin{proof}
Since by the definition of $v$ one has $\scal{(y-c)}{v} > 0$  we have $y - c = \scal{(y-c)}{u} + \sqrt{R^2 - (\scal{(y-c)}{u})^2}v$, and any point $c + su + tv$ lies on the spherical geodesic $[x,y]$ if and only if $s^2 + t^2 = R^2$ and $\scal{(y-c)}{u} \leq s, t \geq 0$. 
Now the orthogonal projection of $c_i$ onto the plane $P = \mathrm{Aff}(c,x,y)$ is obtained by $\Pi_{P}(c_i) = c + (\scal{(c_i -c)}{u})u + (\scal{(c_i - c)}{v})v = c + w_i$.
The intersection $S^P_i = S_i \cap P$ is either empty or a circle of radius $R_{i,P} = \sqrt{R_i^2 -  \vvnorm{c_i - c}^2 + \vvnorm{w_i}^2}$ within $P$, centered at $c + w_i$, depending on the sign of $R_i^2 -  \vvnorm{c_i - c}^2 + \vvnorm{w_i}^2$. $S^P_i$ and $S$ have non-empty intersection if and only if $\vvnorm{w_i} \leq R + R_{i,P}$.

Decomposed as $c + su + tv$,  the intersecting points are exactly the solutions of the quadratic system $s^2 + t^2  =  R^2,\vvnorm{su + tv - w_i}^2 = (R_{i,P}^2$ which is equivalent to:
\[
\left \{ 
\begin{array}{rcl}
s^2 + t^2 & = &  R^2 \\
sw_i^x + tw_i^y & = & D_i
\end{array} \right.
\]
where $D_i  = \frac{1}{2}\left (R^2 + \vvnorm{c - c_i}^2 - R_i^2 \right)$. It is simpler to see the solution $c + su + tv$ as $c + \alpha w_i + \beta w^{\perp}_i$, with $w_i^{\perp} = - w^y_i u + w^x_i v$. Then from the second equation of the last system we have $\alpha = \frac{D_i}{\vvnorm{w_i}^2}$ and $\beta = \pm \frac{1}{\vvnorm{w_i}} \sqrt{R^2 - \frac{D_i^2}{\vvnorm{w_i}^2}}$.
\end{proof}

\begin{lemma}[\algsphere decreases $F$]
Let $x$ be a point of $\Rd$ which does not minimize $F$, with $\#I^0_*(x) \geq 2$, $g = f_{I^+_*(x)}$. As per the previous proposition let $y$ be the point of $S = S_{I^+_*(x)}$ where $g_{|S}$ reaches its minimum. Let $[x,y]_{|S}$ be the geodesic between $x$ and $y$ on $S$. Let $\lambda$ be the minimum of $t$ such that the projection $x_t$ of $tx + (1-t)y$ on $S$ (or equivalently, onto $[x,y]_S$) has one of $I^+(x_t)$, $I^-(x_t)$ different from respectively $I^+_*(x)$ and $I^-_*(x)$. Then $\lambda$ is well-defined and we have 
\begin{equation}
F(x_{\lambda}) < F(x).
\end{equation}
\end{lemma} 

\begin{proof}
By Lemma \ref{lem:neighboring_cells}, we know that $x$ belongs to the closure of $\calC^*(x)$. Moreover for $t$ small enough, $x_t$ belongs to this cell as by definition $x_t$ stays on $S$. Now on the trajectory $t \mapsto x_t$, $F$ coincides with $f_{I^+_*}(x)$ which is a strictly convex function. Its restriction to $S$ has a unique minimum attained at $y$; thus $F$ decreases along the geodesic $[x,y]_S$. 
\end{proof}

\algname{\algsphere}
\begin{algorithm}[h!]
\caption{Compute the first point along the geodesic between $x,y$ on a Sphere $S_{I^0}$ of center $C$, radius $R$ leaving the cell and the cell to which this point belong.}
\label{alg:SphereDescent}
\begin{algorithmic}
%%\Procedure{name}{params}
\Require{A set of centers $c_i$ and radii $R_i$, an initial point $x$, destination $y$, sets of indices $I^+, I^0, I^-$.}
\Ensure{$z = C + su + \sqrt{R^2 - s^2} v$ is the first point along the geodesic between $x,y$ on a sphere leaving the cell.} 
\State $(s,t) \gets (-R,0)$
\State $u \gets \frac{x - C}{\vvnorm{x- C}}$
\State $v \gets y - C - (y - C) \cdot u$
\State $v \gets \frac{v}{\vvnorm{v}}$
\State ChangingIndices $\gets\emptyset$ \Comment{{\togray Set of indices in $I^+ \cup I^-$ to which the final $z$ belongs - usually a singleton.}}
\For{$i \in I^+ \cup I^-$}
 \State $D_i \gets \frac{1}{2}\left (R_i^2 - \vvnorm{c_i - C}^2 - R^2 \right )$
 \State $w^x_i \gets (c_i - C) \cdot u, w^y_i \gets (c_i - C) \cdot v$
 \State $w_i \gets (w^x_i)^2 + (w^y_i)^2$
 \State $\Delta_i \gets  R^2 - \frac{D_i^2}{w_i}$
  \If{$\Delta_i > 0$} \Comment{{ \togray If the circle on which the geodesic lies intersects with $S_i$}}
  %tag delta >0
    \State $s_+ \gets \frac{D_i}{w_i} w^x_{i} + \sqrt{\frac{\Delta_i}{w_i}}w^y_{i}, s_- \gets  \frac{D_i}{w_i} w^x_{i} - \sqrt{\frac{\Delta_i}{w_i}}w^y_{i}$
    \State $t_+ \gets \frac{D_i}{w_i} w^y_{i} - \sqrt{\frac{\Delta_i}{w_i}}w^x_{i}, t_- \gets  \frac{D_i}{w_i} w^x_{i} + \sqrt{\frac{\Delta_i}{w_i}}w^x_{i}$
    \State $L \gets$ set of pairs among $(s_+, t_+),(s_-, t_-)$ verifying $s_{\cdot} \geq s, t_{\cdot} \geq 0$.
    \State $(s_{temp}, t_{temp}) \gets$ the pair with maximal first element in $L$. 
    \If{ $s_{temp} = s$} \Comment{{ \togray Degenerate case where $z = c + su + tv$ (at that point in the algorithm) already lies on a sphere $S_i$ with $i \notin I^0$.}}     
    % tag s_temp = s
    \State Add $i$ to ChangingIndices
    \Else 
    \State ChangingIndices = $[i]$
	\State $(s,t) \gets (s_{temp}, t_{temp})$
    \EndIf %tag s_temp = s
   \EndIf %tag delta > 0
 \EndFor 
 \State Add elements of ChangingIndices to $I^0$, Remove elements from ChangingIndices off of $I^+$, $I^-$.
\State \Return{$z = C + su + tv$, $I^0$, $I^+$, $I^-$}
%%\EndProcedure
\end{algorithmic}
\end{algorithm}

\subsection{Exact algorithm : pseudocode and convergence result}



\algname{\algexact}
\vspace{-1em}
\begin{algorithm}[H]

\begin{algorithmic}
%%\Procedure{name}{params}
\Require{A set of centers $c_i$ and radii $R_i$, an initial point $x_0$}
\Ensure{$x$ is the point where the minimum of $F$ is attained} 
\State $x \gets x_0$, MinimumAttained $\gets$ False.
\State $I^+(x), I^0(x), I^-(x) \gets \sgns(x)$
\While{MinimumAttained is False}
%% tag k=0
\If{$\#I^0(x)$}\label{alg:kzero-done}
	
%% tag 1
\If{\textcolor{teal}{$c_{I^+(x)} \in \calC(x)$}} \Comment{\textcolor{gray}{Check if the minimum lies in $\calC(x)$.}}
\State 	\algtele procedure : MinimumAttained $\gets$ True, $x \gets c_{I^+(x)}$.
\Else
\State	\LComment{ \color{red} Compute the lowest $t$ such that $x -t\nabla f_{I^+(x)}$ is in another cell using the \hyperref[alg:LineDescent]{\algline} algorithm and update the signs. \label{alg:dim_pleine_départ}}
\State	$x, I^+, I^0, I^- \gets$ \algline($x,- \nabla f_{I^+(x)}$).
\EndIf %% end tag 1

\Else \Comment{\textcolor{gray}{$x$ lies on at least one $S_i$}} \label{alg:codim_pos}
\State \LComment{\color{violet} Compute $\gengrad F(x)$ and the sets of indices $I^+_*(x), I^0_*(x), I^-_*(x)$ via \algqp}
%% tag nabla
\State $I^+_*(x), I^0_*(x), I^-_*(x) \gets \algqp(x, I^+(x), I^0(x), I^-(x)).$ 
\If{\textcolor{violet}{$0 \in \clarke  F(x)$}} \Comment{\textcolor{gray}{Check if the minimum is attained at $x$.}}
\State	MinimumAttained $\gets$ True \label{alg:fin}
\ElsIf{$\#I^0_*(x) =0$ } \Comment{\textcolor{gray}{Traj. out of $x$ is in a full dimensional cell.}}

\If{\textcolor{teal}{$c_{I^+_*(x)} \in \calC^*(x)$}} \Comment{\textcolor{gray}{Check if the minimum lies in $\calC^*(x)$.}}
\State \algtele procedure : $x \gets c_{I^+_*(x)}$, MinimumAttained $\gets$ True
\Else 
\State \LComment{ \color{red} Compute the lowest $t$ such that $x -t\nabla f_{I^+_*(x)}$ is in another cell using the  \hyperref[alg:LineDescent]{\algline} algorithm and update the signs.}
\State $x, I^+, I^0, I^- \gets$ \algline($x,- \nabla f_{I^+_*(x)}$)  \label{alg:dim_pleine_traj}
\EndIf

\Else \Comment{\textcolor{gray}{Trajectory out of $x$ is \emph{not} in a full dimensional cell.}} \label{alg:codim_pos2}
\State \LComment{\color{olive} Compute $c_S, R_S$ respectively the center/radius of $S_{I^0_*(x)}$ via \algsphereinter.} 
\State $c_S, R_S \gets$ \algsphereinter$(I^0_*(x))$.
\State \LComment{\color{olive} Compute the argmin of $f_{|I^+_*(x)}$ on $S_{I^0_*(x)}$ using the \hyperref[alg:MinSphereIntersections]{\algminsphereinter} algorithm.}\label{alg:minimum}
\State $y \gets $ \algminsphereinter$(I^+_*(x), c_S, R_S)$
\If{\textcolor{teal}{$y \in \calC^*(x)$}}
\State $x \gets y$
\Else 
\State \LComment{\color{red} Compute the first intersection of the geodesic from $x$ to $y$ on $S$ with another cell using the \hyperref[alg:SphereDescent]{\algsphere} algorithm. Update the signs. \label{alg:codim_pos_traj}}
\State	$x, I^+, I^0, I^- \gets $ \algsphere($x,y,S$)
\EndIf
\EndIf %% tag nabla
\EndIf %% tag k=0
\EndWhile
\State \Return{$x$}
%%\EndProcedure
\end{algorithmic}
\caption{Compute the minimum of $F = \sum_{i} \max(0, \vvnorm{x-c_i}^2 - R_i^2)$.}
\label{alg:exact}
\vspace{0.1em}
\hrule
\vspace{0.5em}
\textcolor{red}{Red} = Computations taking the minimum of roots of second degree polynomials. \\
\textcolor{violet}{Violet} = Computing the solution of a quadratic programming problem. \\
\textcolor{olive}{Olive} = Computations solving a full-rank linear system and roots of degree two polynomials.\\
\textcolor{teal}{Teal} = Exact computations of predicates.
\end{algorithm}
%% xfc: some Compute are not colored

We now prove Thm. \ref{thm:convergence}, which we re-state for convenience:
%%
\begin{theorem}[Algorithm convergence]
%% There exists a neighborhood of the point $x^*$ where $F$ reaches its
%% minimum, such that for any starting point in this neighborhood the
%% algorithm converges in at most $3^c$ operations, where $c$ is the
%% number of spheres on which $x^*$ lies.
\thmStatement
\end{theorem}



\begin{proof}
At point $x^*$, the minimum of $F$ is reached meaning that its Clarke
gradient $\clarke F(x^*)$ contains 0. Recall that this Clarke gradient
$\partial_* F(x)$ of $F$ at any point $x$ is a box of the form
\[ \{ \nabla f_{I^+(x)}(x) + \sum_{i \in I^0(x)} \alpha_i \nabla f_i(x), 0 \leq \alpha_i \leq 1 \}\]
%%
Such a box has $3^c$
faces--of all dimensions, where $c$ is the cardinal of $I^0(x)$. For
any point $x$ with $I^0(x) \subset I^0(x^*)$, $I^+(x^*) \subset
I^+(x), I^-(x^*) \subset I^-(x)$, $\partial_* F(x)$ is a box of
smaller dimension than $\partial_* F(x^*)$, and it is similar to a
facet of the box $\partial_* F(x^*)$.

The underlying trajectory followed by our algorithm takes direction in
the Clarke gradient of $F$, so that the sequence $\vvnorm{x_n - x^*}$
is decreasing.  
%%
\toblue
Let $R_{min}$ be the maximum radius around the
minimizer $x^*$ with non-empty intersection only with cells touching
$x^*$. Since the distance of $x_n$ to $x^*$ is decreasing, $x_n$ stays
in the ball $B := B(x^*, R_{min})$.

By the convex nature of the functional our underlying semiflow
trajectory has necessarily decreasing norm of generalized gradient.
Indeed we have seen that the semiflow either (i) follows the flow of a
 convex function on an intersection of spheres or (ii) encounters a new
sphere, so that the dimension of the box of the Clarke gradient is
increased by 1. The new generalized gradient, which is the point
of least norm among the Clarke gradient, has necessarily smaller norm
(Fig. \ref{fig:trajectory-disjunction} for an illustration).

Each point $x_n$ in our sequence corresponds to the point where the
semiflow changes cell (situtation (ii) in the previous paragraph), so
that the sequence $\vvnorm{\gengrad F(x_n)}$ is strictly decreasing.

For any point $z$, we build a directed graph whose vertices are the
faces (of any dimension) of the box $\clarke F(z)$. There is one edge
between faces $C, C'$ is and only if one is contained in the other,
with dimension different than one. In this case, assuming $C' \subset
C$ the direction of the edge is determined by the following rule:
\begin{equation}
\label{eq:graph}
\begin{aligned}
\bullet\;& C' \mapsto C \quad \text{if $C$ is strictly closer to $0$ than $C'$,} \\
\bullet\;& C \mapsto C' \quad \text{if $C, C'$ lie at equal distance to $0$.}
\end{aligned}
\end{equation}
%%
This directed graph provides a well-ordering of the faces of
$\partial_* F(z)$ (Fig. \ref{fig:order}). 

Let $d$ be the infimum of $\degenparam{C}$ among all non-empty faces $C$ of $\clarke F(x^*)$. 
%
The points defining the vertices of the Clarke gradient box are each
$2n$-Lipschitz at most. Therefore, for any $z$ in the ball of radius
$\min(d/2n, R_{min})$ centered at $x^*$, the vertices have not moved
enough the change the directed graph associated to $\clarke F(z)$: it
corresponds to the subgraph of the directed graph associated to
$\clarke F(x^*)$ given by the vertices of indices $I^+(z),I^0(z),
I^-(z)$.



%%
%%
%% In $B$, the Clarke gradient of $F$ is a box of maximum dimension $c$,
%% determined by at most $n$ vectors of the form $\nabla f_i(z) = 2(z -
%% c_i)$, 2-Lipschitz as a function of $z$. In a ball of radius $R :=
%% \min(\frac{\alpha}{2n}, R)$, the order of $\gengrad F(z)$ is the same
%% as $\gengrad F(x^*)$.
%%
As the sequence $\vvnorm{\gengrad F(x_n)}$ is decreasing and each step
modifies the facet of $\clarke F(z)$ nearest to $0$, the sequence of
$x_n$ induces a decreasing sequence among the order of $\clarke
F(x^*)$. Hence the sequence has less element than the number of faces, i.e., $3^c$.
\end{proof}

\FloatBarrier

\begin{figure}[htb]% or !htb or H
\centerline{\includegraphics[width=.9\linewidth]{\wfig/fig_bon_ordre.pdf}}
\caption{\toblue {\bf Directed graph on faces of two boxes in $\mathbb{R}^2$.}
On these 2D examples, the faces have dimensions 0, 1 and 2. The arrows correspond
to the edges defined in \ref{eq:graph}. Note that a small change in the position of vertices would not change the directed graph, as the corresponding $d$ (infimum of $\degenparam{C}$ among all non-empty faces $C$ of the Clarke gradient) is positive.}
\label{fig:order} 
\end{figure} 

\begin{figure}[htb]% or !htb or H
\centerline{\includegraphics[width=.8\linewidth]{\wfig/fig_Clarke_illustr.pdf}}
\caption{\toblue {\bf Clarke gradient of $F$ of the last two steps of
    our algorithm.}  Between $x_3$ and $x_4$ the semiflow remains on
  $S_{ \{ 3 \}}$ and its norm is decreasing.  At $x_4$, the semiflow
  reaches a new sphere, so that the dimension of the box of the Clarke
  gradient increases by one, and the generalized gradient becomes 0:
  the critical point (minimum) has been reached.  Among the directed
  graph associated to $\clarke F(x_4)$ the last three steps were $v_4
  $ (semiflow before $x_3$) $\to e_4$ (from $x_3$) $\to \clarke
  F(x_4)$ (when $x_4$ is reached by the semiflow).  }
\label{fig:trajectory-disjunction}
\end{figure}  


\toblack
\begin{comment}
Aggregating the previous results, it is straightforward that $F(x_n)$ is a strictly decreasing sequence.
Now assume that the list is infinite. The sequence $(x_n)_{n \in \mathbb{N}}$ is bounded and injective as $F(x_n)$ is strictly decreasing, without encountering any \algtele procedure.
There cannot be a non-stationary accumulation point of the sequence $x_n$. Indeed, assume their is one which we denote by $x$.
\begin{itemize}
\item  Suppose said accumulation point is the argmin of $F$. When $x_n$ is close enough to $x$, the semiflow trajectory coincides with that underlying our algorithm. This trajectory being in our case a semi-algebraic curve \cite{gradient_descent_o_minimal}, it has a finite number of steps, leading to a contradiction. 

%% \item
%% Suppose said accumulation point is not the argmin of $F$. For any non
%% minimal $z$, there is a positive $\eps$ such that for any $y$ at
%% within distance $\eps$ of $z$, at any step, the procedure used picks a
%% direction very close to be in the Clarke gradient of $F$ at $z$;
%%
%% In particular the changes of cell only happen in the directions of the
%% generalized gradient $\gengrad F(z)$. Around $x$, as there is a only a
%% finite number of cells, there is a finite number of steps until $x_n$
%% either reaches $x$ or such that $F(x_n) < F(x)$, both being
%% contradictory to our assumption.
\item
Since $x$ is non minimal, by the semi-continuity of the Clarke
gradient, there exists $\eps$ such that for each $y$ at distance at
most $\eps$ from $x$, the direction picked by our procedure is close
to be in the Clarke gradient of $x$.
%%
In particular, the individual functions $f_i$ undergoing sign changes
are those contributing to the Clarke gradient.  As a consequence, the
number of sign changes until either $x$ is reached of the sequence
$(x_n)$ exits the neighborhood (while retaining function values
$<F(x)$) is finite.
\end{itemize}
\end{comment}

\begin{comment}
\begin{figure}[htb]% or !htb or H
\centerline{ \includegraphics[width=.5\textwidth]{\wfig/k-subspaces-arrangement/arrangement-3pts-montage.pdf}}
\caption{{\bf Spherical clusters: sinks and associated arrangement on a 2D toy example
with three points $x_1, x_2, x_3$.}
{\bf (Sinks)}  Each circle represents the  sink regions $B_{x_i}$ of a data point $x_i$.
%%
{\bf (Arrangement)} These circles induce decomposition of the square
into 2D cells (colored pieces of disks), 1D cells (circle arcs), and 0D cells (black points). }
%% This decomposition is associated with a na Each colored surface is a
%% cell of dimension 2, each circular arc is a cell of dimension 1, and
%% each black and white point represents a cell of dimension 0.
\label{fig:sink-arrangement} 
\end{figure} 
\end{comment}	
