...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
#include <boost/math/special_functions/legendre.hpp>
namespace boost{ namespace math{ template <class T> calculatedresulttype legendre_p(int n, T x); template <class T, class Policy> calculatedresulttype legendre_p(int n, T x, const Policy&); template <class T> calculatedresulttype legendre_p_prime(int n, T x); template <class T, class Policy> calculatedresulttype legendre_p_prime(int n, T x, const Policy&); template <class T, class Policy> std::vector<T> legendre_p_zeros(int l, const Policy&); template <class T> std::vector<T> legendre_p_zeros(int l); template <class T> calculatedresulttype legendre_p(int n, int m, T x); template <class T, class Policy> calculatedresulttype legendre_p(int n, int m, T x, const Policy&); template <class T> calculatedresulttype legendre_q(unsigned n, T x); template <class T, class Policy> calculatedresulttype legendre_q(unsigned n, T x, const Policy&); template <class T1, class T2, class T3> calculatedresulttype legendre_next(unsigned l, T1 x, T2 Pl, T3 Plm1); template <class T1, class T2, class T3> calculatedresulttype legendre_next(unsigned l, unsigned m, T1 x, T2 Pl, T3 Plm1); }} // namespaces
The return type of these functions is computed using the result
type calculation rules: note than when there is a single
template argument the result is the same type as that argument or double
if the template argument is an integer
type.
The final Policy argument is optional and can be used to control the behaviour of the function: how it handles errors, what level of precision to use etc. Refer to the policy documentation for more details.
template <class T> calculatedresulttype legendre_p(int l, T x); template <class T, class Policy> calculatedresulttype legendre_p(int l, T x, const Policy&);
Returns the Legendre Polynomial of the first kind:
Requires 1 <= x <= 1, otherwise returns the result of domain_error.
Negative orders are handled via the reflection formula:
P_{l1}(x) = P_{l}(x)
The following graph illustrates the behaviour of the first few Legendre Polynomials:
template <class T> calculatedresulttype legendre_p_prime(int n, T x); template <class T, class Policy> calculatedresulttype legendre_p_prime(int n, T x, const Policy&);
Returns the derivatives of the Legendre polynomials.
template <class T, class Policy> std::vector<T> legendre_p_zeros(int l, const Policy&); template <class T> std::vector<T> legendre_p_zeros(int l);
The zeros of the Legendre polynomials are calculated by Newton's method using an initial guess given by Tricomi with root bracketing provided by Szego.
Since the Legendre polynomials are alternatively even and odd, only the nonnegative zeros are returned. For the odd Legendre polynomials, the first zero is always zero. The rest of the zeros are returned in increasing order.
Note that the argument to the routine is an integer, and the output is a
floatingpoint type. Hence the template argument is mandatory. The time to
extract a single root is linear in l
(this is scaling to evaluate the Legendre polynomials), so recovering all
roots is 𝑶(l
^{2}). Algorithms
with linear scaling exist
for recovering all roots, but requires tooling not currently built into boost.math.
This implementation proceeds under the assumption that calculating zeros
of these functions will not be a bottleneck for any workflow.
template <class T> calculatedresulttype legendre_p(int l, int m, T x); template <class T, class Policy> calculatedresulttype legendre_p(int l, int m, T x, const Policy&);
Returns the associated Legendre polynomial of the first kind:
Requires 1 <= x <= 1, otherwise returns the result of domain_error.
Negative values of l and m are handled via the identity relations:
Caution  

