Arxiv:1209.0735V2 [Cs.MS] 7 Jan 2018 Running Time: the Tests Provided Take Only a Few Seconds to Run
Total Page:16
File Type:pdf, Size:1020Kb
Lambert W Function for Applications in Physics Darko Vebericˇ a,b aLaboratory for Astroparticle Physics, University of Nova Gorica, Slovenia bDepartment of Theoretical Physics, J. Stefan Institute, Ljubljana, Slovenia Abstract The Lambert W(x) function and its possible applications in physics are presented. The actual numerical implementa- tion in C++ consists of Halley’s and Fritsch’s iterations with initial approximations based on branch-point expansion, asymptotic series, rational fits, and continued-logarithm recursion. Keywords: Lambert W function, computational physics, numerical methods and algorithms, C++ Program summary Program title: LambertW Catalogue identifier: AENC v1 0 Program summary URL: http://cpc.cs.qub.ac.uk/summaries/AENC v1 0.html Program obtainable from: CPC Program Library, Queen’s University, Belfast, N. Ireland Licensing provisions: GNU General Public License version 3 No. of lines in distributed program, including test data, etc.: 1335 No. of bytes in distributed program, including test data, etc.: 25 283 Distribution format: tar.gz Programming language: C++ (with suitable wrappers it can be called from C, Fortran etc.), the supplied command-line utility is suitable for other scripting languages like sh, csh, awk, perl etc. Computer: All systems with a C++ compiler. Operating system: All Unix flavors, Windows. It might work with others. RAM: Small memory footprint, less than 1 MB Classification: 1.1, 4.7, 11.3, 11.9. Nature of problem: Find fast and accurate numerical implementation for the Lambert W function. Solution method: Halley’s and Fritsch’s iterations with initial approximations based on branch-point expansion, asymptotic series, rational fits, and continued logarithm recursion. Additional comments: Distribution file contains the command-line utility lambert-w. Doxygen comments, included in the source files. Makefile. arXiv:1209.0735v2 [cs.MS] 7 Jan 2018 Running time: The tests provided take only a few seconds to run. Source repository: https://github.com/DarkoVeberic/LambertW Email address: [email protected] (Darko Vebericˇ ) Preprint submitted to Computer Physics Communications September 5, 2012 1. Introduction 0 The Lambert W function is defined as the inverse W0 function of the -1 x 7! x ex (1) L x - 2 W- 1 mapping and thus solves the H W - 3 y ey = x (2) equation. This solution is given in the form of the Lam- - 4 bert W function, y = W(x), (3) - 0.4 - 0.2 0.0 0.2 0.4 0.6 0.8 1.0 and according to Eq. (2) it satisfies the following relation, x W(x) W(x) e = x. (4) Figure 1: The two branches of the Lambert W function, W−1(x) in blue −1 and W0(x) in red. The branching point at (−e , −1) is indicated with Since the mapping in Eq. (1) is not injective, no unique a green dash. inverse of the x ex function exists (except at the mini- mum). As can be seen in Fig.1, the Lambert W function inverse of Eddington-Finkelstein (tortoise) coordinates [16]), has two real branches with a branching point located at statistical mechanics [17], fluid dynamics [18], optics [19] (−e−1 − ) (x) , 1 . The bottom branch, W−1 , is defined in etc. x 2 [−e−1 ] the interval , 0 and has a negative singular- The main motivation for the implementation in this x ! − (x) ity for 0 . The upper branch W0 is defined for work comes from the research in cosmic ray physics where x 2 [−e−1 ] , ¥ . it has been in use already for several years [20] and new The earliest mention of the problem of Eq. (2) is at- interesting applications are appearing frequently [21]. In tributed to Euler [1]. However, Euler himself credited the next sections let us describe two new examples that Lambert for his previous work in this subject, Lambert’s arise from this field of astrophysics. transcendental equation [2]. The W(x) function started to be named after Lambert only recently, in the last 20 1.1. Inverse of the Moyal function years or so; nevertheless, the first usage of the letter W The Moyal function and the related distribution was appeared much earlier [3]. proposed as a good approximation for the more com- Recently, the W(x) function amassed quite a follow- plicated Landau distribution [22] used in descriptions of ing in the mathematical community [4]. Its most faithful energy loss of charged particles passing through matter proponents are suggesting elevating it among the present [23]. The un-normalized Moyal function is defined as set of elementary functions, such as sin(x), cos(x), exp(x), h i ln(x), etc. The main argument for doing so is the fact 1 −x M(x) = exp − 2 x + e (5) that W is the root of the simplest exponential polynomial function given in Eq. (2). We will acknowledge these ef- and can be seen in Fig.2 (top). forts by strict usage of a roman symbol W as its name. Its inverse can be written in terms of the two branches While the Lambert W function is called LambertW in of the Lambert W function, the mathematics software tool Maple [5] and lambertw in M−1(x) = W (−x2) − 2 ln x. (6) the Matlab programming environment [6], in the Math- ± 0,−1 ematica computer algebra framework this function [7] is In experimental astrophysics the Moyal function is implemented under the name ProductLog (in the recent used for phenomenological recovery of the saturated sig- versions an alias LambertW is also supported). In open nals from photomultipliers [24], where the largest parts source format the Lambert W function is available in of the saturated signals are obscured by the limited range the special-functions part of the GNU Scientific Library of the analog-to-digital converters. (GSL) [8], unfortunately implemented using only the slower Halley’s iteration (see discussion in Section6). 1.2. Inverse of the Gaisser-Hillas function There are numerous, well documented applications In astrophysics the Gaisser-Hillas function is used to of W(x), certainly abundant in mathematics (like lin- model the longitudinal particle density in a cosmic-ray ear delay-differential equations [9]), numerics [10], com- air shower [25]. We can show that the inverse of the puter science [11] and engineering [12], but surprisingly three-parametric Gaisser-Hillas function, many also in physics, just to mention a few (without Xmax−X0 preference): quantum mechanics (solutions for double- X − X0 l Xmax − X G(X; X0, Xmax, l) = exp , well Dirac-delta potentials [13]), quantum statistics [14], Xmax − X0 l general relativity (solutions to (1+1)-gravity problem [15], (7) 2 0.6 Note that the branch −1 or 0 simply chooses the right or left side relative to the maximum, respectively. 0.5 The Gaisser-Hillas function is intimately related to 0.4 the gamma distribution which was successfully used some- L what earlier [26] in an approximate description of the x H 0.3 mean longitudinal profile of the energy deposition in M electromagnetic cascades. It is trivial to show that the 0.2 inverses of the gamma and inverse-gamma distributions [27] are expressible in terms of the Lambert W function 0.1 as well. 0.0 -2 0 2 4 6 8 2. Numerical methods x 1.0 Before describing the actual implementation let us re- view some of the possible numerical and analytical ap- proaches to find W(x). 0.8 L 2.1. Recursion 0.6 m ax x > (x) > x For 0 and W 0 we can take the natural ; x logarithm of Eq. (4) and rearrange it into H 0.4 g W(x) = ln x − ln W(x). (13) 0.2 From here it is clear that an analytical expression for 0.0 W(x) will exhibit a certain degree of self similarity. The 0 5 10 15 20 25 30 W(x) function has multiple branches in the complex do- > ( ) > x main. Due to the x 0 and W x 0 conditions, Eq. (13) represents the positive part of the W0(x) prin- Figure 2: Top: The Moyal function M(x). Bottom: A family of one- cipal branch and in this form it is suitable for evaluation parametric Gaisser-Hillas functions g(x; xmax) for xmax in the range when W0(x) > 1, i.e. when x > e. from 1 to 10 with step 1. Unrolling the self-similarity in Eq. (13) as a recursive relation, the following curious expression for W0(x) is is intimately related to the Lambert W function. Using obtained, rescale substitutions, W0(x) = ln x − ln(ln x − ln(ln x − ... )), (14) X − X X − X x = 0 and x = max 0 , (8) l max l or in the shorthand of a continued logarithm, the Gaisser-Hillas function is modified into a function of x W0(x) = ln x . (15) ln x one parameter only, ln ··· xmax x The above expression clearly has the form of successive g(x; xmax) = exp(xmax − x). (9) xmax approximations, the final result given by the limit, if it exists. The family of one-parametric Gaisser-Hillas functions is For x < 0 and W(x) < 0 we can multiply both sides shown in Fig.2 (bottom). The problem of finding an of Eq. (4) with −1, take logarithm, and rewrite it into a inverse, similar expansion for the W−1(x) branch, g(x; xmax) ≡ y (10) W(x) = ln(−x) − ln(− W(x)). (16) for 0 < y 6 1, can be rewritten into x x Again, this leads to the similar recursion, − exp − = −y1/xmax e−1. (11) xmax xmax W−1(x) = ln(−x) − ln(−(ln(−x) − ln(− ...))), (17) According to the definition (2), the two (real) solutions for x are obtained from the two branches of the Lambert or as a continued logarithm, W function, −x W−1(x) = ln .