Mercurial > hg > octave-lyh
annotate scripts/specfun/legendre.m @ 11415:e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
author | Michael Godfrey <godfrey@isl.stanford.edu> |
---|---|
date | Sun, 26 Dec 2010 21:28:20 -0800 |
parents | be55736a0783 |
children | 1740012184f9 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2000, 2006, 2007, 2009 Kai Habel |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
2 ## Copyright (C) 2008 Marco Caliari |
5827 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
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 | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
5827 | 10 ## |
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. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
5827 | 19 |
20 ## -*- texinfo -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10672
diff
changeset
|
21 ## @deftypefn {Function File} {@var{l} =} legendre (@var{n}, @var{x}) |
7508 | 22 ## @deftypefnx {Function File} {@var{l} =} legendre (@var{n}, @var{x}, @var{normalization}) |
7517
f88b720bf254
legendre.m: modified help text.
Ben Abbott <bpabbott@mac.com>
parents:
7508
diff
changeset
|
23 ## Compute the Legendre function of degree @var{n} and order |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10672
diff
changeset
|
24 ## @var{m} = 0 @dots{} N@. The optional argument, @var{normalization}, |
7517
f88b720bf254
legendre.m: modified help text.
Ben Abbott <bpabbott@mac.com>
parents:
7508
diff
changeset
|
25 ## may be one of @code{"unnorm"}, @code{"sch"}, or @code{"norm"}. |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8975
diff
changeset
|
26 ## The default is @code{"unnorm"}. The value of @var{n} must be a |
7517
f88b720bf254
legendre.m: modified help text.
Ben Abbott <bpabbott@mac.com>
parents:
7508
diff
changeset
|
27 ## non-negative scalar integer. |
7508 | 28 ## |
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}. | |
5827 | 33 ## |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
34 ## The Legendre Function of degree @var{n} and order @var{m}: |
5827 | 35 ## |
11415
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
36 ## @tex |
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
37 ## $$ |
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
38 ## P^m_n(x) = (-1)^m (1-x^2)^{m/2}{d^m\over {dx^m}}P_n (x) |
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
39 ## $$ |
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
40 ## @end tex |
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
41 ## @ifnottex |
5827 | 42 ## @example |
43 ## @group | |
44 ## m m 2 m/2 d^m | |
45 ## P(x) = (-1) * (1-x ) * ---- P (x) | |
46 ## n dx^m n | |
47 ## @end group | |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
48 ## @end example |
11415
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
49 ## @end ifnottex |
5827 | 50 ## |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
51 ## @noindent |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
52 ## with Legendre polynomial of degree @var{n}: |
5827 | 53 ## |
11415
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
54 ## @tex |
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
55 ## $$ |
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
56 ## P(x) = {1\over{2^n n!}}\biggl({d^n\over{dx^n}}(x^2 - 1)^n\biggr) |
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
57 ## $$ |
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
58 ## @end tex |
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
59 ## @ifnottex |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
60 ## @example |
5827 | 61 ## @group |
62 ## 1 d^n 2 n | |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
63 ## P (x) = ------ [----(x - 1) ] |
5827 | 64 ## n 2^n n! dx^n |
65 ## @end group | |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
66 ## @end example |
11415
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
67 ## @end ifnottex |
5827 | 68 ## |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
69 ## @noindent |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
70 ## @code{legendre (3, [-1.0, -0.9, -0.8])} returns the matrix: |
5827 | 71 ## |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
72 ## @example |
5827 | 73 ## @group |
74 ## x | -1.0 | -0.9 | -0.8 | |
75 ## ------------------------------------ | |
76 ## m=0 | -1.00000 | -0.47250 | -0.08000 | |
77 ## m=1 | 0.00000 | -1.99420 | -1.98000 | |
78 ## m=2 | 0.00000 | -2.56500 | -4.32000 | |
79 ## m=3 | 0.00000 | -1.24229 | -3.24000 | |
80 ## @end group | |
81 ## @end example | |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
82 ## |
7508 | 83 ## If the optional argument @code{normalization} is @code{"sch"}, |
84 ## compute the Schmidt semi-normalized associated Legendre function. | |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
85 ## The Schmidt semi-normalized associated Legendre function is related |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
86 ## to the unnormalized Legendre functions by the following: |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
87 ## |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
88 ## For Legendre functions of degree n and order 0: |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
89 ## |
11415
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
90 ## @tex |
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
91 ## $$ |
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
92 ## SP^0_n (x) = P^0_n (x) |
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
93 ## $$ |
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
94 ## @end tex |
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
95 ## @ifnottex |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
96 ## @example |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
97 ## @group |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
98 ## 0 0 |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
99 ## SP (x) = P (x) |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
100 ## n n |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
101 ## @end group |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
102 ## @end example |
11415
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
103 ## @end ifnottex |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
104 ## |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
105 ## For Legendre functions of degree n and order m: |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
106 ## |
11415
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
107 ## @tex |
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
108 ## $$ |
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
109 ## SP^m_n (x) = P^m_n (x)(-1)^m\biggl({2(n-m)!\over{(n+m)!}}\biggl)^{0.5} |
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
110 ## $$ |
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
111 ## @end tex |
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
112 ## @ifnottex |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
113 ## @example |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
114 ## @group |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
115 ## m m m 2(n-m)! 0.5 |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
116 ## SP (x) = P (x) * (-1) * [-------] |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
117 ## n n (n+m)! |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
118 ## @end group |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
119 ## @end example |
11415
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
120 ## @end ifnottex |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
121 ## |
7508 | 122 ## If the optional argument @var{normalization} is @code{"norm"}, |
123 ## compute the fully normalized associated Legendre function. | |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
124 ## The fully normalized associated Legendre function is related |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
125 ## to the unnormalized Legendre functions by the following: |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
126 ## |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
127 ## For Legendre functions of degree @var{n} and order @var{m} |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
128 ## |
11415
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
129 ## @tex |
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
130 ## $$ |
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
131 ## NP^m_n (x) = P^m_n (x)(-1)^m\biggl({(n+0.5)(n-m)!\over{(n+m)!}}\biggl)^{0.5} |
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
132 ## $$ |
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
133 ## @end tex |
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
134 ## @ifnottex |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
135 ## @example |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
136 ## @group |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
137 ## m m m (n+0.5)(n-m)! 0.5 |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
138 ## NP (x) = P (x) * (-1) * [-------------] |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
139 ## n n (n+m)! |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
140 ## @end group |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
141 ## @end example |
11415
e7ed20f87e82
Add @tex blocks to gammainc and legendre docstrings.
Michael Godfrey <godfrey@isl.stanford.edu>
parents:
10793
diff
changeset
|
142 ## @end ifnottex |
5827 | 143 ## @end deftypefn |
144 | |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
145 ## Author: Marco Caliari <marco.caliari@univr.it> |
5827 | 146 |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
147 function retval = legendre (n, x, normalization) |
5827 | 148 |
8255
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
149 persistent warned_overflow = false; |
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
150 |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
151 if (nargin < 2 || nargin > 3) |
5827 | 152 print_usage (); |
153 endif | |
154 | |
10672
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
155 if (!isscalar (n) || n < 0 || n != fix (n)) |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
156 error ("legendre: N must be a non-negative scalar integer"); |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
157 endif |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
158 |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
159 if (!isvector (x) || !isreal (x) || any (x < -1 | x > 1)) |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
160 error ("legendre: X must be real-valued vector in the range -1 <= X <= 1"); |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
161 endif |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
162 |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
163 if (nargin == 3) |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
164 normalization = lower (normalization); |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
165 else |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
166 normalization = "unnorm"; |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
167 endif |
5827 | 168 |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
169 switch (normalization) |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
170 case "norm" |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
171 scale = sqrt (n+0.5); |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
172 case "sch" |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
173 scale = sqrt (2); |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
174 case "unnorm" |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
175 scale = 1; |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
176 otherwise |
10672
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
177 error ('legendre: expecting normalization option to be "norm", "sch", or "unnorm"'); |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
178 endswitch |
5827 | 179 |
10672
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
180 if (rows (x) != 1) |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
181 x = x'; |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
182 endif |
8255
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
183 scale = scale * ones (1, numel (x)); |
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
184 |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
185 ## Based on the recurrence relation below |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
186 ## m m m |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
187 ## (n-m+1) * P (x) = (2*n+1)*x*P (x) - (n+1)*P (x) |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
188 ## n+1 n n-1 |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
189 ## http://en.wikipedia.org/wiki/Associated_Legendre_function |
5827 | 190 |
8255
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
191 overflow = false; |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
192 for m = 1:n |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
193 lpm1 = scale; |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
194 lpm2 = (2*m-1) .* x .* scale; |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
195 lpm3 = lpm2; |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
196 for k = m+1:n |
8255
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
197 lpm3a = (2*k-1) .* x .* lpm2; |
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
198 lpm3b = (k+m-2) .* lpm1; |
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
199 lpm3 = (lpm3a - lpm3b)/(k-m+1); |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
200 lpm1 = lpm2; |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
201 lpm2 = lpm3; |
8255
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
202 if (! warned_overflow) |
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
203 if (any (abs (lpm3a) > realmax) |
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
204 || any (abs (lpm3b) > realmax) |
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
205 || any (abs (lpm3) > realmax)) |
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
206 overflow = true; |
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
207 endif |
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
208 endif |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
209 endfor |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
210 retval(m,:) = lpm3; |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
211 if (strcmp (normalization, "unnorm")) |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
212 scale = -scale * (2*m-1); |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
213 else |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
214 ## normalization == "sch" or normalization == "norm" |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
215 scale = scale / sqrt ((n-m+1)*(n+m))*(2*m-1); |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
216 endif |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
217 scale = scale .* sqrt(1-x.^2); |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
218 endfor |
5827 | 219 |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
220 retval(n+1,:) = scale; |
5827 | 221 |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
222 if (strcmp (normalization, "sch")) |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
223 retval(1,:) = retval(1,:) / sqrt (2); |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
224 endif |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
225 |
8255
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
226 if (overflow && ! warned_overflow) |
8664 | 227 warning ("legendre: overflow - results may be unstable for high orders"); |
8255
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
228 warned_overflow = true; |
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
229 endif |
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
230 |
5827 | 231 endfunction |
232 | |
233 %!test | |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
234 %! result = legendre (3, [-1.0 -0.9 -0.8]); |
5827 | 235 %! expected = [ |
236 %! -1.00000 -0.47250 -0.08000 | |
237 %! 0.00000 -1.99420 -1.98000 | |
238 %! 0.00000 -2.56500 -4.32000 | |
239 %! 0.00000 -1.24229 -3.24000 | |
240 %! ]; | |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
241 %! assert (result, expected, 1e-5); |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
242 |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
243 %!test |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
244 %! result = legendre (3, [-1.0 -0.9 -0.8], "sch"); |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
245 %! expected = [ |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
246 %! -1.00000 -0.47250 -0.08000 |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
247 %! 0.00000 0.81413 0.80833 |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
248 %! -0.00000 -0.33114 -0.55771 |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
249 %! 0.00000 0.06547 0.17076 |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
250 %! ]; |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
251 %! assert (result, expected, 1e-5); |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
252 |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
253 %!test |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
254 %! result = legendre (3, [-1.0 -0.9 -0.8], "norm"); |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
255 %! expected = [ |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
256 %! -1.87083 -0.88397 -0.14967 |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
257 %! 0.00000 1.07699 1.06932 |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
258 %! -0.00000 -0.43806 -0.73778 |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
259 %! 0.00000 0.08661 0.22590 |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
260 %! ]; |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
261 %! assert (result, expected, 1e-5); |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
262 |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
263 %!test |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
264 %! result = legendre (151, 0); |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
265 %! ## Don't compare to "-Inf" since it would fail on 64 bit systems. |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
266 %! assert (result(end) < -1.7976e308 && all (isfinite (result(1:end-1)))); |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
267 |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
268 %!test |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
269 %! result = legendre (150, 0); |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
270 %! ## This agrees with Matlab's result. |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
271 %! assert (result(end), 3.7532741115719e+306, 0.0000000000001e+306) |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
272 |
8255
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
273 %!test |
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
274 %! result = legendre (0, 0:0.1:1); |
8975 | 275 %! assert (result, full(ones(1,11))) |
10672
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
276 |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
277 %% Check correct invocation |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
278 %!error legendre (); |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
279 %!error legendre (1); |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
280 %!error legendre (1,2,3,4); |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
281 %!error legendre ([1, 2], [-1, 0, 1]); |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
282 %!error legendre (-1, [-1, 0, 1]); |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
283 %!error legendre (1.1, [-1, 0, 1]); |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
284 %!error legendre (1, [-1+i, 0, 1]); |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
285 %!error legendre (1, [-2, 0, 1]); |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
286 %!error legendre (1, [-1, 0, 2]); |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
287 %!error legendre (1, [-1, 0, 1], "badnorm"); |