-
Notifications
You must be signed in to change notification settings - Fork 0
/
legendre_derivative.m
226 lines (194 loc) · 8.36 KB
/
legendre_derivative.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
% LEGENDRE_DERIVATIVE Compute first derivative of (normalized)
% associated Legendre polynomial
%
% Computing accurate derivatives of associated Legendre polynomials can
% be tricky. Even in advanced texts, they are usually written as recurrence
% relations and/or with (normalization) factors involving factorials.
%
% A naive implementation will therefore quickly run into the limits of
% IEEE754 double-precision, resulting in NaN/inf or significant loss of
% precision, already at relatively low degree N.
%
% LEGENDRE_DERIVATIVE is a fully vectorized, numerically stable and
% robustly validated implementation of the derivative computation. It
% allows fast and accurate computations of the derivatives for any degree N.
%
% LEGENDRE_DERIVATIVE works the same as MATLAB's own LEGENDRE, except it
% does not compute the polynomial values, but the values of the derivatives.
%
%
% USAGE:
% ----------------
% dPnmdx = legendre_derivative(N,X);
% dPnmdx = legendre_derivative(N,X, normalization);
% dPnmdx = legendre_derivative(N,Pnm,X);
% dPnmdx = legendre_derivative(N,Pnm,X, normalization);
%
% INPUT ARGUMENTS:
% ----------------
%
% N : degree of the Legendre polynomial.
% X : array of points at which to evaluate the derivatives.
% Pnm : values of the Legendre polynomial at X; these are computed
% automatically when omitted. Since the derivatives only
% depend on the degree/order N,M and the values of the
% polynomials Pnm(x), providing these values if they are
% already computed elsewhere will thus improve performance.
% normalization: type of normalization to use. Can be equal to 'unnorm'
% (default), 'norm' (fully normalized) or 'sch' (Schmidt
% semi-normalized).
%
% OUTPUT ARGUMENTS:
% ----------------
% dPnmdx : value(s) of the derivative of the n-th order associated Legendre
% polynomial at all location(s) X, for all degrees M=0..N. The array
% dPnmdx has one more dimension than x; each element
% dPnmdx(M+1,i,j,k,...) contains the associated Legendre function of
% degree N and order M evaluated at X(i,j,k,...).
%
% See also legendre.
% Copyright (c) 2018, Rody Oldenhuis
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:
%
% 1. Redistributions of source code must retain the above copyright notice, this
% list of conditions and the following disclaimer.
% 2. Redistributions in binary form must reproduce the above copyright notice,
% this list of conditions and the following disclaimer in the documentation
% and/or other materials provided with the distribution.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
% ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
% WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
% DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
% ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
% (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
% ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
% (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
% The views and conclusions contained in the software and documentation are those
% of the authors and should not be interpreted as representing official policies,
% either expressed or implied, of this project.
function dPnmdx = legendre_derivative(varargin)
% Please report bugs and inquiries to the author:
%
% Name : Rody P.S. Oldenhuis
% E-mail : [email protected]
% Licence: 3-clause BSD
%% Initialization
% Parse input, do some checks
if verLessThan('MATLAB', '7.3')
error(nargchk(2, 4, nargin, 'struct')); %#ok<*NCHKN>
else
narginchk(2,4);
end
n = varargin{1};
varargin = varargin(2:end);
assert(isnumeric(n) && isscalar(n) && isfinite(n) && isreal(n) && round(n)==n && n>=0,...
[mfilename ':invalid_n'],...
'Degree N must be a positive scalar integer.');
normalization = 'unnorm';
if ischar(varargin{end})
normalization = varargin{end};
varargin(end) = [];
end
assert(any(strcmpi(normalization, {'unnorm','norm','sch'})),...
[mfilename ':unsupported_normalization'],...
'Unsupported normalization specified: ''%s''.', normalization);
x = varargin{end};
varargin(end) = [];
assert(~isempty(x) && isnumeric(x) && ...
all(isreal(x(:))) && all(abs(x(~isnan(x(:)))) <= 1),...
[mfilename ':invalid_x'],...
'X must be real, numeric, nonempty, and in the range (-1,1).');
if isempty(varargin)
Pnm = legendre(n,x,normalization);
else
Pnm = varargin{end};
end
assert(size(Pnm,1)==n+1,...
[mfilename ':Pnm_dimension_mismatch'],...
'Dimensions of polynomial values Pnm disagrees with degree N.');
% Special case; MATLAB does not normally reshape the Pnm vector
% for size(x) = 1×p
szPnm = size(Pnm);
szX = size(x);
ndx = ndims(x);
assert(isscalar(x) && szPnm(2)==1 || ...
isequal(szX(1:find(szX ~= 1, 1, 'last')), szPnm(2:end)) || ...
(szX(1) == 1 && ndx == 2 && szX(2) ~= 1 && szPnm(2)==szX(2)),...
[mfilename ':Pnm_dimension_mismatch'],...
'Dimensions of polynomial values Pnm disagrees with vector x.');
%% Computation
% n==0 case
if numel(Pnm)==1
dPnmdx = zeros(size(x));
return;
end
% Initialize some arrays for vectorization
x = permute(x, [ndx+1 1:ndx]);
idx = repmat({':'}, ndx,1);
m = (0:n).';
sqx = 1 - x.^2;
% Special case; MATLAB does not normally reshape the Pnm vector
% for size(x) = 1×p
if szX(1)==1 && ndx == 2
Pnm = permute(Pnm, [1 3 2]); end
% Normalization factors: this is actually a nice puzzle :)
F = -ones(n+1,1);
if ~strcmpi(normalization,'unnorm')
% Factors for both normalizations are the same...
s = 1/n/(n+1);
for ii = m.'
F(ii+1) = s;
s = s * (n-ii+1)/(n+ii+1)*(n+ii)/(n-ii);
end
%... save for the first few entries
if numel(F)>1
switch normalization
case 'norm'
F(1) = 1/F(2);
case 'sch'
F(1) = 1/2/F(2);
F(2) = 1/F(1);
end
end
F = sqrt(F);
end
% Compute derivative, vectorized, efficient.
% Sadly, that means it's unreadable.
dPnmdx = bsxfun(@rdivide, Pnm .* bsxfun(@times, m, x) - ...
bsxfun(@times, F, ...
[-Pnm(2,idx{:})/n/(n+1)
+Pnm(1:end-1,idx{:})]...
) .* ...
bsxfun(@times, (n+m).*(n-m+1), sqrt(sqx)), ...
sqx);
% Handle edge cases
ispolar = abs(x)==1;
if any(ispolar)
xp = x(ispolar);
pwr = +xp.^(1+n);
dPnmdx(4:end,ispolar) = 0;
switch normalization
case 'norm'
dPnmdx(1,ispolar) = +xp.*pwr .* Pnm(1,:) .* n*(n+1)/2;
dPnmdx(2,ispolar) = -xp.*pwr * inf;
if n > 1
dPnmdx(3,ispolar) = -pwr.* sqrt(n*(n+1)/(F(3)+4))*abs(Pnm(1))*F(1)/2; end
case 'sch'
dPnmdx(1,ispolar) = +pwr .* n*(n+1)/2;
dPnmdx(2,ispolar) = -xp.*pwr*inf;
if n > 1
dPnmdx(3,ispolar) = -pwr .* sqrt(n*(n+1)/8)/F(3); end
otherwise
dPnmdx(1,ispolar) = +pwr .* n*(n+1)/2;
dPnmdx(2,ispolar) = +xp.*pwr*inf;
if n > 1
dPnmdx(3,ispolar) = -pwr * (n-1)*n*(n+1)*(n+2)/4; end
end
end
end