Post Reply 
lambertw, all branches
04-07-2023, 08:40 PM (This post was last modified: 04-14-2023 04:40 PM by Albert Chan.)
Post: #5
RE: lambertw, all branches
I am keeping code simple. Edge cases, example W0(0) = 0, are not implemented.

Code:
r, err = 0.36787944117144233, -1.2428753672788363E-17

require'complex'
function complex.log1p(x) local y = x-(-1); return I.log(y) - ((y-1-x)/y+0) end

function complex.W01(a, K, verbal, y)   -- K = abs(W branch) = 0 or 1
    K = K or 0
    local h, s = a+r+err, function(y) return I.log1p((y-r-err)/r) end
    if not y then
        y = I.sqrt(2*r*h)*(1-2*abs(K))  -- assume close to branch point
        y = r + y * I.sqrt(1+y/(3*r))   -- assume W imag part tiny
    end
    for i = 1, 10 do
        if verbal then print(a/y, 'W01') end
        h = y - (y+a) / s(y)
        y = y - h -- Newton's correction
        if y == y+h*0x1p-13 or not I.finite(y) then return a/y end
    end
    return NaN
end

function complex.W(a, k, verbal, x)
    a, k = I.new(a), k or 0
    local K, h, s = abs(k), a+r+err
    if I.abs(h)<.25 and (K==0 or K==1 and signbit(k*I.imag(a))) then
        return complex.W01(a, K, verbal), 'W01'
    end
    local A, T = I.abs(a), 2*k*pi+I.arg(a)
    s = function(x) return x + I.new(log(I.abs(x)/A), I.arg(x)-T) end
    if not x then     -- W rough guess
        if K > 1 then    x = 2*k*pi*I
        elseif K==1 then x = A==1 and 1 or I.log(-a)
        else --[[K==0]]  x = A==1 and 1 or I.log1p(a)
        end
    end
    for i = 1, 10 do
        if verbal then print(x) end
        h = x/(x-(-1)) * s(x)
        x = x - h -- Newton's correction
        if x == x+h*0x1p-13 or not I.finite(x) then return x end
    end
    return complex.W01(a, K, verbal, a/I.real(x)), 'W01'
end

W01 is solving W branch 0 or an ambiguous ±1 branch (with the small imag part)
The code is solving for y*ln(y) = a --> y = e^W(a) --> W(a) = a/y

We can "pull out" conjugate: W01(conj(a),k) = conj(W01(a,k))

lua> I.W(3+4*I, 5, true)
(0+31.41592653589793*I)
(-1.815554248884793+30.714634540472343*I)
(-1.8170058914565277+30.71333413897562*I)
(-1.8170058918466272+30.713334137004896*I)
(-1.8170058918466274+30.713334137004896*I)

lua> I.W(3+4*I, -5, true)
(-0-31.41592653589793*I)
(-1.7565805546696363-28.861921295458465*I)
(-1.7547636407605829-28.85710103800315*I)
(-1.7547636279545493-28.85710104741841*I)
(-1.7547636279545493-28.85710104741841*I)

lua> I.W(-0.3,-1,true)
(-1.7824743623491122+0*I)      W01
(-1.7813374873395937+0*I)      W01
(-1.7813370234217047+0*I)      W01
(-1.781337023421628+0*I)        W01

lua> I.W(-0.03,-1,true)
(-3.506557897319982-0*I)
(-5.261733759227882-0*I)
(-5.144793893666586-0*I)
(-5.1444827237861634-0*I)
(-5.144482721515682-0*I)
(-5.144482721515682-0*I)

lua> I.W(-.1+1e-100*I, -1, true)
(-2.3025850929940455-1e-099*I)
(-3.7769077194622533+1.2592576024726166e-099*I)
(-3.579124176402325-8.545840728943592*I)
(-3.7689826579869887-0.8337217972782689*I)
(-3.5535464061313635-0.025256342802877585*I)
(-3.5771468383315437+6.535012002211599e-005*I)
(-3.5770902410326704-8.721224618630954*I)
(-3.7811926340275566-0.8444302692850991*I)
(-3.5536175209072614-0.02647513850865879*I)
(-3.5771431199160215+6.824715919089008e-005*I)
(-3.5770875001635236+0*I)      W01
(-3.5771520641833883-1.3880349425219542e-099*I)      W01
(-3.5771520639572962-1.3880252213229777e-099*I)      W01
(-3.5771520639572976-1.388025221322979e-099*I)        W01

