Python: Complex Number Arithmetic and Lambert Function
07-14-2021, 02:42 PM
Post: #1
 Eddie W. Shore Senior Member Posts: 1,251 Joined: Dec 2013
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