<commit_before>.. index:: Legendre polynomials

.. _legendre_polynomials:

Legendre Polynomials
====================

Legendre polynomials $P_l(x)$ defined by the Rodrigues' formula

.. math::

       P_l(x)={1\over2^l l!}{\d^l\over\d x^l}[(x^2-1)^l]

they also obey the completeness relation

.. math::
    :label: Lorto

       \sum_{l=0}^\infty {2l+1\over2}P_l(x')P_l(x)=\delta(x-x')

and orthogonality relation:

.. math::

    \int_{-1}^1 P_k(x) P_l(x) \d x = {2\delta_{kl} \over 2k+1}

Two Legendre polynomials can be expanded in a series:

.. math::

    P_k(x) P_l(x)
        = \sum_{m=|k-l|}^{k+l}
        \begin{pmatrix} k & l & m \\ 0 & 0 & 0 \end{pmatrix}^2
        (2m+1) P_m(x)

This was first proven by [Adams]_, where he shows:

.. math::

    P_k(x) P_l(x) = \sum_{m=|k-l|}^{k+l} {A(s-k) A(s-l) A(s-m)\over A(s)}
        {2m+1\over 2s+1} P_m(x)

where $s={k+l+m\over 2}$ and

.. math::

    A(n) = {1\cdot3\cdot5 \cdot \dots \cdot (2n-1) \over
        1\cdot 2\cdot 3\cdot \dots \cdot n} =
            {(2n)!\over 2^n (n!)^2} = {1\over 2^n}\binom{2n}{n}

The coefficient in the expansion can then be written using a $3j$ symbol as:

.. math::

    {A(s-k) A(s-l) A(s-m)\over A(s)} {1\over 2s+1} =

    = {
            {1\over2^{s-k}}\binom{2s-2k}{s-k}
            {1\over2^{s-l}}\binom{2s-2l}{s-l}
            {1\over2^{s-m}}\binom{2s-2m}{s-m}
            \over
            {1\over2^{s}}\binom{2s}{s}
        } {1\over 2s+1} =

    = {2^s\over2^{s-k+s-l+s-m}} {
            \binom{2s-2k}{s-k}
            \binom{2s-2l}{s-l}
            \binom{2s-2m}{s-m}
            \over
            \binom{2s}{s}
        } {1\over 2s+1} =

    = {
            \binom{2s-2k}{s-k}
            \binom{2s-2l}{s-l}
            \binom{2s-2m}{s-m}
            \over
            \binom{2s}{s}
        } {1\over 2s+1} =

    = {
            {(2s-2k)! \over ((s-k)!)^2}
            {(2s-2l)! \over ((s-l)!)^2}
            {(2s-2m)! \over ((s-m)!)^2}
            {(s!)^2 \over (2s)!}
        } {1\over 2s+1} =

    = {(2s-2k)! (2s-2l)! (2s-2m)! \over (2s+1)!}
        \left[{s! \over (s-k)! (s-l)! (s-m)!}\right]^2
       =

    = \begin{pmatrix} k & l & m \\ 0 & 0 & 0 \end{pmatrix}^2

So we will be just using the $3j$ symbol form from now on.
We can now calculate the integral of three Legendre polynomials:

.. math::
    :label: int_three_legendre_polys

    \int_{-1}^1 P_k(x) P_l(x) P_m(x) \d x =

    = \int_{-1}^1
        \sum_{n=|k-l|}^{k+l}
        \begin{pmatrix} k & l & n \\ 0 & 0 & 0 \end{pmatrix}^2
        (2n+1) P_n(x)
    P_m(x) \d x =

    =
        \sum_{n=|k-l|}^{k+l}
        \begin{pmatrix} k & l & n \\ 0 & 0 & 0 \end{pmatrix}^2
        (2n+1) {2\delta_{nm}\over 2n+1}
    =

    = 2 \begin{pmatrix} k & l & m \\ 0 & 0 & 0 \end{pmatrix}^2

This is consistent with the series expansion:

