-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrightOmegaq.m
513 lines (467 loc) · 20.3 KB
/
wrightOmegaq.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
function W=wrightOmegaq(Z)
%WRIGHTOMEGAQ Wright omega function, a solution of the equation W+LOG(W) = Z.
% W = WRIGHTOMEGAQ(Z) performs floating point evaluation of the Wright omega
% function. Z is an array and may be complex. If Z is an array of symbolic
% values, it is converted to double-precision for computation and then recast
% as symbolic.
%
% Example:
% % Plot magnitude of the function surface over the complex plane
% v = -6*pi:3*pi/25:6*pi; [x,y] = meshgrid(v);
% w = wrightOmegaq(x+y*1i);
%
% figure('Renderer','zbuffer'); surf(v,v,abs(w)); colormap(hot(256));
% shading flat; axis square; xlabel('X'); ylabel('Y');
% zlabel('|\omega(X+Yi)|'); title('Wright \omega Function')
%
% Note:
% Due to numerical precision and the nature of this equation, the inverse
% of the Wright omega funtion, may not always return a value close (in an
% absolute sense) to Z, e.g., Z <= -714.84989998498+Y*1i, -pi < Y <= pi.
%
% Class support for Z:
% float: double, single
% symbolic
%
% See also: WRIGHTOMEGA, LAMBERTW
% Based on:
%
% [1] Piers W. Lawrence, Robert M. Corless, and David J. Jeffrey, "Algorithm
% 917: Complex Double-Precision Evaluation of the Wright omega Function," ACM
% Transactions on Mathematical Software, Vol. 38, No. 3, Article 20, pp. 1-17,
% Apr. 2012. http://dx.doi.org/10.1145/2168773.2168779
%
% [2] Robert M. Corless and David J. Jeffrey, "The Wright omega Function," In:
% Artificial Intelligence, Automated Reasoning, and Symbolic Computation,
% Joint International Conferences, AISC 2002 and Calculemus 2002, Marseille,
% France, July 2002, (Jacques Calmet, Belaid Benhamou, Olga Caprotti, Laurent
% Henocque, and Volker Sorge, Eds.), Berlin: Springer-Verlag, pp. 76-89, 2002.
% http://orcca.on.ca/TechReports/2000/TR-00-12.html
%
% Numbers in parentheses below refer to equations in Lawrence, et al. 2012.
% The inverse Wright omega function is defined as (Corless & Jeffrey, 2002):
% Z = W+LOG(W)-2*pi*1i, -Inf < REAL(W) < -1, IMAG(W) = 0
% Z = -1+/-pi*1i, W = -1
% Z = W+LOG(W), otherwise
% The solution of W+LOG(W) = Z is given by (Corless & Jeffrey, 2002):
% W = WRIGHTOMEGA(Z), Z ~= t+/-pi*1i, t <= -1
% W = WRIGHTOMEGA(Z),WRIGHTOMEGA(Z-2*pi*1i), Z = t+pi*1i, t <= -1
% W = NaN, Z = t-pi*1i, t <= -1
% WRIGHTOMEGAQ is up three to four orders of magnitude faster than WRIGHTOMEGA
% for double-precision arrays. Additionally, it has much less numeric error,
% properly evaluates values greater than 2^28, supports single-precision
% evaluation, and handles NaN inputs.
% Andrew D. Horchler, horchler @ gmail . com, Created 7-12-12
% Revision: 1.0, 3-12-13
% Convert symbolic input, converted back at end
isSym = isa(Z,'sym') || ischar(Z);
if isSym
try
Z = double(sym(Z));
catch ME
if strcmp(ME.identifier,'symbolic:sym:double:cantconvert')
error('SHCTools:wrightOmegaq:InvalidSymbolicZ',...
['Symbolic input Z must contain only numeric values and no '...
'expressions containing variables.'])
else
rethrow(ME);
end
end
elseif ~isfloat(Z)
error('SHCTools:wrightOmegaq:InvalidZ',...
'Input Z must be an array of floating point or symbolic values.');
end
% Support for single precision: single(pi) ~= pi
dataType = class(Z);
PI = cast(pi,dataType);
PI2 = cast(pi/2,dataType);
EXP1 = cast(exp(1),dataType);
EXP2 = cast(exp(2),dataType);
LN2 = cast(log(2),dataType);
OMEGA = cast(0.5671432904097838,dataType);
ONE_THIRD = cast(1/3,dataType);
tol = eps(dataType);
X = real(Z(:));
Y = imag(Z(:));
if isempty(Z) || all(isnan(Z(:)))
W = Z;
elseif isscalar(Z)
% Special values
if Z > 2^59
W = Z; % W self-saturates: X > 2^59 (abs(Y) > 2^54 too)
elseif Z == 0
W(1) = OMEGA; % Omega constant
elseif Z == 1
W(1) = 1;
elseif Z == 1+EXP1
W(1) = EXP1;
elseif isreal(Z) || Y == 0
if Z < log(eps(realmin(dataType)))-LN2
W(1) = 0; % Z -> -Inf
else
% W(1) used in order retain datatype
if Z <= -2
% Region 3: series about -Inf
x = exp(X);
W(1) = x*(1-x*(1-x*(36-x*(64-125*x))/24)); % (24)
%fprintf(1,'W: %.20f\n',W);
% Series is exact, X < -exp(2)
if X < -EXP2
return;
end
elseif Z > PI+1
% Region 7: log series about Z = Inf
x = log(X);
lzi = x/X;
W(1) = X-x+lzi*(1+lzi*(0.5*x-1 ...
+lzi*((ONE_THIRD*x-1.5)*x+1))); % (25)
else
% Region 4: series about Z = 1
x = X-1;
W(1) = 1+x*(1/2+x*(1/16-x*(1/192+x*(1/3072 ...
-(13/61440)*x)))); % (29)
end
% Residual
r = X-(W+log(W)); % (14)
%fprintf(1,'W: %.20f\n',W);
%fprintf(1,'r: %.20f\n',r);
if abs(r) > tol
% FSC-type iteration, N = 3, (Fritsch, Shafer, & Crowley, 1973)
w1 = 1+W;
w2 = w1+2*ONE_THIRD*r;
W = W*(1+r*(w1*w2-0.5*r)/(w1*(w1*w2-r))); % (15)
% Test residual
r = X-(W+log(W)); % (14)
%sprintf('Zr: %.20f\n',X)
%fprintf(1,'W: %.20f\n',W);
%fprintf(1,'ln W: %.20f\n',log(W));
%fprintf(1,'r: %.20f\n',r);
%sprintf('W+ln W: %.20f\n',W + log(W))
%sprintf('r: %.20f',r)
% Second iterative improvement via FSC method, if needed
if abs(r) > tol
w1 = 1+W;
w2 = w1+2*ONE_THIRD*r;
W = W*(1+r*(w1*w2-0.5*r)/(w1*(w1*w2-r))); % (15)
end
end
end
else
% Special complex values
if Z < log(eps(realmin(dataType)))-LN2 && Y > -PI && Y <= PI
W(1) = 0; % Z -> -Inf
elseif Z == -1+PI*1i || Z == -1-PI*1i
W(1) = -1;
elseif Z == log(ONE_THIRD)-ONE_THIRD+PI*1i
W(1) = -ONE_THIRD;
elseif Z == LN2-2-PI*1i
W(1) = -2;
elseif Z == (1+PI2)*1i
W(1) = 1i;
else
xgtm2 = (X > -2);
% W(1) used in order retain datatype
if X <= -1 && abs(Y) == PI
% Upper and lower lines of discontinuity
if Y == PI
% Region 3: series about -Inf
x = exp(X);
W(1) = ((((-125*x-64)*x-36)*x/24-1)*x-1)*x; % (24)
% Series is exact, X < -exp(2)
if X < -EXP2
return;
end
elseif xgtm2
% Regions 1 and 2: near Z = -1+pi*1i and Z = -1-pi*1i
x = sign(Y)*sqrt(-2*(X+1)); % (20, 22)
W(1) = -1+x*(1-x*(1440-x*(120+x*(16+x)))*(1/4320)); % (21, 23)
else
% Region 6: negative log series about Z = Z+pi*1i
x = log(-X);
lzi = x/X;
W(1) = X-x+lzi*(1+lzi*(0.5*x-1+lzi*((ONE_THIRD*x-1.5)*x+1))); % (28)
end
%fprintf(1,'W: %.20f\n',W);
% Regularization: adjust Z and flip sign of W
Z = X;
s = -1;
else
xlteq1 = (X <= 1);
r12x = (xgtm2 && xlteq1);
if ~xgtm2 && Y > -PI && Y < PI
% Region 3: series about -Inf, within lines of discontinuity
x = exp(Z);
%fprintf(1,'x: %.30f%+.30f\n',real(x),imag(x));
W(1) = x*(1-x*(1-x*(36-x*(64-125*x))*(1/24))); % (24)
%fprintf(1,'W: %.30f%+.30f\n',real(W),imag(W));
% Series is exact, X < -exp(2)
if X < -EXP2
return;
end
elseif r12x && Y > PI2 && Y < 3*PI2
% Region 1: near Z = -1+pi*1i, but not line of discontinuity
x = conj(sqrt(2*conj(Z+1-PI*1i))); % (20)
%fprintf(1,'x: %.30f%+.30f\n',real(x),imag(x));
%W(1) = -1+x*1i+x*x*(1440+16*x*x ...
% +(x*x-120)*x*1i)*(1/4320);
%W(1) = -1+x*1i+(1440*x*x+16*x*x*x*x ...
% +(x*x-120)*x*x*x*1i)/4320;
%W(1) = -1+x*1i+(1440*(x*x)-120*(x*(x*(x*1i)))+16*(x*(x*(x*x))) ...
% +x*(x*(x*(x*(x*1i)))))/4320;
W(1) = -1+x*(1i+x*(1440-x*(120*1i ...
-x*(16+x*1i)))*(1/4320)); % (21)
%fprintf(1,'W: %.30f%+.30f\n',real(W),imag(W));
elseif r12x && Y > -3*PI2 && Y < -PI2
% Region 2: near Z = -1-pi*1i, but not line of discontinuity
x = conj(sqrt(2*conj(Z+1+PI*1i))); % (22)
W(1) = -1-x*(1i-x*(1440+x*(120*1i ...
+x*(16-x*1i)))*(1/4320)); % (23)
%W(1) = -1-x*1i+x*x*(1440+16*x*x ...
% +(120-x*x)*x*1i)/4320;
elseif r12x && abs(Y) <= PI2 || ~xlteq1 && (X-1)^2+Y^2 <= PI^2
% Region 4: series about Z = 1
x = Z-1;
W(1) = 1+x*(1/2+x*(1/16-x*(1/192+x*(1/3072 ...
-(13/61440)*x)))); % (29)
elseif ~xgtm2 && Y > PI && Y-PI <= -0.75*(X+1)
% Region 5: negative log series about t = Z-pi*1i
t = Z-PI*1i; % (26)
x = log(-t);
lzi = x/t;
W(1) = t-x+lzi*(1+lzi*(0.5*x-1+lzi*((ONE_THIRD*x-1.5)*x+1))); % (28)
elseif ~xgtm2 && Y < -PI && Y+PI >= 0.75*(X+1)
% Region 6: negative log series about t = Z+pi*1i
t = Z+PI*1i; % (26)
x = log(-t);
lzi = x/t;
W(1) = t-x+lzi*(1+lzi*(0.5*x-1+lzi*((ONE_THIRD*x-1.5)*x+1))); % (28)
else
% Region 7: log series about Z = Inf
x = log(Z);
%fprintf(1,'x: %.30f%+.30f\n',real(x),imag(x));
lzi = x/Z;
%fprintf(1,'lzi: %.30f%+.30f\n',real(lzi),imag(lzi));
W(1) = Z-x+lzi*(1+lzi*(0.5*x-1+lzi*((ONE_THIRD*x-1.5)*x+1))); % (25)
%fprintf(1,'W: %.30f%+.30f\n',real(W),imag(W));
end
% Check for regularization: adjust Z and flip sign of W
if X <= -0.99 && abs(Y-PI) <= 0.01
Z = Z-PI*1i; % (26)
s = -1;
elseif X <= -0.99 && abs(Y+PI) <= 0.01
Z = Z+PI*1i; % (26)
s = -1;
else
s = 1;
end
end
% Residual (can be zero)
r = Z-(W+log(s*W)); % (14)
%fprintf(1,'r: %.20f\n',r);
if abs(r) > tol
% FSC-type iteration, N = 3, (Fritsch, Shafer, & Crowley, 1973)
%W = W*(1+(r/(1+W))*((1+W)*(1+W+2*ONE_THIRD*r)...
% -0.5*r)/((1+W)*(1+W+2*ONE_THIRD*r)-r)); % (15)
w1 = 1+W;
w2 = w1+2*ONE_THIRD*r;
W = W*(1+r*(w1*w2-0.5*r)/(w1*(w1*w2-r)));
%W = W*(1+(r/(1+W))*((1+W)*(1+W+2*ONE_THIRD*r)...
% -0.5*r)/((1+W)*(1+W+2*ONE_THIRD*r)-r)); % (15)
% Test residual
r = Z-(W+log(s*W)); % (14)
% Second iterative improvement via FSC method, if needed
if abs(r) > tol
w1 = 1+W;
w2 = w1+2*ONE_THIRD*r;
W = W*(1+r*(w1*w2-0.5*r)/(w1*(w1*w2-r)));
%W = W*(1+(r/(1+W))*((1+W)*(1+W+2*ONE_THIRD*r)...
% -0.5*r)/((1+W)*(1+W+2*ONE_THIRD*r)-r)); % (15)
end
end
end
end
else
isRealZ = (isreal(Z) || all(Y == 0));
if isRealZ
W = NaN(size(Z),dataType);
else
W = complex(NaN(size(Z),dataType));
end
% Special values
W(Z == 0) = 0.5671432904097838; % Omega constant
W(Z == 1) = 1;
W(Z == 1+exp(1)) = exp(1);
W(Z > 2^59) = Z(Z > 2^59); % W self-saturates: X > 2^59 (abs(Y) > 2^54 too)
if isRealZ
MinZ = (Z < log(eps(realmin(dataType)))-log(2));
W(MinZ) = 0; % Z -> -Inf
else
MinZ = (Z(:) < log(eps(realmin(dataType)))-log(2) & Y > -PI & Y <= PI);
W(MinZ) = 0; % Z -> -Inf
W(Z == -1+PI*1i | Z == -1-PI*1i) = -1;
W(Z == log(1/3)-1/3+PI*1i) = -1/3;
W(Z == log(2)-2-PI*1i) = -2;
W(Z == (1+PI/2)*1i) = 1i;
end
i = (isnan(W(:)) & ~isnan(Z(:)));
if any(i)
if isRealZ
% Region 3: series about -Inf
j = (i & X <= -2);
if any(j)
x = exp(X(j));
W(j) = x.*(1-x.*(1-x.*(36-x.*(64-125*x))/24)); % (24)
% Series is exact, X < -exp(2)
if all(~i | X < -7.38905609893065)
return;
end
end
% Region 7: log series about Z = Inf
j = (~j & X > pi+1);
if any(j)
t = X(j);
x = log(t);
lzi = x./t;
W(j) = t-x+lzi.*(1+lzi.*(x/2-1+lzi.*((x/3-3/2).*x+1))); % (25)
end
% Region 4: series about Z = 1
j = (~j & X > -2);
if any(j)
x = X(j)-1;
W(j) = 1+x.*(1/2+x.*(1/16-x.*(1/192+x.*(1/3072 ...
-(13/61440)*x)))); % (29)
end
% No regularization
Z = real(Z);
s = 1;
else
ypi = (Y == PI);
ympi = (Y == -PI);
xgtm2 = (X > -2);
xlteq1 = (X <= 1);
r12x = (i & xgtm2 & xlteq1);
% Region 3b: series about -Inf, upper line of discontinuity
c = (i & ~xgtm2 & ypi);
if any(c)
x = exp(X(c));
W(c) = ((((-125*x-64).*x-36).*x/24-1).*x-1).*x; % (24)
% Series is exact, X < -exp(2)
i = (i & X >= -7.38905609893065);
if all(~i)
return;
end
end
c7 = c;
% Region 3: series about -Inf, Z between lines of discontinuity
c = (i & ~xgtm2 & Y > -PI & Y < PI);
if any(c)
x = exp(Z(c));
W(c) = x.*(1-x.*(1-x.*(36+x.*(64+125*x))/24)); % (24)
% Series is exact, X < -exp(2)
if all(~i | X < -7.38905609893065)
return;
end
end
c7 = (c7 | c);
% Regions 1b and 2b: near Z = -1+/-pi*1i, lines of discontinuity
c = (i & xgtm2 & X <= -1 & (ypi | ympi));
if any(c)
x = sign(Y(c)).*sqrt(-2*(X(c)+1)); % (20, 22)
W(c) = -1+x.*(1-x.*(1440-x.*(120+x.*(16+x)))/4320); % (21, 23)
end
c7 = (c7 | c);
% Region 1: near Z = -1+pi*1i, but not upper line of discontinuity
c = (~ypi & r12x & Y > PI/2 & Y < 3*PI/2);
if any(c)
x = conj(sqrt(2*conj(Z(c)+1-PI*1i))); % (20)
W(c) = -1+x*1i+(1440*x.^2-120*x.^3*1i+16*x.^4 ...
+x.^5*1i)/4320; % (21)
end
c7 = (c7 | c);
% Region 2: near Z = -1-pi*1i, but not lower line of discontinuity
c = (~ympi & r12x & Y > -3*PI/2 & Y < -PI/2);
if any(c)
x = conj(sqrt(2*conj(Z(c)+1+PI*1i))); % (22)
W(c) = -1-x*1i+(1440*x.^2+120*x.^3*1i+16*x.^4 ...
-x.^5*1i)/4320; % (23)
end
c7 = (c7 | c);
% Region 4: series about Z = 1
c = (r12x & abs(Y) <= PI/2 | ~xlteq1 & (X-1).^2+Y.^2 <= PI^2);
if any(c)
x = Z(c)-1;
W(c) = 1+x.*(1/2+x.*(1/16-x.*(1/192+x.*(1/3072 ...
-(13/61440)*x)))); % (29)
end
c7 = (c7 | c);
% Region 5: negative log series about t = Z-pi*1i
c = (i & ~xgtm2 & Y > PI & Y-PI <= -(3/4)*(X+1));
if any(c)
t = Z(c)-PI*1i; % (26)
x = log(-t);
lzi = x./t;
W(c) = t-x+lzi.*(1+lzi.*(x/2-1+lzi.*((x/3-3/2).*x+1))); % (28)
end
c7 = (c7 | c);
% Region 6b: negative log series about t = Z+pi*1i
c = (i & ~xgtm2 & ympi);
if any(c)
t = X(c); % (26)
x = log(-t);
lzi = x./t;
W(c) = t-x+lzi.*(1+lzi.*(x/2-1+lzi.*((x/3-3/2).*x+1))); % (28)
end
c7 = (c7 | c);
% Region 6: negative log series about t = Z+pi*1i
c = (i & ~xgtm2 & Y < -PI & Y+PI >= (3/4)*(X+1));
if any(c)
t = Z(c)+PI*1i; % (26)
x = log(-t);
lzi = x./t;
W(c) = t-x+lzi.*(1+lzi.*(x/2-1+lzi.*((x/3-3/2).*x+1))); % (28)
end
c7 = (c7 | c);
% Region 7: log series about Z = Inf
c = (i & ~(c7 | c));
if any(c)
t = Z(c);
x = log(t);
lzi = x./t;
W(c) = t-x+lzi.*(1+lzi.*(x/2-1+lzi.*((x/3-3/2).*x+1))); % (25)
end
% Check for regularization: adjust Z and flip sign of W
s1 = (X <= -0.99 & abs(Y-PI) <= 1e-2);
Z(s1) = Z(s1)-PI*1i; % (26)
s2 = (X <= -0.99 & abs(Y+PI) <= 1e-2);
Z(s2) = Z(s2)+PI*1i; % (26)
s = ones(size(W));
s(s1 | s2) = -1;
end
% Residual (can be zero)
r = Z-(W+log(s.*W)); % (14)
r(MinZ) = 0;
% FSC-type iteration, N = 3, (Fritsch, Shafer, & Crowley, 1973)
rc = (abs(r(:)) > tol);
if any(rc)
wr = W(rc);
r = r(rc);
W(rc) = wr.*(1+(r./(1+wr)).*((1+wr).*(1+wr+(2/3)*r)...
-r/2)./((1+wr).*(1+wr+(2/3)*r)-r)); % (15)
% Test residual
r = Z-(W+log(s.*W)); % (14)
r(MinZ) = 0;
rc = (abs(r(:)) > tol);
if any(rc)
wr = W(rc);
r = r(rc);
% Second iterative improvement via FSC method, if needed
W(rc) = wr.*(1+(r./(1+wr)).*((1+wr).*(1+wr+(2/3)*r)...
-r/2)./((1+wr).*(1+wr+(2/3)*r)-r)); % (15)
end
end
end
end
% Reconvert symbolic input
if isSym
W = sym(W,'d');
end