{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "26109beb",
            "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": "6f9c81fb",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.special import spence\nimport matplotlib.pyplot as plt",
            "outputs": []
        },
        {
            "id": "b4eeadfb",
            "cell_type": "markdown",
            "source": "The function is defined for complex inputs:\n",
            "metadata": {}
        },
        {
            "id": "fc2ef373",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "spence([1-1j, 1.5+2j, 3j, -10-5j])",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([-0.20561676+0.91596559j, -0.86766909-1.39560134j,\n       -0.59422064-2.49129918j, -1.14044398+6.80075924j])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "f3cdab74",
            "cell_type": "markdown",
            "source": "For complex inputs on the branch cut, which is the negative real axis,\nthe function returns the limit for ``z`` with positive imaginary part.\nFor example, in the following, note the sign change of the imaginary\npart of the output for ``z = -2`` and ``z = -2 - 1e-8j``:\n",
            "metadata": {}
        },
        {
            "id": "7d58aab0",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "spence([-2 + 1e-8j, -2, -2 - 1e-8j])",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([2.32018041-3.45139229j, 2.32018042-3.4513923j ,\n       2.32018041+3.45139229j])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "a9275633",
            "cell_type": "markdown",
            "source": "The function returns ``nan`` for real inputs on the branch cut:\n",
            "metadata": {}
        },
        {
            "id": "a719d26b",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "spence(-1.5)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "nan"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "f7c80107",
            "cell_type": "markdown",
            "source": "Verify some particular values: ``spence(0) = pi**2/6``,\n``spence(1) = 0`` and ``spence(2) = -pi**2/12``.\n",
            "metadata": {}
        },
        {
            "id": "ca21d9b8",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "spence([0, 1, 2])",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([ 1.64493407,  0.        , -0.82246703])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "9e306d94",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "np.pi**2/6, -np.pi**2/12",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(1.6449340668482264, -0.8224670334241132)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "b7d66f43",
            "cell_type": "markdown",
            "source": "Verify the identity\n```\n\nspence(z) + spence(1 - z) = pi**2/6 - log(z)*log(1 - z)\n\n```",
            "metadata": {}
        },
        {
            "id": "15c319ff",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "z = 3 + 4j\nspence(z) + spence(1 - z)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(-2.6523186143876067+1.8853470951513935j)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "4e40e41a",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "np.pi**2/6 - np.log(z)*np.log(1 - z)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(-2.652318614387606+1.885347095151394j)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "a069a889",
            "cell_type": "markdown",
            "source": "Plot the function for positive real input.\n",
            "metadata": {}
        },
        {
            "id": "2005a896",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "fig, ax = plt.subplots()\nx = np.linspace(0, 6, 400)\nax.plot(x, spence(x))\nax.grid()\nax.set_xlabel('x')\nax.set_title('spence(x)')\nplt.show()",
            "outputs": []
        }
    ]
}