Mercurial > hg > octave-lyh
annotate scripts/specfun/legendre.m @ 11305:c9df571efe95
subplot.m: Add suppport for "align" and "replace" options.
author | Ben Abbott <bpabbott@mac.com> |
---|---|
date | Thu, 02 Dec 2010 18:46:10 -0500 |
parents | be55736a0783 |
children | e7ed20f87e82 |
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 ## |
36 ## @example | |
37 ## @group | |
38 ## m m 2 m/2 d^m | |
39 ## P(x) = (-1) * (1-x ) * ---- P (x) | |
40 ## n dx^m n | |
41 ## @end group | |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
42 ## @end example |
5827 | 43 ## |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
44 ## @noindent |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
45 ## with Legendre polynomial of degree @var{n}: |
5827 | 46 ## |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
47 ## @example |
5827 | 48 ## @group |
49 ## 1 d^n 2 n | |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
50 ## P (x) = ------ [----(x - 1) ] |
5827 | 51 ## n 2^n n! dx^n |
52 ## @end group | |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
53 ## @end example |
5827 | 54 ## |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
55 ## @noindent |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
56 ## @code{legendre (3, [-1.0, -0.9, -0.8])} returns the matrix: |
5827 | 57 ## |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
58 ## @example |
5827 | 59 ## @group |
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 | |
66 ## @end group | |
67 ## @end example | |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
68 ## |
7508 | 69 ## If the optional argument @code{normalization} is @code{"sch"}, |
70 ## 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
|
71 ## 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
|
72 ## 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
|
73 ## |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
74 ## 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
|
75 ## |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
76 ## @example |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
77 ## @group |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
78 ## 0 0 |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
79 ## SP (x) = P (x) |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
80 ## n n |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
81 ## @end group |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
82 ## @end example |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
83 ## |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
84 ## 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
|
85 ## |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
86 ## @example |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
87 ## @group |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
88 ## 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
|
89 ## SP (x) = P (x) * (-1) * [-------] |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
90 ## n n (n+m)! |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
91 ## @end group |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
92 ## @end example |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
93 ## |
7508 | 94 ## If the optional argument @var{normalization} is @code{"norm"}, |
95 ## 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
|
96 ## 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
|
97 ## 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
|
98 ## |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
99 ## 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
|
100 ## |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
101 ## @example |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
102 ## @group |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
103 ## 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
|
104 ## NP (x) = P (x) * (-1) * [-------------] |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
105 ## n n (n+m)! |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
106 ## @end group |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
107 ## @end example |
5827 | 108 ## @end deftypefn |
109 | |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
110 ## Author: Marco Caliari <marco.caliari@univr.it> |
5827 | 111 |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
112 function retval = legendre (n, x, normalization) |
5827 | 113 |
8255
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
114 persistent warned_overflow = false; |
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
115 |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
116 if (nargin < 2 || nargin > 3) |
5827 | 117 print_usage (); |
118 endif | |
119 | |
10672
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
120 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
|
121 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
|
122 endif |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
123 |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
124 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
|
125 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
|
126 endif |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
127 |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
128 if (nargin == 3) |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
129 normalization = lower (normalization); |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
130 else |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
131 normalization = "unnorm"; |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
132 endif |
5827 | 133 |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
134 switch (normalization) |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
135 case "norm" |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
136 scale = sqrt (n+0.5); |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
137 case "sch" |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
138 scale = sqrt (2); |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
139 case "unnorm" |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
140 scale = 1; |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
141 otherwise |
10672
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
142 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
|
143 endswitch |
5827 | 144 |
10672
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
145 if (rows (x) != 1) |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
146 x = x'; |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
147 endif |
8255
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
148 scale = scale * ones (1, numel (x)); |
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
149 |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
150 ## Based on the recurrence relation below |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
151 ## m m m |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
152 ## (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
|
153 ## n+1 n n-1 |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
154 ## http://en.wikipedia.org/wiki/Associated_Legendre_function |
5827 | 155 |
8255
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
156 overflow = false; |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
157 for m = 1:n |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
158 lpm1 = scale; |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
159 lpm2 = (2*m-1) .* x .* scale; |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
160 lpm3 = lpm2; |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
161 for k = m+1:n |
8255
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
162 lpm3a = (2*k-1) .* x .* lpm2; |
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
163 lpm3b = (k+m-2) .* lpm1; |
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
164 lpm3 = (lpm3a - lpm3b)/(k-m+1); |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
165 lpm1 = lpm2; |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
166 lpm2 = lpm3; |
8255
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
167 if (! warned_overflow) |
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
168 if (any (abs (lpm3a) > realmax) |
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
169 || any (abs (lpm3b) > realmax) |
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
170 || any (abs (lpm3) > realmax)) |
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
171 overflow = true; |
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
172 endif |
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
173 endif |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
174 endfor |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
175 retval(m,:) = lpm3; |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
176 if (strcmp (normalization, "unnorm")) |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
177 scale = -scale * (2*m-1); |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
178 else |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
179 ## normalization == "sch" or normalization == "norm" |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
180 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
|
181 endif |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
182 scale = scale .* sqrt(1-x.^2); |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
183 endfor |
5827 | 184 |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
185 retval(n+1,:) = scale; |
5827 | 186 |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
187 if (strcmp (normalization, "sch")) |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
188 retval(1,:) = retval(1,:) / sqrt (2); |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
189 endif |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
190 |
8255
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
191 if (overflow && ! warned_overflow) |
8664 | 192 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
|
193 warned_overflow = true; |
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
194 endif |
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
195 |
5827 | 196 endfunction |
197 | |
198 %!test | |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
199 %! result = legendre (3, [-1.0 -0.9 -0.8]); |
5827 | 200 %! expected = [ |
201 %! -1.00000 -0.47250 -0.08000 | |
202 %! 0.00000 -1.99420 -1.98000 | |
203 %! 0.00000 -2.56500 -4.32000 | |
204 %! 0.00000 -1.24229 -3.24000 | |
205 %! ]; | |
7507
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
206 %! assert (result, expected, 1e-5); |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
207 |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
208 %!test |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
209 %! 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
|
210 %! expected = [ |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
211 %! -1.00000 -0.47250 -0.08000 |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
212 %! 0.00000 0.81413 0.80833 |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
213 %! -0.00000 -0.33114 -0.55771 |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
214 %! 0.00000 0.06547 0.17076 |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
215 %! ]; |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
216 %! assert (result, expected, 1e-5); |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
217 |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
218 %!test |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
219 %! 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
|
220 %! expected = [ |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
221 %! -1.87083 -0.88397 -0.14967 |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
222 %! 0.00000 1.07699 1.06932 |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
223 %! -0.00000 -0.43806 -0.73778 |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
224 %! 0.00000 0.08661 0.22590 |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
225 %! ]; |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
226 %! assert (result, expected, 1e-5); |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
227 |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
228 %!test |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
229 %! result = legendre (151, 0); |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
230 %! ## 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
|
231 %! 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
|
232 |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
233 %!test |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
234 %! result = legendre (150, 0); |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
235 %! ## This agrees with Matlab's result. |
bc6573d2fa40
legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
236 %! 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
|
237 |
8255
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
238 %!test |
3f1199ad212f
legendre.m: Warn once on under/overflow.
Ben Abbott <bpabbott@mac.com>
parents:
7517
diff
changeset
|
239 %! result = legendre (0, 0:0.1:1); |
8975 | 240 %! 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
|
241 |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
242 %% Check correct invocation |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
243 %!error legendre (); |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
244 %!error legendre (1); |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
245 %!error legendre (1,2,3,4); |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
246 %!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
|
247 %!error legendre (-1, [-1, 0, 1]); |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
248 %!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
|
249 %!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
|
250 %!error legendre (1, [-2, 0, 1]); |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
251 %!error legendre (1, [-1, 0, 2]); |
1cd7c39a96ee
legendre.m: Orient row vector correctly (bug #29997).
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
252 %!error legendre (1, [-1, 0, 1], "badnorm"); |