normalized ALF (Assotiated Legendre Function)

Marco Caliari marco.caliari at univr.it
Tue Feb 12 09:02:23 CST 2008


Hi.

The normalized Lagrange functions should (almost) never give NaNs of Infs. 
The enclosed script fixes a problem in the previous:

octave:1> legendreold(151,-0.9,"norm")(end-1:end)
ans =

   -3.3248e-53
           Inf
octave:2> legendre(151,-0.9,"norm")(end-1:end)
ans =

   -3.3248e-53
    9.2660e-55

Marco

> "The following functions compute the associated Legendre Polynomials 
> P_l^m(x). Note that this function grows combinatorially with l and can 
> overflow for l larger than about 150. There is no trouble for small m, but 
> overflow occurs when m and l are both large. Rather than allow overflows, 
> these functions refuse to calculate P_l^m(x) and return GSL_EOVRFLW when they 
> can sense that l and m are too big."
-------------- next part --------------
## 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
## <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn {Function File} {@var{L} =} legendre (@var{n}, @var{X})
## @deftypefnx {Function File} {@var{L} =} legendre (@var{n}, @var{X}, "unnorm")
##
## Legendre Function of degree n and order m
## where all values for m = 0.. at var{n} 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} {@var{L} =} 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} {@var{L} =} 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 <marco.caliari at univr.it>
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));
  LP(m,:) = scale*LP(m,:).*sqrt(1-x.^2).^(m-1);
  LP(m+1,:) = (2*m-1).*x.*LP(m,:);
  for l = m+1:n
    LP(l+1,:) = ((2*l-1).*x.*LP(l,:)-(l+m-2).*LP(l-1,:))./(l-m+1);
  end
  L(m,:) = LP(n+1,:);
  if (strcmp(normalization,"unnorm"))
    scale = -scale*(2*m-1);
  else # normalization == "sch" or normalization == "norm"
    scale = scale/sqrt((n-m+1)*(n+m))*(2*m-1);
  end
end
L(n+1,:) = scale.*sqrt(1-x.^2).^n;
if (strcmp(normalization,"sch"))
  L(1,:) = L(1,:)/sqrt(2);
end


More information about the Help-octave mailing list