amep.utils.unit_vector_3D#
- amep.utils.unit_vector_3D(theta: float | ndarray, phi: float | ndarray) ndarray #
Generates 3D unit vectors with components
\[ \begin{align}\begin{aligned}x = \sin(\theta)\cos(\phi)\\y = \sin(\theta)\sin(\phi)\\z = \cos(\theta).\end{aligned}\end{align} \]- Parameters:
theta (float or np.ndarray) – Angle in degrees. A single value can be given as a float or multiple values as an array of shape (N,).
phi (float or np.ndarray) – Angle in degrees. A single value can be given as a float or multiple values as an array of the same shape as theta.
- Returns:
Unit vector(s).
- Return type:
np.ndarray
Examples
>>> import amep >>> import numpy as np >>> import matplotlib.pyplot as plt >>> print(amep.utils.unit_vector_3D(60, 60)) >>> theta = np.array([0, 30, 60, 90, 120, 150, 180]) >>> phi = np.array([0, 60, 120, 180, 240, 300, 360]) >>> t, p = np.meshgrid(theta, phi) >>> vectors = amep.utils.unit_vector_3D( ... t.flatten(), p.flatten() ... ) >>> fig = plt.figure() >>> axs = fig.add_subplot(111, projection='3d') >>> axs.quiver( ... 0, 0, 0, vectors[:,0], vectors[:,1], ... vectors[:,2], length=0.05 ... ) >>> axs.set_xlabel(r'$x$') >>> axs.set_ylabel(r'$y$') >>> axs.set_zlabel(r'$z$') >>> fig.savefig('./figures/utils/utils-unit_vector_3D.png') >>>