HP Forums
Python: Complex Number Arithmetic and Lambert Function - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: HP Prime Software Library (/forum-15.html)
+--- Thread: Python: Complex Number Arithmetic and Lambert Function (/thread-17247.html)



Python: Complex Number Arithmetic and Lambert Function - Eddie W. Shore - 07-14-2021 02:42 PM

Complex Number Arithmetic

The HP Prime App "Python-Complex Arithmetic" performs the four arithmetic functions on two complex numbers.

Code:
from cmath import *
print("Complex Number Arithmetic")
x1=float(input("real_1: "))
# input values are not auto printed
print(x1)
y1=float(input("imag_y: "))
print(y1)
z1=complex(x1,y1)
print("z1 = "+str(z1))

x2=float(input("real_2: "))
print(x2)
y2=float(input("imag_2: "))
print(y2)
z2=complex(x2,y2)
print("z2 = "+str(z2))

print()
print("z1 + z2 = "+str(z1+z2))
print("z1 - z2 = "+str(z1-z2))
print("z1 * z2 = "+str(z1*z2))
print("z1 / z2 = "+str(z1/z2))

Lambert W Function

The HP Prime App "Python-Lambert W Function" approximates the Lambert W function. The Python script estimates w given complex number z using Newtons Method:

w * e^w = z

Code:
from cmath import *
print("Lambert W Function - approx")
print("w * exp(w) = z, find w")
zr=float(input("z_real: "))
print(zr)
zj=float(input("z_imag: "))
print(zj)
z=complex(zr,zj)
w0=complex(1,1)
w1=complex(1,1)

while abs(w1)>1*10**-10:
 w1=(w0*exp(w0)-z)/(exp(w0)+w0*exp(w0))
 w0=w0-w1

# round low parts to zero
# z.real, z.imag are read only
wr=w0.real
if abs(wr)<1*10**-10:
 wr=0
wj=w0.imag
if abs(wj)<1*10**-10:
  wj=0
w0=complex(wr,wj)
  
print("w = "+str(w0))


Example:

Input: -1.57079362679 (about π/2)
Result: approx -1.223152769062088e-06+1.57079554811127j

Input: 2+3j
Result: approx 1.090076534485791+0.5301397207748389j

Source:
Wikipedia. "Lambert W Function" Last updated June 13, 2021.
https://en.wikipedia.org/wiki/Lambert_W_function Retrieved July 9, 2021

Download both apps here (zip file):
https://drive.google.com/file/d/1MX5G-1MJidb2ZmoBVe57GpiUnYvY5BqD/view?usp=sharing


RE: Python: Complex Number Arithmetic and Lambert Function - Albert Chan - 07-14-2021 04:17 PM

Newton's method with f(w) = w*e^w - z is not stable with bad guess.
https://www.hpmuseum.org/forum/thread-15792-post-138282.html#pid138282

A better setup is with f(w) = w + log(w/z), more stable and faster convergence.
https://www.hpmuseum.org/forum/thread-15792-post-138355.html#pid138355

>>> from mpmath import *
>>> z = 2+3j
>>> w = 1+1j # guess of W(z)
>>> for i in range(5): w -= (w-z*exp(-w)) / (w+1); print i+1, w
...
1 (0.925920455007468 + 0.525629062362773j)
2 (1.11168436157991 + 0.529383890736917j)
3 (1.09040865244656 + 0.530090770362652j)
4 (1.09007661082612 + 0.530139691067221j)
5 (1.09007653448579 + 0.530139720774835j)

>>> w = 1+1j # guess of W(z)
>>> for i in range(5): w -= (w+log(w/z))*w/(w+1); print i+1, w
...
1 (1.1220615411005 + 0.505617553600088j)
2 (1.09019950929821 + 0.530419495346751j)
3 (1.09007653522354 + 0.530139702926175j)
4 (1.09007653448579 + 0.530139720774839j)
5 (1.09007653448579 + 0.530139720774839j)