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?
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?
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$$
Although this has already been answered, I think a step by step explanation is more informative than a general one.
The following is my attempt:
\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*}
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.
This is an old question, but I cannot help myself from posting a really nice iterative approximation.
Taking a look at Wikipedia, we can see some boundary functions for $W_0(z)$ and $W_{-1}(z)$. The boundary function of interest is:
$$ \begin{align*} & W_0\left(z\right) \leq \ln \left({\frac {z+b}{1+\ln\left(b\right)}}\right) && \text{ if } b \lt \frac{1}{e} \\ & W_{-1}\left(z\right) \geq \ln \left({\frac {z+b}{1+\ln\left(b\right)}}\right) && \text{ if } b \gt \frac{1}{e} \end{align*} $$
This boundary function is an exact evaluation when $b=\frac{z}{W_0\left(z\right)}$ or $b=\frac{z}{W_{-1}\left(z\right)}$.
If we start with a good approximation for the branch of our choice, $W_0\left(z\right) \approx x_0$ or $W_{-1}\left(z\right) \approx x_0$, we can use the following iterative approach to rapidly converge to the true value.
$$ x_{i+1} = \ln\left( \frac{z+\frac{z}{x_{i}}}{1+\ln\left(\frac{z}{x_i}\right)} \right)$$
We can improve upon this. A Newton's method approximation requires an exponentiation of the current approximation of the function. However, this is an intermediate value that we already compute. Reusing the intermediate value gives us a Newton's iteration in terms of elementary operations, $+-\times\div$:
$$ \begin{align*} & r_i=\frac{z+\frac{z}{x_i}}{1+\ln\left(\frac{z}{x_i}\right)} \\ & s_i=\ln\left(r_i\right) \\ & x_{i+1}=s_i - \frac{s_i r_i - z}{(1 + s_i) r_i} \end{align*} $$
This will converge to within about $10^{-15}$ in $3$ iterations for inputs $z \gt -0.35$, if starting with the following suitable approximation for $x_0$.
$$ \begin{align*} & W_{0}\left(z\right) \approx \frac{z}{z+1} \\ & W_{-1}\left(z\right) \approx -2 \end{align*} $$
Or if $z$ is close to $-\frac{1}{e}$.
$$ \begin{align*} & W_{0}\left(z\right) \approx -1 + g - \frac{1}{3}g^2 \\ & W_{-1}\left(z\right) \approx -1 - g - \frac{1}{3}g^2 \\ & \text{where } g = \sqrt{-2 - 2\ln\left(-z\right)} \end{align*} $$