1## Copyright (C) 2000, 2006, 2007, 2009 Kai Habel
2## Copyright (C) 2008 Marco Caliari
4## This file is part of Octave.
6## Octave is free software; you can redistribute it and/or modify it
7## under the terms of the GNU General Public License as published by
8## the Free Software Foundation; either version 3 of the License, or (at
9## your option) any later version.
11## Octave is distributed in the hope that it will be useful, but
12## WITHOUT ANY WARRANTY; without even the implied warranty of
13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14## General Public License for more details.
16## You should have received a copy of the GNU General Public License
17## along with Octave; see the file COPYING. If not, see
18## <http://www.gnu.org/licenses/>.
21## @deftypefn {Function File} {@var{l} =} legendre (@var{n}, @var{x})
22## @deftypefnx {Function File} {@var{l} =} legendre (@var{n}, @var{x}, @var{normalization})
23## Compute the Legendre function of degree @var{n} and order
24## @var{m} = 0 @dots{} N. The optional argument, @var{normalization},
25## may be one of @code{"unnorm"}, @code{"sch"}, or @code{"norm"}.
26## The default is @code{"unnorm"}. The value of @var{n} must be a
27## non-negative scalar integer.
29## If the optional argument @var{normalization} is missing or is
30## @code{"unnorm"}, compute the Legendre function of degree @var{n} and
31## order @var{m} and return all values for @var{m} = 0 @dots{} @var{n}.
32## The return value has one dimension more than @var{x}.
34## The Legendre Function of degree @var{n} and order @var{m}:
39## P(x) = (-1) * (1-x ) * ---- P (x)
45## with Legendre polynomial of degree @var{n}:
50## P (x) = ------ [----(x - 1) ]
56## @code{legendre (3, [-1.0, -0.9, -0.8])} returns the matrix:
60## x | -1.0 | -0.9 | -0.8
61## ------------------------------------
62## m=0 | -1.00000 | -0.47250 | -0.08000
63## m=1 | 0.00000 | -1.99420 | -1.98000
64## m=2 | 0.00000 | -2.56500 | -4.32000
65## m=3 | 0.00000 | -1.24229 | -3.24000
69## If the optional argument @code{normalization} is @code{"sch"},
70## compute the Schmidt semi-normalized associated Legendre function.
71## The Schmidt semi-normalized associated Legendre function is related
72## to the unnormalized Legendre functions by the following:
74## For Legendre functions of degree n and order 0:
84## For Legendre functions of degree n and order m:
89## SP (x) = P (x) * (-1) * [-------]
94## If the optional argument @var{normalization} is @code{"norm"},
95## compute the fully normalized associated Legendre function.
96## The fully normalized associated Legendre function is related
97## to the unnormalized Legendre functions by the following:
99## For Legendre functions of degree @var{n} and order @var{m}
103## m m m (n+0.5)(n-m)! 0.5
104## NP (x) = P (x) * (-1) * [-------------]
110## Author: Marco Caliari <marco.caliari@univr.it>
112function retval = legendre (n, x, normalization)
114 persistent warned_overflow = false;
116 if (nargin < 2 || nargin > 3)
121 normalization = lower (normalization);
123 normalization = "unnorm";
126 if (! isscalar (n) || n < 0 || n != fix (n))
127 error ("legendre: n must be a non-negative scalar integer");
130 if (! isvector (x) || any (x < -1 || x > 1))
131 error ("legendre: x must be vector in range -1 <= x <= 1");
134 switch (normalization)
136 scale = sqrt (n+0.5);
142 error ("legendre: expecting normalization option to be \"norm\", \"sch\", or \"unnorm\"");
145 scale = scale * ones (1, numel (x));
147 ## Based on the recurrence relation below
149 ## (n-m+1) * P (x) = (2*n+1)*x*P (x) - (n+1)*P (x)
151 ## http://en.wikipedia.org/wiki/Associated_Legendre_function
156 lpm2 = (2*m-1) .* x .* scale;
159 lpm3a = (2*k-1) .* x .* lpm2;
160 lpm3b = (k+m-2) .* lpm1;
161 lpm3 = (lpm3a - lpm3b)/(k-m+1);
164 if (! warned_overflow)
165 if (any (abs (lpm3a) > realmax)
166 || any (abs (lpm3b) > realmax)
167 || any (abs (lpm3) > realmax))
173 if (strcmp (normalization, "unnorm"))
174 scale = -scale * (2*m-1);
176 ## normalization == "sch" or normalization == "norm"
177 scale = scale / sqrt ((n-m+1)*(n+m))*(2*m-1);
179 scale = scale .* sqrt(1-x.^2);
182 retval(n+1,:) = scale;
184 if (strcmp (normalization, "sch"))
185 retval(1,:) = retval(1,:) / sqrt (2);
188 if (overflow && ! warned_overflow)
189 warning ("legendre: overflow - results may be unstable for high orders");
190 warned_overflow = true;
196%! result = legendre (3, [-1.0 -0.9 -0.8]);
198%! -1.00000 -0.47250 -0.08000
199%! 0.00000 -1.99420 -1.98000
200%! 0.00000 -2.56500 -4.32000
201%! 0.00000 -1.24229 -3.24000
203%! assert (result, expected, 1e-5);
206%! result = legendre (3, [-1.0 -0.9 -0.8], "sch");
208%! -1.00000 -0.47250 -0.08000
209%! 0.00000 0.81413 0.80833
210%! -0.00000 -0.33114 -0.55771
211%! 0.00000 0.06547 0.17076
213%! assert (result, expected, 1e-5);
216%! result = legendre (3, [-1.0 -0.9 -0.8], "norm");
218%! -1.87083 -0.88397 -0.14967
219%! 0.00000 1.07699 1.06932
220%! -0.00000 -0.43806 -0.73778
221%! 0.00000 0.08661 0.22590
223%! assert (result, expected, 1e-5);
226%! result = legendre (151, 0);
227%! ## Don't compare to "-Inf" since it would fail on 64 bit systems.
228%! assert (result(end) < -1.7976e308 && all (isfinite (result(1:end-1))));
231%! result = legendre (150, 0);
232%! ## This agrees with Matlab's result.
233%! assert (result(end), 3.7532741115719e+306, 0.0000000000001e+306)
236%! result = legendre (0, 0:0.1:1);
237%! assert (result, full(ones(1,11)))