Details of Spherical Harmonics Computation
A spherical harmonics of degree \(l\) and order \(m\) is defined as
\[
Y_{l}^{m} (\theta, \phi) = K_{l}^{m} P_{l}^{m}(cos(\theta)) e^{im \phi}
\]
Where \(K_{l}^{m}\) is a normalization constant for each \(l\) and \(m\), and \(P_{l}^{m}\) is the associated Legendre polynomial of degree \(l\) and order \(m\).
Notice that the spherical coordinate \((r, \theta, \phi)\) follows the physics convention here.
\[
v(\theta, \phi) = (sin(\theta) cos(\phi), sin(\theta) sin(\phi), cos(\theta))
\]
Associated Legendre Polynomial
The associated Legendre polynomials are a set of orthogonal polynomials that satisfies
\[\begin{split}
\int_{-1}^{1} F_{m}(x) F_{n}(x) dx = \begin{cases}
0, \text{ if } m \ne n \\
c, \text{ if } m = n
\end{cases}
\end{split}\]
For \(P_{l}^{m}\), the polynomials are orthogonal w.r.t. a constant between degrees, and they are orthogonal w.r.t. another constant between orders. The details of orthogonality is not discussed in this notes, please read spherical harmonic lighting: the gritty details for details.
To derive \(P_{l}^{m}\), we need to follow three rules:
\((l-m) P_{l}^{m} = x (2l - 1) P_{l-1}^{m} - (l + m - 1)P_{l-2}^{m}\)
\(P_{m}^{m} = (-1)^{m} (2m - 1)!! (1 - x^{2})^{m/2}\)
\(P_{m+1}^{m} = x (2m + 1) P_{m}^{m}\)
Rule 1 can be used recursively to evaluate \(P_{l}^{m}\) for any \(l\) and \(m\) as long as \(l \ge m + 2\). And to get the first two orders of polynomials, we need to use rule 2 and rule 3 to bootstrap the algorithm.
In other words, for any \(P_{l}^{m}\),
If \(l = m\), use rule 2.
If \(l = m + 1\), use rule 3.
If \(l \ge m+2\), use rule 1.

