{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "066394e5",
            "cell_type": "markdown",
            "source": "<div class='alert alert-warning'>\n\nSciPy's interactive examples with Jupyterlite are experimental and may not always work as expected. Execution of cells containing imports may result in large downloads (up to 60MB of content for the first import from SciPy). Load times when importing from SciPy may take roughly 10-20 seconds. If you notice any problems, feel free to open an [issue](https://github.com/scipy/scipy/issues/new/choose).\n\n</div>",
            "metadata": {}
        },
        {
            "id": "373f0ba0",
            "cell_type": "markdown",
            "source": "The Lambert W function is the inverse of ``w exp(w)``:\n",
            "metadata": {}
        },
        {
            "id": "10ba1f98",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.special import lambertw\nw = lambertw(1)\nw",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(0.56714329040978384+0j)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "3ed3d286",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "w * np.exp(w)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(1.0+0j)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "751d504b",
            "cell_type": "markdown",
            "source": "Any branch gives a valid inverse:\n",
            "metadata": {}
        },
        {
            "id": "02833a62",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "w = lambertw(1, k=3)\nw",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(-2.8535817554090377+17.113535539412148j)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "d464fa16",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "w*np.exp(w)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(1.0000000000000002+1.609823385706477e-15j)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "e78437dd",
            "cell_type": "markdown",
            "source": "**Applications to equation-solving**\n\nThe Lambert W function may be used to solve various kinds of\nequations.  We give two examples here.\n\nFirst, the function can be used to solve implicit equations of the\nform\n\n    $x = a + b e^{c x}$\n\nfor $x$.  We assume $c$ is not zero.  After a little\nalgebra, the equation may be written\n\n    $z e^z = -b c e^{a c}$\n\nwhere $z = c (a - x)$.  $z$ may then be expressed using\nthe Lambert W function\n\n    $z = W(-b c e^{a c})$\n\ngiving\n\n    $x = a - W(-b c e^{a c})/c$\n\nFor example,\n",
            "metadata": {}
        },
        {
            "id": "e6bdc151",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "a = 3\nb = 2\nc = -0.5",
            "outputs": []
        },
        {
            "id": "0ca367e9",
            "cell_type": "markdown",
            "source": "The solution to $x = a + b e^{c x}$ is:\n",
            "metadata": {}
        },
        {
            "id": "c567e0e8",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "x = a - lambertw(-b*c*np.exp(a*c))/c\nx",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(3.3707498368978794+0j)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "40915086",
            "cell_type": "markdown",
            "source": "Verify that it solves the equation:\n",
            "metadata": {}
        },
        {
            "id": "33efa238",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "a + b*np.exp(c*x)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(3.37074983689788+0j)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "a78629b0",
            "cell_type": "markdown",
            "source": "The Lambert W function may also be used find the value of the infinite\npower tower $z^{z^{z^{\\ldots}}}$:\n",
            "metadata": {}
        },
        {
            "id": "ec574692",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "def tower(z, n):\n    if n == 0:\n        return z\n    return z ** tower(z, n-1)\n\ntower(0.5, 100)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.641185744504986"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "b3b439ec",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "-lambertw(-np.log(0.5)) / np.log(0.5)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(0.64118574450498589+0j)"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}