.. math::

    P_k(x) P_l(x) = \sum_{m=|k-l|}^{k+l}
        {2m+1\over 2}\int_{-1}^1 P_k(x) P_l(x) P_m(x) \d x\,\,
        P_m(x) =

    = \sum_{m=|k-l|}^{k+l}
        \begin{pmatrix} k & l & m \\ 0 & 0 & 0 \end{pmatrix}^2
        (2m+1) P_m(x)

Any function $f(x)$ (where $-1\le x \le 1$) can be expanded as:

.. math::

    f(x) = \sum_{l=0}^\infty f_l P_l(x)

    f_l = {(2l+1)\over 2} \int_{-1}^1 f(x) P_l(x) \d x

For the following choice of $f(x)$ we get (for $|t| \le 1$):

.. math::

    f(x) = {1\over\sqrt{1-2xt+t^2}}

    f_l = {(2l+1)\over 2} \int_{-1}^1 {P_l(x)\over\sqrt{1-2xt+t^2}} \d x
        = {(2l+1)\over 2} \int_{|1+t|}^{|1-t|}
                 {P_l\left(1-R^2-t^2\over 2 t\right)\over R}
                 \left(-{R\over t}\right) \d R
        =

        = {(2l+1)\over 2 t} \int_{|1-t|}^{|1+t|}
                 P_l\left(1-R^2-t^2\over 2 t\right) \d R
        = {(2l+1)\over 2 t} \int_{1-t}^{1+t}
                 P_l\left(1-R^2-t^2\over 2 t\right) \d R
        =

        = t^l

Code::

    >>> from sympy import var, legendre, integrate
    >>> var("l R t")
    (l, R, t)
    >>> f = (2*l+1) / (2*t) * integrate(legendre(l, (1-R**2+t**2) / (2*t)),
    ...         (R, 1-t, 1+t))
    >>> for _l in range(20): print _l, f.subs(l, _l).doit().simplify()
    ...
    0 1
    1 t
    2 t**2
    3 t**3
    4 t**4
    5 t**5
    6 t**6
    7 t**7
    8 t**8
    9 t**9
    10 t**10
    11 t**11
    12 t**12
    13 t**13
    14 t**14
    15 t**15
    16 t**16
    17 t**17
    18 t**18
    19 t**19


So the Legendre polynomials are the coefficients of the following expansion
for $|t| \le 1$:

.. math::

    {1\over\sqrt{1-2xt+t^2}} = \sum_{l=0}^\infty P_l(x) t^l

Note that for $|t| > 1$ we get:

.. math::

    {1\over\sqrt{1-2xt+t^2}}
    = {1\over |t|}{1\over\sqrt{1-2x{1\over t}+\left({1\over t}\right)^2}}
    = {1\over |t|}\sum_{l=0}^\infty P_l(x) \left({1\over t}\right)^l
    = \sign t \sum_{l=0}^\infty P_l(x) t^{-l-1}


.. [Adams] <NAME>. (1878). On the Expression of the Product of Any Two Legendre’s Coefficients by Means of a Series of Legendre's Coefficients.  Proceedings of the Royal Society of London, 27, 63-71.

Example I
~~~~~~~~~

Very important is the following multipole expansion:

