Solution (source code)

= Solution

The three spherical <basis vectors> form an <orthonormal basis>: their displayed Cartesian components have unit lengths and pairwise zero <dot products>. For an <orthonormal basis>, a <vector> is the sum of its projections onto the <basis vectors>. Taking the <dot product> of each Cartesian <unit vector> with each spherical <unit vector> therefore gives the inverse <change of basis>:
$$
\boxed{\begin{aligned}
\mathbf i&=\cos\phi\sin\theta\,\mathbf e_r+\cos\phi\cos\theta\,\mathbf e_\theta-\sin\phi\,\mathbf e_\phi,\\
\mathbf j&=\sin\phi\sin\theta\,\mathbf e_r+\sin\phi\cos\theta\,\mathbf e_\theta+\cos\phi\,\mathbf e_\phi,\\
\mathbf k&=\cos\theta\,\mathbf e_r-\sin\theta\,\mathbf e_\theta.
\end{aligned}}
$$
Equivalently, the <change-of-basis matrix> is <orthogonal>, so its <matrix inverse> is its transpose. These formulas use the stated angular convention for <spherical polar coordinates>; at a pole the tangential <basis vectors> still depend on the chosen azimuth.