_lambertw.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from ._ufuncs import _lambertw
  2. def lambertw(z, k=0, tol=1e-8):
  3. r"""
  4. lambertw(z, k=0, tol=1e-8)
  5. Lambert W function.
  6. The Lambert W function `W(z)` is defined as the inverse function
  7. of ``w * exp(w)``. In other words, the value of ``W(z)`` is
  8. such that ``z = W(z) * exp(W(z))`` for any complex number
  9. ``z``.
  10. The Lambert W function is a multivalued function with infinitely
  11. many branches. Each branch gives a separate solution of the
  12. equation ``z = w exp(w)``. Here, the branches are indexed by the
  13. integer `k`.
  14. Parameters
  15. ----------
  16. z : array_like
  17. Input argument.
  18. k : int, optional
  19. Branch index.
  20. tol : float, optional
  21. Evaluation tolerance.
  22. Returns
  23. -------
  24. w : array
  25. `w` will have the same shape as `z`.
  26. Notes
  27. -----
  28. All branches are supported by `lambertw`:
  29. * ``lambertw(z)`` gives the principal solution (branch 0)
  30. * ``lambertw(z, k)`` gives the solution on branch `k`
  31. The Lambert W function has two partially real branches: the
  32. principal branch (`k = 0`) is real for real ``z > -1/e``, and the
  33. ``k = -1`` branch is real for ``-1/e < z < 0``. All branches except
  34. ``k = 0`` have a logarithmic singularity at ``z = 0``.
  35. **Possible issues**
  36. The evaluation can become inaccurate very close to the branch point
  37. at ``-1/e``. In some corner cases, `lambertw` might currently
  38. fail to converge, or can end up on the wrong branch.
  39. **Algorithm**
  40. Halley's iteration is used to invert ``w * exp(w)``, using a first-order
  41. asymptotic approximation (O(log(w)) or `O(w)`) as the initial estimate.
  42. The definition, implementation and choice of branches is based on [2]_.
  43. See Also
  44. --------
  45. wrightomega : the Wright Omega function
  46. References
  47. ----------
  48. .. [1] https://en.wikipedia.org/wiki/Lambert_W_function
  49. .. [2] Corless et al, "On the Lambert W function", Adv. Comp. Math. 5
  50. (1996) 329-359.
  51. https://cs.uwaterloo.ca/research/tr/1993/03/W.pdf
  52. Examples
  53. --------
  54. The Lambert W function is the inverse of ``w exp(w)``:
  55. >>> import numpy as np
  56. >>> from scipy.special import lambertw
  57. >>> w = lambertw(1)
  58. >>> w
  59. (0.56714329040978384+0j)
  60. >>> w * np.exp(w)
  61. (1.0+0j)
  62. Any branch gives a valid inverse:
  63. >>> w = lambertw(1, k=3)
  64. >>> w
  65. (-2.8535817554090377+17.113535539412148j)
  66. >>> w*np.exp(w)
  67. (1.0000000000000002+1.609823385706477e-15j)
  68. **Applications to equation-solving**
  69. The Lambert W function may be used to solve various kinds of
  70. equations, such as finding the value of the infinite power
  71. tower :math:`z^{z^{z^{\ldots}}}`:
  72. >>> def tower(z, n):
  73. ... if n == 0:
  74. ... return z
  75. ... return z ** tower(z, n-1)
  76. ...
  77. >>> tower(0.5, 100)
  78. 0.641185744504986
  79. >>> -lambertw(-np.log(0.5)) / np.log(0.5)
  80. (0.64118574450498589+0j)
  81. """
  82. return _lambertw(z, k, tol)