# Any ideas on how to make the Lambert W function in roblox?

Self explanatory… any ideas?

Lambert W function explained

Lambert W function - Wikipedia

Did you look hard enough? Someone made a module with all of these nifty math stuff

1 Like

I know but when i looked at the module i didnt really understand what the formula could be / what to return

I took this snippet from GitHub - protobi/lambertw: Javascript implementation of Lambert W function, and made it work for lua, i too dont exactly know why it works

I’ve been researching this function for a bit and found wolfram alpha provides an integral representation of it. (again, cant find an explanation/proof as to why it works)

(lambert w function with branch 2 of 3)

however after evaluating this outside of the function, i get a different result

ik this doesnt help, just wanted to give my 2 cents on lambert w and where i got with it. hopefully someone can provide a legitimate answer/research better than I

1 Like

What do you need it for, and do you need the entire function or just one branch? The function has to be solved for by numerical integration, and most implementations have a piecewise function as the starting point for a Newton’s method type solver, where the piecewise function is made up of different curves that crudely approximate different regions of the whole function.

That said, for specific applications, there are tricks where Lambert W function-based solutions can be reduced to partial Taylor series or Padé approximants that can be coded without iteration.

like this …

``````function LambertW(x)
local epsilon = 1e-10
local w = x
local prevW = x
local maxIterations = 1000
local iteration = 0

while iteration < maxIterations do
local eToW = math.exp(w)
local wTimesE = w * eToW
local f = wTimesE - x
local fPrime = eToW * (w + 1)

prevW = w
w = w - f / fPrime

if math.abs(w - prevW) < epsilon then
return w
end

iteration = iteration + 1
end

return w
end
``````
3 Likes

HOW’D YOU EVEN FIND A WAY TO DO THAT-

tysm <<33

It’s called newtons method, it isn’t a specific method for the lambert w function, but rather a one size fits all type thing

you can actually solve any equation with the method

its the process of making an initial guess for a function at some x value. then draw a tangent at that point on the function (through the use of derivatives). Wherever that line acrosses the x axis, that will be your new guess, you repeat this process until you start approaching some value. In the code presented above, it just iterates 1000 times and hopes it gets a close enough point, which is perfectly fine for a generalized equation solver.

I don’t like using this method for specific cases (such as cubic/quartic functions, or functions like this) because it’s probably not the most efficient way, and for the case of the lambert w function, it only solves for the real solutions of branch 0, which doesnt tell the full story.

1 Like

Oh, if only this were true

Aren’t you hand-waving over an important detail here, one that would make this whole process so much more generally useful? I mean of course, that you’ve summarized Newton’s method of root finding, but you haven’t explained the big (IMHO) leap from root-finding a differentiable function to iteratively finding the inverse of a function. There is of course a connection, but I think it’s fair to say that it’s not necessarily self-explanatorially obvious, especially not to someone who isn’t already familiar with Newon’s method.

Notice, for example, that 2112Jay’s example code (which BTW has an efficiency problem and AI-generated code smell), isn’t root finding the Lambert W function, or its inverse xe^x. I feel like this bit still needs to be explained. Do you want to do it? If not, I’d be happy to.

Well, this is just down to the code example not making a sensible initial guess. It’s actually going to find the W-1 function for some values of x, and W0 for others… not ideal. The iterative portion of the code could be made to solve for the W0 or W-1 branch correctly, with the right starting conditions. The poor choice of inital guess here is also going to make it do way more iterations than necessary, for a given precision, and increasingly so for larger x.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.