Welcome, guest | Sign In | My Account | Store | Cart

Lambert W function.

Python, 19 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# http://en.wikipedia.org/wiki/Lambert_W_function
# FB - 201105297
import math
eps = 0.00000001 # max error allowed
def w0(x): # Lambert W function using Newton's method
    w = x
    while True:
        ew = math.exp(w)
        wNew = w - (w * ew - x) / (w * ew + ew)
        if abs(w - wNew) <= eps: break
        w = wNew
    return w

# usage example: solution of x**x = a
a = float(raw_input("Enter a positive number: "))
x = math.log(a) / w0(math.log(a)) # solution 1
print x
x = math.exp(w0(math.log(a))) # solution 2
print x
Created by FB36 on Sun, 29 May 2011 (MIT)
Python recipes (4591)
FB36's recipes (148)

Required Modules

  • (none specified)

Other Information and Tasks