Does anyone know of a well-tested, reliable implementation of hyper-spherical coordinates, converting Cartesian vectors to spherical angles? I'd prefer python but I can translate it if necessary.
In other words, essentially the opposite of this question.
Before you tell me it's trivial to implement, here is my implementation (for 5 dimensional space):
def theta(x):
n = 5
x = np.array(x)
toFill = np.array([0.0, 0.0, 0.0, 0.0])
r_array = np.sqrt( np.array( [ sum( [xj**2 for xj in x[i+1:]] ) for i in range(0,n-1) ] ) )
for k in range(0,n-2):
toFill[k] = np.arctan2( r_array[k] , x[k] )
toFill[n-2] = 2 * np.arctan2( x[n-1] , ( x[n-2] + np.sqrt(x[n-1]**2 + x[n-2]**2) ) )
return toFill
Note that I only care about the angles, not the radius. The implementation is essentially from here.
It seems to work, but I'm getting some odd results in some simulations (which I think may be due to this method, though I haven't found the problem directly yet). If you can see some boundary condition issues here, please let me know.
You can use hyperspherical python package.
import numpy as np
from hyperspherical import cartesian2spherical, spherical2cartesian
# Convert Cartesian to spherical
cartesian_points = np.array([[1, 1, 1], [0, 1, 0], [1, 0, 0]])
spherical_points = cartesian2spherical(cartesian_points)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With