Lines 46
##### Keywords
maths (2) numerics (5)
##### Permissions
Owner: Stou S.
Viewable by Everyone
Editable by All Siafoo Users
Bored? Check out the Recent Activity on Siafoo

# Recursive template for the Legendre Polynomials 0

 In Brief A template implementation of the Bonnet recursion formula for generating of Legendre polynomials. I wrote this as a sort of template meta-programming exercise but have verified that the first ~10 generated polynomials produce identical output to that of the legendre_p function from the boost Math Toolkit.... more
 Language C++
# 's
` 1/* 2 * Legendre.hpp 3 * 4 *  Created on: Oct 27, 2009 5 *  Author: stou@icapsid.net 6 *  License: Public Domain (send me improvements though) 7 */ 8 9namespace Legendre10{1112template<class T, unsigned int L> struct P13{14    T operator()(T x)15    {16        return ((2 * L - 1) / T(L)) * x * P<T, L - 1> ()(x) - ((L - 1) / T(L)) * P<T, L - 2> ()(x);17    }18};1920template<class T> struct P<T, 0>21{22    T operator()(T x)23    {24        return T(1);25    }26};2728template<class T> struct P<T, 1>29{30    T operator()(T x)31    {32        return x;33    }34};3536// Sum of the first N legendre polynomials37template<class T, unsigned int N> struct P_sum38{39    T operator()(T x)40    {41        return P_sum<T, N - 1> ()(x) + P<T, N - 1> ()(x);42    }43};4445template<class T> struct P_sum<T, 0>46{47    T operator()(T x)48    {49        return T(0);50    }51};5253}`

A template implementation of the Bonnet recursion formula for generating of Legendre polynomials. I wrote this as a sort of template meta-programming exercise but have verified that the first ~10 generated polynomials produce identical output to that of the legendre_p function from the boost Math Toolkit.

Use the template like this:

# 's
`1P<float, 5>()(x)23// OR4P<float, 10> p();5p_10(x)`

It is also possible to sum the first N polynomials using:

# 's
`1P_sum<float, 5>()(x)`

You are much better off using the boost Math Toolkit when summing the polynomials because:

Warning

The polynomial sums are VERY VERY slow above > 10, much slower than simply looping over legendre_p from the boost Math Toolkit.

Also this isn't exactly the Bonnet recursion formula, it is a rearranged version: