W-function of Lambert and its applications

Introduction

Mathematical analysis knows many beautiful functions with unusual properties. Among them are integral sine si(x)and logarithm li(x)also note the gamma function \Gamma(x) or the very famous Riemann zeta function \zeta(s). But today I invite the reader to look at the W-Lambert function W(x).

What is the Lambert W-function?

In order to understand what the Lambert W-function is, it is enough to look at the following equality, which, by analogy with the basic trigometric identity, I propose to call the “basic Lambert identity”:

W(x)e^{W(x)} = x

In other words, the Lambert function is the inverse of f(x)=xe^x. However, after the first studies, it becomes clear that f not injective, but exactly the same meaning y is achieved with two different arguments if y \in (-e^{-1}; 0). Therefore, the above definition needs some explanation.

Plot y = xe^x
Plot y = xe^x

By examining the derivative of the function f' = e^x(x+1)we understand that the function increases by[-1; +\infty)” alt=”[-1; +\infty)” src=”https://habrastorage.org/getpro/habr/upload_files/788/259/543/788259543709a1e97476adfe3066d2a7.svg” width=”82″ height=”22″/>и убывает на . Thus, let’s construct the inverse function to the given one on the corresponding intervals of monotonicity.

Plots of the Lambert W-function (black) and y = xe^x (grey)
Plots of the Lambert W-function (black) and y = xe^x (grey)

The branch for which y\in[-1;+\infty)[-1;+\infty)is called W_{0}(x)another – W_{-1}(x).

Formulation of the problem

Task. Learn to find the real roots of an equation of the following form:

x^pe^x=q \\p \in \mathbb{Q} \ \backslash \ \mathbb{Z}, q\in\mathbb{R_{+}}

As you probably already guessed, we will use the Lambert W-function for the solution. So, first we raise both the left and right sides to a power p^{-1}(this transformation is not equivalent for even integers p and for odd ones, it will be necessary to expand the set of values q to all real numbers, so we solve the problem for the above restrictions).

xe^{\frac{x}p} = q^{\frac{1}{p))

Now, in order to use the basic Lambertian identity, we need to obtain an expression with x the same as in the exponent. To do this, we divide both the left and right sides by p.

\frac{x}{p}e^{\frac{x}{p}}=\frac{q^{\frac{1}{p}}}{p}

And now we can use the basic Lambertian identity:

\frac{x}{p}=W(\frac{q^\frac{1}{p}}{p})

From where we get the final formula for x.

x = p*W(\frac{q^\frac{1}{p}}{p})

Calculation of the Lambert W-function

Note that when x\in (-e^{-1}; 0) the Lambert function gives two real values: one on each of the branches W_{0}(x) and W_{-1}(x) respectively. In this case, the original equation will have 2 roots.

Calculating W0. We will use the method binary search by answer. We can do this because W_{0}(x) increases by [-e^{-1};+\infty)[-e^{-1};+\infty).

The left bound of binary search is clear and equal to-one. Now the question arises how to choose the right border. The first idea that comes to mind is to put it equal to xbecause the inequality x \geq W(x)and equality is achieved only at zero.

x \geq W(x) \iff x \geq \frac{x}{e^{W(x)}} \iff (e^{W(x)}-1)x \geq0

However, for large enough x this may not be the best option. So let’s look at another one: we choose the right border equal to log(x).

ln(x) \geq W(x) \iff x \geq e^{W(x)} \iff x\geq \frac{x}{W(x)} \iff x(\frac{W(x) -1}{W(x)})\geq0 \\ x\geq e

Total: at-e^{-1}\leq x\leq e choose the right border xand whenx > e” src=”https://habrastorage.org/getpro/habr/upload_files/9e1/9e9/1b6/9e19e91b613d32e168fa09a2a344cc61.svg” width=”46″ height=”14″/> take <img data-lazyloaded=.

Plots y = x (blue), y = W(x) (green) and y = ln(x) (red)
Plots y = x (blue), y = W(x) (green) and y = ln(x) (red)

Asymptotics: O(\frac{ln(x)}{prec})prec – initially chosen precision (for example, 10-12)

Calculating W-one. Here we will use the following infinite expression for W_{-1}(x):

W_{-1}(x)=ln\frac{-x}{-ln\frac{-x}{\dots))

The deeper we go down, the higher the accuracy of the calculations.

Implementation in Python

from math import *

def LambertW0(x):
    left = -1
    right = x if x <= e else log(x)
    prec = 10**-12 # точность
    # бинарный поиск
    while right - left > prec:
        mid = (right + left) / 2
        if mid * exp(mid) > x:
            right = mid
        else:
            left = mid
    return right

def LambertW_1(x, t): # t - показатель точности
    if t == 100:
      	return log(-x)
    else:
      	return log((-x)/(-LambertW_1(x, t + 1)))

def sol(p, q):
    s = q**(1/p) / p
    if s < -exp(-1):
        return "No real solutions"
    ans = "Solutions: " + str(p * LambertW0(s)) + " "
    if -exp(-1) < s and s < 0:
      	ans += str(p * LambertW_1(s, 0))
    return ans
    

p = float(input())
q = float(input())
print(sol(p, q))

Examination

Equation 1 x^{-\frac{3}{2}}e^x = 2

Plots y = x^(-1.5)*e^x (purple) and y = 2 (blue)
Plots y = x^(-1.5)*e^x (purple) and y = 2 (blue)
Program output
Program output

Equation 2 x^{\frac{21}{10}}e^x=3

Plots y = x^2.1*e^x (purple) and y = 3 (blue)
Plots y = x^2.1*e^x (purple) and y = 3 (blue)
Program output
Program output

Equation 3 x^{-\frac{3}{4}}e^x=4

Plots y = x^(-0.75)*e^x (purple) and y = 4 (blue)
Plots y = x^(-0.75)*e^x (purple) and y = 4 (blue)
Program output
Program output

Conclusion / Conclusions

The values ​​on the 0 and -1 branches of the Lambert W-function can be calculated quite accurately in a short time, which makes it possible to solve some types of equations.

Similar Posts

Leave a Reply