'More efficient method to convert XYZ coords into Spherical Coords

I have a script in Blender for plotting data points either in plane or spherical projection. However, the current method I have for converting my X,Y,Z coordinate for each vertex to spherical format is quite slow. Maybe some of you know of a more efficient method.

Essentially I have a (#verts,3) array of XYZ coordinates. Then I apply the following function over it.

def deg2rads(deg):
    return deg*pi/180

def spherical(row):
    x,y,z = [deg2rads(i) for i in row]
    new_x = cos(y)*cos(x)
    new_y = cos(y)*sin(x)
    new_z = sin(y)
    return new_x,new_y,new_z

polar_verts = np.apply_along_axis(spherical,1,polar_verts)

I believe apply_along_axis is not vectorized like other numpy operations. So maybe someone knows a better method? Now that I'm looking at it I think I can just vector multiply my verts to convert to rads. So that might shave a couple miliseconds off.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source