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