W code f=0 have trouble converging. Switched to W01 help a lot.
W01 use W last guess, but dropped the unreliable imaginary part.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
lambertw, all branches - Albert Chan - 04-07-2023, 01:24 PM
RE: lambertw, all branches - Albert Chan - 04-07-2023, 02:47 PM
RE: lambertw, all branches - Albert Chan - 04-19-2023, 01:30 AM
RE: lambertw, all branches - pier4r - 04-07-2023, 06:04 PM
RE: lambertw, all branches - Albert Chan - 04-07-2023, 07:54 PM
RE: lambertw, all branches - Albert Chan - 04-08-2023, 03:21 PM
RE: lambertw, all branches - Albert Chan - 04-08-2023, 05:54 PM
RE: lambertw, all branches - Albert Chan - 04-07-2023 08:40 PM
RE: lambertw, all branches - Albert Chan - 04-09-2023, 03:59 AM
RE: lambertw, all branches - Albert Chan - 04-09-2023, 04:36 PM
RE: lambertw, all branches - Albert Chan - 04-10-2023, 04:44 PM
RE: lambertw, all branches - Albert Chan - 04-10-2023, 06:47 PM
RE: lambertw, all branches - Albert Chan - 04-13-2023, 03:03 PM
RE: lambertw, all branches - floppy - 04-13-2023, 04:14 PM
RE: lambertw, all branches - Albert Chan - 04-23-2023, 02:49 PM
RE: lambertw, all branches - Albert Chan - 04-23-2023, 04:40 PM
RE: lambertw, all branches - Albert Chan - 01-19-2024, 04:14 PM
RE: lambertw, all branches - Albert Chan - 01-20-2024, 04:48 PM
RE: lambertw, all branches - Gil - 01-20-2024, 10:52 PM
RE: lambertw, all branches - Albert Chan - 01-21-2024, 01:14 AM
RE: lambertw, all branches - Albert Chan - 01-21-2024, 01:54 AM
RE: lambertw, all branches - Gil - 01-21-2024, 01:53 PM
RE: lambertw, all branches - Albert Chan - 01-21-2024, 04:19 PM
RE: lambertw, all branches - Gil - 01-21-2024, 04:35 PM
RE: lambertw, all branches - Albert Chan - 01-21-2024, 06:03 PM
RE: lambertw, all branches - Albert Chan - 01-21-2024, 07:01 PM
RE: lambertw, all branches - Gil - 01-21-2024, 07:30 PM
RE: lambertw, all branches - Gil - 01-21-2024, 08:39 PM
RE: lambertw, all branches - Albert Chan - 01-21-2024, 10:06 PM
RE: lambertw, all branches - Gil - 01-21-2024, 09:51 PM
RE: lambertw, all branches - Gil - 01-21-2024, 10:56 PM
RE: lambertw, all branches - Albert Chan - 01-22-2024, 01:34 AM
RE: lambertw, all branches - Gil - 01-21-2024, 11:15 PM
RE: lambertw, all branches - Gil - 01-22-2024, 06:09 PM
RE: lambertw, all branches - Albert Chan - 01-22-2024, 07:29 PM
RE: lambertw, all branches - Gil - 01-22-2024, 11:33 PM
RE: lambertw, all branches - Albert Chan - 01-23-2024, 02:32 AM
RE: lambertw, all branches - Gil - 01-23-2024, 02:35 PM
RE: lambertw, all branches - Albert Chan - 01-23-2024, 03:54 PM
RE: lambertw, all branches - Gil - 01-23-2024, 04:57 PM
RE: lambertw, all branches - Albert Chan - 01-23-2024, 06:17 PM
RE: lambertw, all branches - Gil - 01-23-2024, 06:44 PM
RE: lambertw, all branches - Gil - 01-23-2024, 11:00 PM
RE: lambertw, all branches - Gil - 01-24-2024, 03:18 PM
RE: lambertw, all branches - Albert Chan - 01-24-2024, 08:53 PM
RE: lambertw, all branches - Gil - 01-25-2024, 12:37 AM
RE: lambertw, all branches - Gil - 01-25-2024, 01:10 AM
RE: lambertw, all branches - Gil - 01-25-2024, 03:04 AM
RE: lambertw, all branches - Albert Chan - 01-25-2024, 07:02 AM
RE: lambertw, all branches - Gil - 01-25-2024, 10:09 AM
RE: lambertw, all branches - Albert Chan - 01-25-2024, 04:13 PM
RE: lambertw, all branches - Gil - 01-25-2024, 05:14 PM
RE: lambertw, all branches - Albert Chan - 01-25-2024, 05:57 PM
RE: lambertw, all branches - Gil - 01-25-2024, 06:19 PM
RE: lambertw, all branches - Albert Chan - 01-28-2024, 11:18 PM
RE: lambertw, all branches - Albert Chan - 02-01-2024, 02:17 AM
RE: lambertw, all branches - Albert Chan - 02-01-2024, 04:16 PM
RE: lambertw, all branches - Albert Chan - 02-02-2024, 11:49 AM



User(s) browsing this thread: 1 Guest(s)