Re: coordinate and vector transformations between cartesian and cylindrical coordinate system

"Schuldei, Andreas" <[email protected]> Mon, 12 Jul 2021 15:40:16 +0000
Newsgroups gmane.comp.python.scientific.user
Message-ID <[email protected]>
Hi Guillaume,


I had come up with this


# coordinate transformation from cylindrical to cartesian coordinates
def coordinate_transform_cy2ca(r, theta, z):
    x = np.cos(theta) * r
    y = np.sin(theta) * r
    return x, y, z


# coordinate transformation back
def coordinate_transform_ca2cy(x: np.ndarray, y: np.ndarray, z: np.ndarray):
    r = np.sqrt(np.square(x) + np.square(y))
    theta = np.arctan2(x, y)
    return r, theta, z


# vector transformation from cylindrical to cartesian coordinate system
def vector_transform_cy2ca(v_r, v_theta, v_z, R, T, Z):
    x = v_r * np.cos(T) - R * np.sin(T) * v_theta
    y = v_r * np.sin(T) + R * np.cos(T) * v_theta
    return x, y, v_z


# vector transformation back
def vector_transform_ca2cy(v_x, v_y, v_z, X, Y, Z):
    sq_devisor = np.square(X) + np.square(Y)
    devisor = np.sqrt(sq_devisor)
    r = np.divide((v_x * X + v_y * Y), devisor, out=np.zeros_like(X, dtype=float), where=devisor != 0)
    theta = np.divide((-Y * v_x + v_y * X), sq_devisor, out=np.zeros_like(Y, dtype=float), where=sq_devisor != 0)
    return r, theta, v_z


# translate (aka move origin) of coordinates (in a meshgrid or otherwise) by vector.
# axis orientation and scaling remains the same
def coordinate_translate_ca_by_vector(x: np.ndarray, y: np.ndarray, z: np.ndarray, vector: np.ndarray) -> \
        Tuple[np.ndarray, np.ndarray, np.ndarray]:
    x_t: np.ndarray = x - vector[0]
    y_t: np.ndarray = y - vector[1]
    z_t: np.ndarray = z - vector[2]
    return x_t, y_t, z_t



based on https://mathepedia.de/Zylinderkoordinaten.html


However, as you can guess from my gymnastics with the !=0 in the vector_transform_ca2cy(), there are issues if you need to transform vectors on the z-axis. Incidentally, those are quite important to me and I had hoped that a well-done library would have found a better solution than what I came up with -- which frankly does not work all that well.


Furthermore, here http://www.uobabylon.edu.iq/eprints/paper_11_24775_76.pdf (e.g. page 36/37) I find confirmation that vector transformation is not the same as coordinate transformation. However, it disagrees a little with my vector transformation based on https://mathepedia.de/Zylinderkoordinaten.html. So I would have liked a battle-tested lib like scipy that I can rely on.


Is anyone familiar with the topic who can share some light on this?


Andreas

________________________________
Von: SciPy-User <[email protected]> im Auftrag von Guillaume Gay <[email protected]>
Gesendet: Montag, 12. Juli 2021 16:46:33
An: [email protected]
Betreff: Re: [SciPy-User] coordinate and vector transformations between cartesian and cylindrical coordinate system


Hi Andreas?

What exactly do you have in mind? If your positions are stored as a (n, 3) array of points, Cartesian to cylindrical is (for example):

rho = np.linalg.norm(pos[:, :2], axis=1)
theta = np.arctan2(pos[:, 1], pos[:, 0])
z = pos[:, 2]


Conversely if you have three cylindrical coordinates (rho, theta, z), you can get the Cartesian with:


x = pos[:, 0] * np.cos(pos[:, 1])
y = pos[:, 0] * np.sin(pos[:, 1])
z = pos[:, 2]


Hope this helps,

Best regards,
Guillaume

On 12/07/2021 16:03, Schuldei, Andreas wrote:

Hi,


I am looking for ways to do coordinate and vector transformations between cartesian and cylindrical coordinates. Scipy has scipy.spatial.transform<https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.transform.Rotation.__len__.html>, but no ready (easy?) way to do the transformations I look for. Is there a way to use spatial.transform for this that I don't see? Transformations this common surely are done frequently, but I fail to find examples that I can learn from or reuse.


Kind regards,


Andreas



_______________________________________________
SciPy-User mailing list
[email protected]<mailto:[email protected]>
https://mail.python.org/mailman/listinfo/scipy-user


​

_______________________________________________
SciPy-User mailing list
[email protected]
https://mail.python.org/mailman/listinfo/scipy-user