The definition of the associated Legendre polynomial used here includes a leading CondonShortley phase term of (1)^{m}. This matches the definition given by Abramowitz and Stegun (8.6.6) and that used by Mathworld and Mathematica's LegendreP function. However, uses in the literature do not always include this phase term, and strangely the specification for the associated Legendre function in the C++ TR1 (assoc_legendre) also omits it, in spite of stating that it uses Abramowitz and Stegun as the final arbiter on these matters. See: Weisstein, Eric W. "Legendre Polynomial." From MathWorldA Wolfram Web Resource. Abramowitz, M. and Stegun, I. A. (Eds.). "Legendre Functions" and "Orthogonal Polynomials." Ch. 22 in Chs. 8 and 22 in Handbook of Mathematical Functions with Formulas, Graphs, and Mathematical Tables, 9th printing. New York: Dover, pp. 331339 and 771802, 1972. 
template <class T> calculatedresulttype legendre_q(unsigned n, T x); template <class T, class Policy> calculatedresulttype legendre_q(unsigned n, T x, const Policy&);
Returns the value of the Legendre polynomial that is the second solution to the Legendre differential equation, for example:
Requires 1 <= x <= 1, otherwise domain_error is called.
The following graph illustrates the first few Legendre functions of the second kind:
template <class T1, class T2, class T3> calculatedresulttype legendre_next(unsigned l, T1 x, T2 Pl, T3 Plm1);
Implements the three term recurrence relation for the Legendre polynomials, this function can be used to create a sequence of values evaluated at the same x, and for rising l. This recurrence relation holds for Legendre Polynomials of both the first and second kinds.
For example we could produce a vector of the first 10 polynomial values using:
double x = 0.5; // Abscissa value vector<double> v; v.push_back(legendre_p(0, x)); v.push_back(legendre_p(1, x)); for(unsigned l = 1; l < 10; ++l) v.push_back(legendre_next(l, x, v[l], v[l1])); // Double check values: for(unsigned l = 1; l < 10; ++l) assert(v[l] == legendre_p(l, x));
Formally the arguments are:
The degree of the last polynomial calculated.
The abscissa value
The value of the polynomial evaluated at degree l.
The value of the polynomial evaluated at degree l1.
template <class T1, class T2, class T3> calculatedresulttype legendre_next(unsigned l, unsigned m, T1 x, T2 Pl, T3 Plm1);
Implements the three term recurrence relation for the Associated Legendre polynomials, this function can be used to create a sequence of values evaluated at the same x, and for rising l.
For example we could produce a vector of the first m+10 polynomial values using:
double x = 0.5; // Abscissa value int m = 10; // order vector<double> v; v.push_back(legendre_p(m, m, x)); v.push_back(legendre_p(1 + m, m, x)); for(unsigned l = 1; l < 10; ++l) v.push_back(legendre_next(l + 10, m, x, v[l], v[l1])); // Double check values: for(unsigned l = 1; l < 10; ++l) assert(v[l] == legendre_p(10 + l, m, x));
Formally the arguments are:
The degree of the last polynomial calculated.
The order of the Associated Polynomial.
The abscissa value
The value of the polynomial evaluated at degree l.
The value of the polynomial evaluated at degree l1.
The following table shows peak errors (in units of epsilon) for various domains of input arguments. Note that only results for the widest floating point type on the system are given as narrower types have effectively zero error.
Table 7.32. Error rates for legendre_p
GNU C++ version 7.1.0 
GNU C++ version 7.1.0 
Sun compiler version 0x5150 
Microsoft Visual C++ version 14.1 


Legendre Polynomials: Small Values 
Max = 0.732ε (Mean = 0.0619ε) 
Max = 69.2ε (Mean = 9.58ε) 
Max = 69.2ε (Mean = 9.58ε) 
Max = 211ε (Mean = 20.4ε) 
Legendre Polynomials: Large Values 
Max = 0.632ε (Mean = 0.0693ε) 
Max = 699ε (Mean = 59.6ε) 
Max = 699ε (Mean = 59.6ε) 
Max = 300ε (Mean = 33.2ε) 
Table 7.33. Error rates for legendre_q
GNU C++ version 7.1.0 
GNU C++ version 7.1.0 
Sun compiler version 0x5150 
Microsoft Visual C++ version 14.1 


Legendre Polynomials: Small Values 
Max = 0.612ε (Mean = 0.0517ε) 
Max = 50.9ε (Mean = 9ε) 
Max = 50.9ε (Mean = 8.98ε) 
Max = 46.4ε (Mean = 7.32ε) 
Legendre Polynomials: Large Values 
Max = 2.49ε (Mean = 0.202ε) 
Max = 5.98e+03ε (Mean = 478ε) 
Max = 5.98e+03ε (Mean = 478ε) 
Max = 4.6e+03ε (Mean = 366ε) 
Table 7.34. Error rates for legendre_p (associated)
GNU C++ version 7.1.0 
GNU C++ version 7.1.0 
Sun compiler version 0x5150 
Microsoft Visual C++ version 14.1 


Associated Legendre Polynomials: Small Values 
Max = 0.999ε (Mean = 0.05ε) 
Max = 175ε (Mean = 9.88ε) 
Max = 77.7ε (Mean = 5.59ε) 
Max = 121ε (Mean = 7.14ε) 
Note that the worst errors occur when the order increases, values greater than ~120 are very unlikely to produce sensible results, especially in the associated polynomial case when the degree is also large. Further the relative errors are likely to grow arbitrarily large when the function is very close to a root.
A mixture of spot tests of values calculated using functions.wolfram.com, and randomly generated test data are used: the test data was computed using NTL::RR at 1000bit precision.
These functions are implemented using the stable three term recurrence relations. These relations guarantee low absolute error but cannot guarantee low relative error near one of the roots of the polynomials.