Sunday, October 3, 2021

HP Prime Python: Complex Number Arithmetic and Lambert Function - and Firmware Update!

 HP Prime Python:  Complex Number Arithmetic and  Lambert Function

HP Prime Python:  Complex Number Arithmetic and  Lambert Function - and Firmware Update!


Firmware Update!


There is a new firmware update (10/1/2021) - v. 14596 for both the Hardware versions (Version 1 and Version 2) of the HP Prime.   The link to the MHPoC entry and download links:  


HPMoC Thread:

https://www.hpmuseum.org/forum/thread-17538-post-152707.html#pid152707


HP Prime Hardware 1 Download:

https://www.hpcalc.org/details/7469


HP Prime Hardware 2 Download:

https://www.hpcalc.org/details/7783


Now on to complex numbers and the Lambert function.


Python File Introduction

A work around of having separate Python files is to create separate apps for each application.  Both applications are available to download at the blog. 


Complex Number Arithmetic


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


Script:


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))


Example:

x1 = 9

y1 = -8  (use [ +/- ] to enter negative numbers)


x2 = -1

y2 = 4


Results:

z1 = 9-8j

z2 = -1+4j


z1 + z2 = 8-4j

z1 - z2 = 10-12j

z1 * z2 = 23+44j

z1 / z2 = -2.411764705882353-1.647058823529412j



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


Script:


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


Eddie

All original content copyright, © 2011-2021.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 



  Casio fx-7000G vs Casio fx-CG 50: A Comparison of Generating Statistical Graphs Today’s blog entry is a comparison of how a hist...