.. math::
    :label: legendre_expansion

    {1\over |{\bf r}-{\bf r'}|}
        ={1\over \sqrt{({\bf r}-{\bf r'})^2}}
        ={1\over \sqrt{r^2-2{\bf r}\cdot {\bf r'} + r'^2}}
        ={1\over r_>\sqrt{1-2\left(r_<\over r_>\right){\bf\hat r}\cdot {\bf\hat
            r'} + \left(r<\over r_>\right)^2}} =

    ={1\over r_>}\sum_{l=0}^\infty\left(r_<\over r_>\right)^l P_l({\bf\hat r}\cdot {\bf\hat r'})
    =\sum_{l=0}^\infty {r_<^l\over r_>^{l+1}} P_l({\bf\hat r}\cdot {\bf\hat r'})

Where $r_{>} = \max(r, r')$ and
$r_{<} = \min(r, r')$.
Assuming $r > r'$, we get for the first few terms:

.. math::

    {1\over |{\bf r}-{\bf r'}|}
    ={1\over r}\left( P_0({\bf\hat r}\cdot {\bf\hat r'}) + P_1({\bf\hat r}\cdot {\bf\hat r'}){r'\over r} + P_2({\bf\hat r}\cdot {\bf\hat r'})\left(r'\over r\right)^2 + O\left(r'^3\over r^3\right) \right) =

    ={1\over r}\left( 1 + {\bf\hat r}\cdot {\bf\hat r'} {r'\over r} + \half\left(3({\bf\hat r}\cdot {\bf\hat r'})^2-1\right)\left(r'\over r\right)^2 + O\left(r'^3\over r^3\right) \right) =

    ={1\over r} +{{\bf r}\cdot {\bf r'}\over r^3} +{3({\bf r}\cdot {\bf r'})^2-r^2r'^2\over 2r^5} + O\left(r'^3\over r^4\right)

Example II
~~~~~~~~~~

Let's find the expansion of

.. math::

    f(x) = {e^{-\alpha \sqrt{1-2xt+t^2}}\over\sqrt{1-2xt+t^2}}

for $|t| \le 1$. We get:

.. math::

    f_l = {(2l+1)\over 2} \int_{-1}^1
        {P_l(x)e^{-\alpha \sqrt{1-2xt+t^2}}\over\sqrt{1-2xt+t^2}} \d x
        = {(2l+1)\over 2} \int_{|1+t|}^{|1-t|}
                 {P_l\left(1-R^2-t^2\over 2 t\right)e^{-\alpha R}\over R}
                 \left(-{R\over t}\right) \d R
        =

        = {(2l+1)\over 2 t} \int_{|1-t|}^{|1+t|}
                 P_l\left(1-R^2-t^2\over 2 t\right) e^{-\alpha R} \d R
        = {(2l+1)\over 2 t} \int_{1-t}^{1+t}
                 P_l\left(1-R^2-t^2\over 2 t\right) e^{-\alpha R} \d R

Here is the result for the first few $l$:

.. math::

    f_0 & = \frac{\left(e^{2 \alpha t} -1\right) e^{- \alpha t - \alpha}}{2 \alpha t} \\
    f_1 & = \frac{3}{2} \frac{\left(\alpha^{2} t e^{2 \alpha t} + \alpha^{2} t + \alpha t e^{2 \alpha t} + \alpha t - \alpha e^{2 \alpha t} + \alpha - e^{2 \alpha t} + 1\right) e^{- \alpha t - \alpha}}{\alpha^{3} t^{2}} \\
    f_2 & = \frac{5}{2} \frac{\left(\alpha^{4} t^{2} e^{2 \alpha t} - \alpha^{4} t^{2} + 3 \alpha^{3} t^{2} e^{2 \alpha t} - 3 \alpha^{3} t^{2} - 3 \alpha^{3} t e^{2 \alpha t} - 3 \alpha^{3} t + 3 \alpha^{2} t^{2} e^{2 \alpha t} - 3 \alpha^{2} t^{2} - 9 \alpha^{2} t e^{2 \alpha t} - 9 \alpha^{2} t + X\right) e^{- \alpha t - \alpha}}{\alpha^{5} t^{3}}

    X = 3 \alpha^{2} e^{2 \alpha t} - 3 \alpha^{2} - 9 \alpha t e^{2 \alpha t} - 9 \alpha t + 9 \alpha e^{2 \alpha t} - 9 \alpha + 9 e^{2 \alpha t} -9


Expanding in $t$ up to
$\operatorname{\mathcal{O}}\left(t^{7}\right)$ we get:

.. math::

    f_l & = e^\alpha g_l \\
    g_0 & = 1 + \frac{1}{6} \alpha^{2} t^{2} + \frac{1}{120} \alpha^{4} t^{4} + \frac{1}{5040} \alpha^{6} t^{6} + \operatorname{\mathcal{O}}\left(t^{7}\right) \\
    g_1 & = t + \alpha t + \frac{1}{10} \alpha^{2} t^{3} + \frac{1}{10} \alpha^{3} t^{3} + \frac{1}{280} \alpha^{4} t^{5} + \frac{1}{280} \alpha^{5} t^{5} + \operatorname{\mathcal{O}}\left(t^{7}\right) \\<commit_msg>Fix sign errors<commit_after>.. index:: Legendre polynomials

.. _legendre_polynomials:

Legendre Polynomials
====================

Legendre polynomials $P_l(x)$ defined by the Rodrigues' formula

.. math::

       P_l(x)={1\over2^l l!}{\d^l\over\d x^l}[(x^2-1)^l]

they also obey the completeness relation

.. math::
    :label: Lorto

       \sum_{l=0}^\infty {2l+1\over2}P_l(x')P_l(x)=\delta(x-x')

and orthogonality relation:

.. math::

    \int_{-1}^1 P_k(x) P_l(x) \d x = {2\delta_{kl} \over 2k+1}

Two Legendre polynomials can be expanded in a series:

.. math::

    P_k(x) P_l(x)
        = \sum_{m=|k-l|}^{k+l}
        \begin{pmatrix} k & l & m \\ 0 & 0 & 0 \end{pmatrix}^2
        (2m+1) P_m(x)

This was first proven by [Adams]_, where he shows:

.. math::

    P_k(x) P_l(x) = \sum_{m=|k-l|}^{k+l} {A(s-k) A(s-l) A(s-m)\over A(s)}
        {2m+1\over 2s+1} P_m(x)

where $s={k+l+m\over 2}$ and

.. math::

    A(n) = {1\cdot3\cdot5 \cdot \dots \cdot (2n-1) \over
        1\cdot 2\cdot 3\cdot \dots \cdot n} =
            {(2n)!\over 2^n (n!)^2} = {1\over 2^n}\binom{2n}{n}

The coefficient in the expansion can then be written using a $3j$ symbol as:

.. math::

    {A(s-k) A(s-l) A(s-m)\over A(s)} {1\over 2s+1} =

    = {
            {1\over2^{s-k}}\binom{2s-2k}{s-k}
            {1\over2^{s-l}}\binom{2s-2l}{s-l}
            {1\over2^{s-m}}\binom{2s-2m}{s-m}
            \over
            {1\over2^{s}}\binom{2s}{s}
        } {1\over 2s+1} =

    = {2^s\over2^{s-k+s-l+s-m}} {
            \binom{2s-2k}{s-k}
            \binom{2s-2l}{s-l}
            \binom{2s-2m}{s-m}
            \over
            \binom{2s}{s}
        } {1\over 2s+1} =

    = {
            \binom{2s-2k}{s-k}
            \binom{2s-2l}{s-l}
            \binom{2s-2m}{s-m}
            \over
            \binom{2s}{s}
        } {1\over 2s+1} =

    = {
            {(2s-2k)! \over ((s-k)!)^2}
            {(2s-2l)! \over ((s-l)!)^2}
            {(2s-2m)! \over ((s-m)!)^2}
            {(s!)^2 \over (2s)!}
        } {1\over 2s+1} =

    = {(2s-2k)! (2s-2l)! (2s-2m)! \over (2s+1)!}
        \left[{s! \over (s-k)! (s-l)! (s-m)!}\right]^2
       =

    = \begin{pmatrix} k & l & m \\ 0 & 0 & 0 \end{pmatrix}^2

So we will be just using the $3j$ symbol form from now on.
We can now calculate the integral of three Legendre polynomials:

.. math::
    :label: int_three_legendre_polys

    \int_{-1}^1 P_k(x) P_l(x) P_m(x) \d x =

    = \int_{-1}^1
        \sum_{n=|k-l|}^{k+l}
        \begin{pmatrix} k & l & n \\ 0 & 0 & 0 \end{pmatrix}^2
        (2n+1) P_n(x)
    P_m(x) \d x =

    =
        \sum_{n=|k-l|}^{k+l}
        \begin{pmatrix} k & l & n \\ 0 & 0 & 0 \end{pmatrix}^2
        (2n+1) {2\delta_{nm}\over 2n+1}
    =

    = 2 \begin{pmatrix} k & l & m \\ 0 & 0 & 0 \end{pmatrix}^2

This is consistent with the series expansion:

.. math::

    P_k(x) P_l(x) = \sum_{m=|k-l|}^{k+l}
        {2m+1\over 2}\int_{-1}^1 P_k(x) P_l(x) P_m(x) \d x\,\,
        P_m(x) =

    = \sum_{m=|k-l|}^{k+l}
        \begin{pmatrix} k & l & m \\ 0 & 0 & 0 \end{pmatrix}^2
        (2m+1) P_m(x)

Any function $f(x)$ (where $-1\le x \le 1$) can be expanded as:

.. math::

    f(x) = \sum_{l=0}^\infty f_l P_l(x)

    f_l = {(2l+1)\over 2} \int_{-1}^1 f(x) P_l(x) \d x

For the following choice of $f(x)$ we get (for $|t| \le 1$):

.. math::

    f(x) = {1\over\sqrt{1-2xt+t^2}}

    f_l = {(2l+1)\over 2} \int_{-1}^1 {P_l(x)\over\sqrt{1-2xt+t^2}} \d x
        = {(2l+1)\over 2} \int_{|1+t|}^{|1-t|}
                 {P_l\left(1-R^2-t^2\over 2 t\right)\over R}
                 \left(-{R\over t}\right) \d R
        =

        = {(2l+1)\over 2 t} \int_{|1-t|}^{|1+t|}
                 P_l\left(1-R^2-t^2\over 2 t\right) \d R
        = {(2l+1)\over 2 t} \int_{1-t}^{1+t}
                 P_l\left(1-R^2-t^2\over 2 t\right) \d R
        =

        = t^l

Code::

    >>> from sympy import var, legendre, integrate
    >>> var("l R t")
    (l, R, t)
    >>> f = (2*l+1) / (2*t) * integrate(legendre(l, (1-R**2+t**2) / (2*t)),
    ...         (R, 1-t, 1+t))
    >>> for _l in range(20): print _l, f.subs(l, _l).doit().simplify()
    ...
    0 1
    1 t
    2 t**2
    3 t**3
    4 t**4
    5 t**5
    6 t**6
    7 t**7
    8 t**8
    9 t**9
    10 t**10
    11 t**11
    12 t**12
    13 t**13
    14 t**14
    15 t**15
    16 t**16
    17 t**17
    18 t**18
    19 t**19


So the Legendre polynomials are the coefficients of the following expansion
for $|t| \le 1$:

.. math::

    {1\over\sqrt{1-2xt+t^2}} = \sum_{l=0}^\infty P_l(x) t^l

Note that for $|t| > 1$ we get:

.. math::

    {1\over\sqrt{1-2xt+t^2}}
    = {1\over |t|}{1\over\sqrt{1-2x{1\over t}+\left({1\over t}\right)^2}}
    = {1\over |t|}\sum_{l=0}^\infty P_l(x) \left({1\over t}\right)^l
    = \sign t \sum_{l=0}^\infty P_l(x) t^{-l-1}


.. [Adams] <NAME>. (1878). On the Expression of the Product of Any Two Legendre’s Coefficients by Means of a Series of Legendre's Coefficients.  Proceedings of the Royal Society of London, 27, 63-71.

Example I
~~~~~~~~~

Very important is the following multipole expansion:

.. math::
    :label: legendre_expansion

    {1\over |{\bf r}-{\bf r'}|}
        ={1\over \sqrt{({\bf r}-{\bf r'})^2}}
        ={1\over \sqrt{r^2-2{\bf r}\cdot {\bf r'} + r'^2}}
        ={1\over r_>\sqrt{1-2\left(r_<\over r_>\right){\bf\hat r}\cdot {\bf\hat
            r'} + \left(r<\over r_>\right)^2}} =

    ={1\over r_>}\sum_{l=0}^\infty\left(r_<\over r_>\right)^l P_l({\bf\hat r}\cdot {\bf\hat r'})
    =\sum_{l=0}^\infty {r_<^l\over r_>^{l+1}} P_l({\bf\hat r}\cdot {\bf\hat r'})

Where $r_{>} = \max(r, r')$ and
$r_{<} = \min(r, r')$.
Assuming $r > r'$, we get for the first few terms:

.. math::

    {1\over |{\bf r}-{\bf r'}|}
    ={1\over r}\left( P_0({\bf\hat r}\cdot {\bf\hat r'}) + P_1({\bf\hat r}\cdot {\bf\hat r'}){r'\over r} + P_2({\bf\hat r}\cdot {\bf\hat r'})\left(r'\over r\right)^2 + O\left(r'^3\over r^3\right) \right) =

    ={1\over r}\left( 1 + {\bf\hat r}\cdot {\bf\hat r'} {r'\over r} + \half\left(3({\bf\hat r}\cdot {\bf\hat r'})^2-1\right)\left(r'\over r\right)^2 + O\left(r'^3\over r^3\right) \right) =

    ={1\over r} +{{\bf r}\cdot {\bf r'}\over r^3} +{3({\bf r}\cdot {\bf r'})^2-r^2r'^2\over 2r^5} + O\left(r'^3\over r^4\right)

Example II
~~~~~~~~~~

Let's find the expansion of

.. math::

    f(x) = {e^{-\alpha \sqrt{1-2xt+t^2}}\over\sqrt{1-2xt+t^2}}

for $|t| \le 1$. We get:

.. math::

    f_l = {(2l+1)\over 2} \int_{-1}^1
        {P_l(x)e^{-\alpha \sqrt{1-2xt+t^2}}\over\sqrt{1-2xt+t^2}} \d x
        = {(2l+1)\over 2} \int_{|1+t|}^{|1-t|}
                 {P_l\left(1-R^2+t^2\over 2 t\right)e^{-\alpha R}\over R}
                 \left(-{R\over t}\right) \d R
        =

        = {(2l+1)\over 2 t} \int_{|1-t|}^{|1+t|}
                 P_l\left(1-R^2+t^2\over 2 t\right) e^{-\alpha R} \d R
        = {(2l+1)\over 2 t} \int_{1-t}^{1+t}
                 P_l\left(1-R^2+t^2\over 2 t\right) e^{-\alpha R} \d R

Here is the result for the first few $l$:

.. math::

    f_0 & = \frac{\left(e^{2 \alpha t} -1\right) e^{- \alpha t - \alpha}}{2 \alpha t} \\
    f_1 & = \frac{3}{2} \frac{\left(\alpha^{2} t e^{2 \alpha t} + \alpha^{2} t + \alpha t e^{2 \alpha t} + \alpha t - \alpha e^{2 \alpha t} + \alpha - e^{2 \alpha t} + 1\right) e^{- \alpha t - \alpha}}{\alpha^{3} t^{2}} \\
    f_2 & = \frac{5}{2} \frac{\left(\alpha^{4} t^{2} e^{2 \alpha t} - \alpha^{4} t^{2} + 3 \alpha^{3} t^{2} e^{2 \alpha t} - 3 \alpha^{3} t^{2} - 3 \alpha^{3} t e^{2 \alpha t} - 3 \alpha^{3} t + 3 \alpha^{2} t^{2} e^{2 \alpha t} - 3 \alpha^{2} t^{2} - 9 \alpha^{2} t e^{2 \alpha t} - 9 \alpha^{2} t + X\right) e^{- \alpha t - \alpha}}{\alpha^{5} t^{3}}

    X = 3 \alpha^{2} e^{2 \alpha t} - 3 \alpha^{2} - 9 \alpha t e^{2 \alpha t} - 9 \alpha t + 9 \alpha e^{2 \alpha t} - 9 \alpha + 9 e^{2 \alpha t} -9


Expanding in $t$ up to
$\operatorname{\mathcal{O}}\left(t^{7}\right)$ we get:

.. math::

    f_l & = e^{-\alpha} g_l \\
    g_0 & = 1 + \frac{1}{6} \alpha^{2} t^{2} + \frac{1}{120} \alpha^{4} t^{4} + \frac{1}{5040} \alpha^{6} t^{6} + \operatorname{\mathcal{O}}\left(t^{7}\right) \\
    g_1 & = t + \alpha t + \frac{1}{10} \alpha^{2} t^{3} + \frac{1}{10} \alpha^{3} t^{3} + \frac{1}{280} \alpha^{4} t^{5} + \frac{1}{280} \alpha^{5} t^{5} + \operatorname{\mathcal{O}}\left(t^{7}\right) \\
{'fraction_non_alphanumeric': 0.2596765400690532, 'fraction_numerical': 0.059482706402568294, 'mean_word_length': 2.048938134810711, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3930485', 'n_tokens_mistral': 8066, 'n_tokens_neox': 7565, 'n_words': 1712}