discretize.utils.cartesian_to_cylindrical(grid, vec=None)[source]#

Transform from cartesian to cylindrical coordinates.

Transform a grid or a vector from Cartesian coordinates \((x, y, z)\) to cylindrical coordinates \((r, \theta, z)\).

grid(n, 3) array_like

Location points defined in Cartesian coordinates \((x, y z)\).

vec(n, 3) array_like, optional

Vector defined in Cartesian coordinates. This also accepts a flattened array with the same total elements in column major order.

(n, 3) numpy.ndarray

If vec is None, this returns the transformed grid array, otherwise this is the transformed vec array.


Here, we convert a series of vectors in 3D space from Cartesian coordinates to cylindrical coordinates.

>>> from discretize.utils import cartesian_to_cylindrical
>>> import numpy as np

Create set of vectors in Cartesian coordinates

>>> r = np.ones(9)
>>> phi = np.linspace(0, 2*np.pi, 9)
>>> z = np.linspace(-4., 4., 9)
>>> x = r*np.cos(phi)
>>> y = r*np.sin(phi)
>>> u = np.c_[x, y, z]
>>> u
array([[ 1.00000000e+00,  0.00000000e+00, -4.00000000e+00],
       [ 7.07106781e-01,  7.07106781e-01, -3.00000000e+00],
       [ 6.12323400e-17,  1.00000000e+00, -2.00000000e+00],
       [-7.07106781e-01,  7.07106781e-01, -1.00000000e+00],
       [-1.00000000e+00,  1.22464680e-16,  0.00000000e+00],
       [-7.07106781e-01, -7.07106781e-01,  1.00000000e+00],
       [-1.83697020e-16, -1.00000000e+00,  2.00000000e+00],
       [ 7.07106781e-01, -7.07106781e-01,  3.00000000e+00],
       [ 1.00000000e+00, -2.44929360e-16,  4.00000000e+00]])

Compute equivalent set of vectors in cylindrical coordinates

>>> v = cartesian_to_cylindrical(u)
>>> v
array([[ 1.00000000e+00,  0.00000000e+00, -4.00000000e+00],
       [ 1.00000000e+00,  7.85398163e-01, -3.00000000e+00],
       [ 1.00000000e+00,  1.57079633e+00, -2.00000000e+00],
       [ 1.00000000e+00,  2.35619449e+00, -1.00000000e+00],
       [ 1.00000000e+00,  3.14159265e+00,  0.00000000e+00],
       [ 1.00000000e+00, -2.35619449e+00,  1.00000000e+00],
       [ 1.00000000e+00, -1.57079633e+00,  2.00000000e+00],
       [ 1.00000000e+00, -7.85398163e-01,  3.00000000e+00],
       [ 1.00000000e+00, -2.44929360e-16,  4.00000000e+00]])