# Lambert- W -Function calculation?

I have an equation of the form:

$$n \log n = x$$

Upon searching I came across the term "Lambert- W -Function" but couldn't find a proper method for evaluation, and neither any computer code for it's evaluation.

Any ideas as to how I can evaluate?

• Do you mean an evaluation of the function for a certain argument? Sorry, just trying to make sure I understand your question. – Sheheryar Zaidi Sep 21 '14 at 8:39
• Well,this is more of a programming question,but was just wondering if it could be solved manually. For given value of x,can we find n? – techriften Sep 21 '14 at 8:41
• mathworld.wolfram.com/LambertW-Function.html give several series expansions. See the equations (11), (13) and (14). – Nabigh Sep 21 '14 at 8:43
• wikipedia has a derivate containing code ("wikicode"? "Rosettastone"?). I found it easy to find a recursive programming-example, translatable into Pari/GP – Gottfried Helms Sep 21 '14 at 8:49
• $n\ln(n)=x\implies \exp(n\ln(n))=\exp(x)\implies n^n=\exp(x)\implies n=\frac{x}{W(x)}$, using Example 2 of the wiki article on $W$. – Yiannis Galidakis Sep 21 '14 at 9:11

Let us consider the function $$f(x)=x \log(x)-a$$ Effcetively, the solution of $f(x)=0$ is given by $$x=\frac{a}{W(a)}$$ and, if I properly understood, you look for a computation method for getting $W(a)$.

From definition $W(a)$ is defined such that $a=W(a)e^{W(a)}$ so Newton method seems to be (and is) very good.

I strongly suggest you have a look at http://en.wikipedia.org/wiki/Lambert_W_function. In the paragraph entitled "Numerical evaluation", they give Newton and Halley formulae (the latest one has been massively used by Corless et al. to compute $W(a)$.

In the same Wikipedia page, you will find very nice and efficient approximations of $W(a)$ for small and large values. These estimates will allow you to start really close to the solution.

If I may underline one thing which is really nice : all derivatives od $W(a)$ express as functions of $a$ and $W(a)$ itself and this is extremely convenient.

You could be interested by http://people.sc.fsu.edu/~jburkardt/cpp_src/toms443/toms443.html where the source code is available.

Although an old post, I'm surprised nobody mentioned this:

$$n\log n=(e^{\log n})\log n=x$$

Now it should be obvious on how to proceed.

$$W(x)=\log n$$

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

And because $W(x)e^{W(x)}=x$, $e^W(x)=\frac{x}{W(x)}$ so,

$$\frac{x}{W(x)}=n$$

If you're using Matlab or want to look at a robust (but simple) implementation of the Lambert $W$ function ($W_0$ branch only) using Halley's method, see my answer here.

As an alternative to @ClaudeLeibovici's more general answer, an equation specifically of the form $n \text{ln}(n) = x$ might best be solved and analyzed using the simpler Wright $\omega$ function:

$$n = \frac{x}{\omega(-ln(1/x))}$$

Matlab has wrightOmega and Maple has Wrightomega to evaluate this function symbolically. See Corless and Jeffrey, 2002, The Wright omega Function (PDF) for further details. Unfortunately, there does not seem to be support for this function in SciPy or SymPy.

The Wright $\omega$ function can also be evaluated numerically. See Lawrence, et al., 2012, Algorithm 917: Complex Double-Precision Evaluation of the Wright ω Function. I've implemented this algorithm in Matlab – you can find it on GitHub here. Evaluating this numerically is 3+ orders of magnitude faster than evaluating it symbolically. If your $x$ parameter is constrained in particular ways (e.g., real-valued), you may be able to simplify your algorithm – see the Lawrence, et al. paper and the comments in my wrightOmega code.

\begin{align*} n \ln (n) =x \quad & \Rightarrow \quad \ln n^n=x \\ & \Rightarrow \quad n^n=e^x \\ & \Rightarrow \quad n=e^{x/n} \\ & \Rightarrow \quad n \times \frac{x}{n}=\frac{x}{n}e^{x/n} \\ & \Rightarrow \quad x=\frac{x}{n}e^{x/n} \\ & \Rightarrow \quad W(x)=W \left(\frac{x}{n}e^{x/n} \right) \\ & \Rightarrow \quad W(x)=\frac{x}{n} \\ & \Rightarrow \quad \boxed{n=\frac{x}{W(x)}} \\ & \Rightarrow \quad n=\frac{x}{x/e^{W(x)}} \quad \text{(since W(x)e^{W(x)}=x)}\\ & \Rightarrow \quad \boxed{n=e^{W(x)}} \\ \end{align*}
• How do we replace $\log(x)$ with $\ln(x)$? – Mathrix Mar 30 at 0:37