]> gitweb.michael.orlitzky.com - octave.git/blob - legendre_p.m
Two minor optimizations to test functions.
[octave.git] / legendre_p.m
1 function P = legendre_p(n)
2 ## Return the `n`th Legendre polynomial.
3 ##
4 ## INPUT:
5 ##
6 ## * ``n`` - The index of the polynomial that we want.
7 ##
8 ## OUTPUT:
9 ##
10 ## * ``P`` - A polynomial function of one argument.
11 ##
12 if (n < 0)
13 ## Can't do anything here. Return nothing.
14 P = NA;
15 elseif (n == 0)
16 ## One of our base cases.
17 P = @(x) 1;
18 elseif (n == 1)
19 ## The second base case.
20 P = @(x) x;
21 else
22 ## Not one of the base cases, so use the recursive formula.
23 prev = legendre_p(n-1);
24 prev_prev = legendre_p(n-2);
25 P = @(x) (1/n).*( (2*n - 1).*x.*prev(x) - (n-1).*prev_prev(x) );
26 end
27 end