# rot_a_to_b¶

plasmapy.formulary.rot_a_to_b(a: numpy.ndarray, b: numpy.ndarray)numpy.ndarray

Calculates the 3D rotation matrix that will rotate vector a to be aligned with vector b. The rotation matrix is calculated as follows. Let

$\vec v = \vec a \times \vec b$

and let $$\theta$$ be the angle between $$\vec a$$ and $$\vec b$$ such that the projection of $$\vec a$$ along $$\vec b$$ is

$c = \vec a \cdot \vec b \cos\theta$

Then the rotation matrix $$R$$ is

$R = I + v_x + v_x^2 \frac{1}{1 + c}$

where $$I$$ is the identity matrix and $$v_x$$ is the skew-symmetric cross-product matrix of $$v$$ defined as

$\begin{split}v_x = \begin{bmatrix} 0 & -v_3 & v_2 \\ v_3 & 0 & -v_1 \\ -v_2 & v_1 & 0 \end{bmatrix}\end{split}$

Note that this algorithm fails when $$1+c=0$$, which occurs when $$a$$ and $$b$$ are anti-parallel. However, since the correct rotation matrix in this case is simply $$R=-I$$, this function just handles this special case explicitly.

This algorithm is based on this discussion on StackExchange.

Parameters
• a (ndarray, shape (3,)) – Vector to be rotated. Should be a 1D, 3-element unit vector. If a is not normalize, then it will be normalized.

• b (ndarray, shape (3,)) – Vector representing the desired orientation after rotation. Should be a 1D, 3-element unit vector. If b is not normalized, then it will be.

Returns

R – The rotation matrix that will rotate vector a onto vector b.

Return type

ndarray, shape (3,3)