Note: this article makes use of MathML, the standard XML markup for math formulas. Sadly, this is not properly supported on some allegedly ‘modern’ and ‘feature-rich’ browsers. If the formulas don't make sense in your browser, consider reporting the issue to the respective developers and/or switching to a standard-compliant browser.

There is something I learned today that, as a mathematician, I should have probably learned some time ago: the name of the function that solves the equation

xex=y

where e is Napier's constant. I did know that the equation didn't have an elementary solution, but that didn't really help me solving the problem I wanted to solve, which was to find a solution to the equation:

x2ekx=c

for positive k,c (and x too, for what it's worth).

Apparently the non-elementary function we're interested in is called Lambert's W or ω function, or the product logarithm. It is the (multi-valued, in fact) function such that

x=W(y)y=xex.

The name product logarithm comes from the analogy with the (natural) logarithm

x=ln(y)y=ex

and the fact that there's an extra x multiplying the exponential on the right hand side of the equation we want to invert (solve for x).

I'm not going to discuss the properties of the function (I mean, what would the Wikipedia link above be here for, otherwise), but I will show how it can be used to solve the equation we're interested in, something that is not shown on Wikipedia and actually had me thinking for a while —mostly about the fact that I was obvioulsy missing something trivial, as it often happens in mathematics.

The general idea, to use the product logarithm in solving an equation, is to bring it in the form

f(x)ef(x)=c

which then leads to

f(x)=W(c)

and finally, assuming f is invertible, to x=f-1(W(c)). Of course, how to get to the form we want is not always obvious.

Let us take for example the equation I wanted to solve:

x2ekx=c.

Making sure k appears both as power of e and outside of it is trivial: simply multiply by k both sides. However, how to make sure we have the same power of x muliplying the exponential and as a power is not: the most obvious ideas (divide by x both sides, or multiply and divide by x the power and then try to juggle things around) don't really take you anywhere.

We'd have to be able to take the square root … wait, that might actually work!1 If we take the square root on both sides, then we get two equations (with the only difference being the sign, so we'll write it as one):

xek2x=±c.

Now we can balance the constants:

k2xek2x=±k2c

and apply the definition of W to get

k2x=W(±k2c)

and finally

x=2kW(±k2c).

As mentioned, W is a multi-valued function. However, for the specific application we derived the equation for we needed the largest positive value, meaning we need specifically the W0 branch (which is the only one that gives positive value), and since W0 is monotone increasing, we want the solution with the largest argument, that is, ultimately:

x=2kW0(k2c),

a surprisingly elegant formula —for which there is no built-in function in the C++ standard library.

(Surprisingly, making the latter discovery wasn't enough to reduce my enthusiasm and satisfaction in finding the answer and discovering the function, although it will be a problem I'll have to solve in the next few days —I'm not really looking forward to having to include the GSL or Boost libraries just for this.)


  1. this is actually more or less how it went. ↩