Post Reply 
Python: Complex Number Arithmetic and Lambert Function
07-14-2021, 02:42 PM
Post: #1
Python: Complex Number Arithmetic and Lambert Function
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-1M...sp=sharing
Visit this user's website Find all posts by this user
Quote this message in a reply
07-14-2021, 04:17 PM (This post was last modified: 07-14-2021 05:27 PM by Albert Chan.)
Post: #2
RE: Python: Complex Number Arithmetic and Lambert Function
Newton's method with f(w) = w*e^w - z is not stable with bad guess.
https://www.hpmuseum.org/forum/thread-15...#pid138282

A better setup is with f(w) = w + log(w/z), more stable and faster convergence.
https://www.hpmuseum.org/forum/thread-15...#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)
Find all posts by this user
Quote this message in a reply
Post Reply 




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