I am in the process of coding a function to convert Cartesian coordinates to hyperspherical coordinates. However, I've encountered some confusion regarding the transformation formulas.
On the Wikipedia page for N-sphere, the formula for hyperspherical coordinates is provided as shown in this image.
$$ \begin{align} r &= \sqrt{{x_n}^2 + {x_{n-1}}^2 + \cdots + {x_2}^2 + {x_1}^2} \\[6pt] \varphi_1 &= arccot \frac{x_{1}}{\sqrt{{x_n}^2+{x_{n-1}}^2+\cdots+{x_2}^2}} &&= \arccos \frac{x_{1}}{\sqrt{{x_n}^2+{x_{n-1}}^2+\cdots+{x_1}^2}} \\[6pt] \varphi_2 &= arccot \frac{x_{2}}{\sqrt{{x_n}^2+{x_{n-1}}^2+\cdots+{x_3}^2}} &&= \arccos \frac{x_2}{\sqrt{{x_n}^2+{x_{n-1}}^2+\cdots+{x_2}^2}} \\[6pt] &\,\,\,\vdots &&\,\,\,\vdots \\[6pt] \varphi_{n-2} &= arccot \frac{x_{n-2}}{\sqrt{{x_n}^2+{x_{n-1}}^2}} &&= \arccos \frac{x_{n-2}}{\sqrt{{x_n}^2+{x_{n-1}}^2+{x_{n-2}}^2}} \\[6pt] \varphi_{n-1} &= 2arccot \frac{x_{n-1}+\sqrt{x_n^2+x_{n-1}^2}}{x_n} &&= \begin{cases} \arccos \frac{x_{n-1}}{\sqrt{{x_n}^2+{x_{n-1}}^2}} & x_n\geq 0, \\[6pt] 2\pi - \arccos \frac{x_{n-1}}{\sqrt{{x_n}^2+{x_{n-1}}^2}} & x_n < 0. \end{cases} \end{align} $$
If I interpret this for a 3D point$(x, y, z)$, I deduce the following:
$$ \begin{align} r = \sqrt{x^2 + y^2 + z^2} \\ \varphi_1 = arccot\left(\frac{x}{\sqrt{y^2 + z^2}}\right) \\ \varphi_2 = 2 \times arccot\left(\frac{y + \sqrt{y^2 + z^2}}{z}\right) \\ \end{align} $$
However, when I refer to the Wikipedia page for the 3D spherical coordinate system, the formula seems different: $$ \begin{align} r &= \sqrt{x^2 + y^2 + z^2} \\ \theta &= \arccos\frac{z}{\sqrt{x^2 + y^2 + z^2}} = \arccos\frac{z}{r}= \begin{cases} \arctan\frac{\sqrt{x^2+y^2}}{z} &\text{if } z > 0 \\ \pi +\arctan\frac{\sqrt{x^2+y^2}}{z} &\text{if } z < 0 \\ +\frac{\pi}{2} &\text{if } z = 0 \text{ and } xy \neq 0 \\ \text{undefined} &\text{if } x=y=z = 0 \\ \end{cases} \\ \varphi &= sgn(y)\arccos\frac{x}{\sqrt{x^2+y^2}} = \begin{cases} \arctan(\frac{y}{x}) &\text{if } x > 0, \\ \arctan(\frac{y}{x}) + \pi &\text{if } x < 0 \text{ and } y \geq 0, \\ \arctan(\frac{y}{x}) - \pi &\text{if } x < 0 \text{ and } y < 0, \\ +\frac{\pi}{2} &\text{if } x = 0 \text{ and } y > 0, \\ -\frac{\pi}{2} &\text{if } x = 0 \text{ and } y < 0, \\ \text{undefined} &\text{if } x = 0 \text{ and } y = 0. \end{cases} \end{align} $$
I suspect I might be overlooking a detail or misunderstanding something. Could someone help clarify this discrepancy?