## Copyright (C) 2008 Marco Caliari
##
## This file is part of Octave.
##
## Octave is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or (at
## your option) any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING. If not, see
## .
## -*- texinfo -*-
## @deftypefn {Function File} address@hidden =} legendre (@var{n}, @var{X})
## @deftypefnx {Function File} address@hidden =} legendre (@var{n}, @var{X}, "unnorm")
##
## Legendre Function of degree n and order m
## where all values for m = address@hidden are returned.
## @var{n} must be an integer.
## The return value has one dimension more than @var{x}.
##
## @example
## The Legendre Function of degree n and order m
##
## @group
## m m 2 m/2 d^m
## P(x) = (-1) * (1-x ) * ---- P (x)
## n dx^m n
## @end group
##
## with:
## Legendre polynomial of degree n
##
## @group
## 1 d^n 2 n
## P (x) = ------ [----(x - 1) ]
## n 2^n n! dx^n
## @end group
##
## legendre(3,[-1.0 -0.9 -0.8]) returns the matrix
##
## @group
## x | -1.0 | -0.9 | -0.8
## ------------------------------------
## m=0 | -1.00000 | -0.47250 | -0.08000
## m=1 | 0.00000 | -1.99420 | -1.98000
## m=2 | 0.00000 | -2.56500 | -4.32000
## m=3 | 0.00000 | -1.24229 | -3.24000
## @end group
## @end example
##
## @deftypefnx {Function File} address@hidden =} legendre (@var{n}, @var{X}, "sch")
##
## Computes the Schmidt semi-normalized associated Legendre function.
## The Schmidt semi-normalized associated Legendre function is related
## to the unnormalized Legendre functions by
##
## @example
## For Legendre functions of degree n and order 0
##
## @group
## 0 0
## SP (x) = P (x)
## n n
## @end group
##
## For Legendre functions of degree n and order m
##
## @group
## m m m 2(n-m)! 0.5
## SP (x) = P (x) * (-1) * [-------]
## n n (n+m)!
## @end group
## @end example
##
## @deftypefnx {Function File} address@hidden =} legendre (@var{n}, @var{X}, "norm")
##
## Computes the fully normalized associated Legendre function.
## The fully normalized associated Legendre function is related
## to the unnormalized Legendre functions by
##
## @example
## For Legendre functions of degree n and order m
##
## @group
## m m m (n+0.5)(n-m)! 0.5
## NP (x) = P (x) * (-1) * [-------------]
## n n (n+m)!
## @end group
## @end example
##
## @end deftypefn
## Author: Marco Caliari
function L = legendre(n,x,varargin)
x = x(:).';
L = zeros(n+1,length(x));
if (nargin == 2)
normalization = "unnorm";
else
normalization = varargin{1};
end
if (strcmp(normalization,"norm"))
scale = sqrt(n+0.5);
elseif (strcmp(normalization,"sch"))
scale = sqrt(2);
elseif (strcmp(normalization,"unnorm"))
scale = 1;
else
error("Normalization option not recognized.")
end
% Based on the first recurrence relation found in
% http://en.wikipedia.org/wiki/Associated_Legendre_function
for m = 1:n
LP = ones(n+1,length(x))*scale;
LP(m,:) = prod(1:2:2*(m-1)-1)*LP(m,:).*sqrt(1-x.^2).^(m-1);
if (m == n+1)
L(m,:) = LP(m,:);
break
end
LP(m+1,:) = (2*m-1).*x.*LP(m,:);
for l = m+1:n
LP(l+1,:) = ((2*(l-1)+1).*x.*LP(l,:)-(l-1+m-1).*LP(l-1,:))./(l-m+1);
end
L(m,:) = LP(n+1,:);
if (strcmp(normalization,"norm"))
scale = scale/sqrt((n-m+1)*(n+m));
elseif (strcmp(normalization,"sch"))
scale = scale/sqrt((n-m+1)*(n+m));
else
scale = -scale;
end
end
L(n+1,:) = prod(1:2:2*n-1)*scale.*sqrt(1-x.^2).^n;
if (strcmp(normalization,"sch"))
L(1,:) = L(1,:)/sqrt(2);
end