Example
We can derived the 1st order (\(m=1\)) associated Legendre polynomial with the aforementioned algorithm
\[\begin{split}
\begin{align*}
P_{1}^{1} &= (-1) \cdot 1 \cdot (1 - x^{2})^{\frac{1}{2}}, \quad (\text{Rule 2}) \\
&= -(1 - x^{2})^{\frac{1}{2}} \\
P_{2}^{1} &= x \cdot 3 \cdot P_{1}^{1}, \quad (\text{Rule 3}) \\
&= -3x (1 - x^{2})^{\frac{1}{2}} \\
P_{3}^{1} &= \frac{x \cdot 5 \cdot P_{2}^{1} - 3 \cdot P_{1}^{1}}{2}, \quad (\text{Rule 1}) \\
&= \frac{3}{2} (1 - 5x^{2})(1 - x^{2})^{\frac{1}{2}}
\end{align*}
\end{split}\]
This checks out with the common associated Legendre functions listed on Wikipedia.
Analytical Solutions to Common Associated Legendre Functions
We also checked and use other associated Legendre functions listed on Wikipedia up to degree 3, since 3 degrees of spherical harmonics is accurate enough as mentioned in many literature.
\[\begin{split}
\begin{align*}
P_{0}^{0} &= 1 \\
P_{1}^{0} &= x \\
P_{1}^{1} &= -(1 - x^{2})^{\frac{1}{2}} \\
P_{2}^{0} &= \frac{1}{2} (3x^{2} - 1) \\
P_{2}^{1} &= -3x (1 - x^{2})^{\frac{1}{2}} \\
P_{2}^{2} &= 3 (1 - x^{2}) \\
P_{3}^{0} &= \frac{1}{2} (5x^{3} - 3x) \\
P_{3}^{1} &= \frac{3}{2} (1 - 5x^{2}) (1 - x^{2})^{\frac{1}{2}} \\
P_{3}^{2} &= 15x (1 - x^{2}) \\
P_{3}^{2} &= -15 (1 - x^{2})^{\frac{3}{2}}
\end{align*}
\end{split}\]
Evaluation of Spherical Harmonics
With the normalization factor
\[
K_{l}^{m} = \sqrt{\frac{2l + 1}{4 \pi} \frac{(l - m)!}{(l + m)!}}
\]
We evaluate the spherical harmonics equation
\[\begin{split}
Y_{l}^{m} (\theta, \phi) &= K_{l}^{|m|} P_{l}^{|m|}(cos(\theta)) e^{im \phi} \\
&= K_{l}^{|m|} P_{l}^{|m|}(cos(\theta)) (cos(m \phi) + sin(m \phi) i) \\
\end{split}\]
According to the Spherical Harmonics Blog and this post on StackExchange, the real form of spherical harmonics is given by:
\[\begin{split}
Y_{l}^{m}(\theta, \phi) = \begin{cases}
(-1)^{m} \sqrt{2} K_{l}^{-m} P_{l}^{-m}(cos(\theta)) sin(-m\phi), & m < 0 \\
K_{0}^{0} P_{0}^{0}(cos(\theta)), & m = 0 \\
(-1)^{m} \sqrt{2} K_{l}^{m} P_{l}^{m}(cos(\theta)) cos(m\phi), & m > 0 \\
\end{cases}
\end{split}\]
Spherical Harmonics in Cartesian Coordinate
It’ll be convenient to convert spherical coordinate to cartesian coordinate to evaluate spherical harmonics for normalized direction vector.
We start with
\[\begin{split}
\begin{align*}
cos(\theta) &= z \\
sin(\theta) &= \sqrt{1 - z^{2}} \\
cos(\phi) &= \frac{x}{sin(\theta)} \\
&= \frac{x}{\sqrt{1 - z^{2}}} \\
sin(\phi) &= \frac{y}{sin(\theta)} \\
&= \frac{y}{\sqrt{1 - z^{2}}}
\end{align*}
\end{split}\]
For all positive integer \(m\), we can expand \(cos(m \phi)\) and \(sin(m \phi)\) using Chebyshev polynomial
\[\begin{split}
\begin{align*}
cos(m \phi) &= \sum_{k=0}^{\lfloor {m/2} \rfloor} (-1)^{k} \binom{m}{2k} cos^{m-2k}(\phi) sin^{2k}(\phi) \\
sin(m \phi) &= \sum_{k=0}^{\lfloor {m-1/2} \rfloor} (-1)^{k} \binom{m}{2k+1} cos^{m-2k-1}(\phi) sin^{2k+1}(\phi)
\end{align*}
\end{split}\]
Analytical Solutions to Common Spherical Harmonics
\[\begin{split}
\begin{align*}
Y_{0}^{0} &= \frac{1}{2} \sqrt{\frac{1}{\pi}} \\
Y_{1}^{-1} &= \frac{1}{2} \sqrt{\frac{3}{\pi}} y \\
Y_{1}^{0} &= \frac{1}{2} \sqrt{\frac{3}{\pi}} z \\
Y_{1}^{1} &= \frac{1}{2} \sqrt{\frac{3}{\pi}} x \\
Y_{2}^{-2} &= \frac{1}{2} \sqrt{\frac{15}{\pi}} xy \\
Y_{2}^{-1} &= \frac{1}{2} \sqrt{\frac{15}{\pi}} yz \\
Y_{2}^{0} &= \frac{1}{4} \sqrt{\frac{5}{\pi}} (3z^{2} - 1) \\
Y_{2}^{1} &= \frac{1}{2} \sqrt{\frac{15}{\pi}} xz \\
Y_{2}^{2} &= \frac{1}{4} \sqrt{\frac{15}{\pi}} (x^{2} - y^{2}) \\
Y_{3}^{-3} &= \frac{1}{4} \sqrt{\frac{35}{2\pi}} y(3x^{2} - y^{2}) \\
Y_{3}^{-2} &= \frac{1}{2} \sqrt{\frac{105}{\pi}} xyz \\
Y_{3}^{-1} &= \frac{1}{4} \sqrt{\frac{21}{2\pi}} y(5z^{2} - 1) \\
Y_{3}^{0} &= \frac{1}{4} \sqrt{\frac{7}{\pi}} (5z^{3} - 3z) \\
Y_{3}^{1} &= \frac{1}{4} \sqrt{\frac{21}{2\pi}} x(5z^{2} - 1) \\
Y_{3}^{2} &= \frac{1}{4} \sqrt{\frac{105}{\pi}} (x^{2} - y^{2})z \\
Y_{3}^{3} &= \frac{1}{4} \sqrt{\frac{35}{2\pi}} x(x^{2} - 3y^{2})
\end{align*}
\end{split}\]