{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "bf5aecc3",
            "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": "d6d456d7",
            "cell_type": "markdown",
            "source": "Compute the infinite sum of the reciprocals of squared integers.\n",
            "metadata": {}
        },
        {
            "id": "15af5eab",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.integrate import nsum\nres = nsum(lambda k: 1/k**2, 1, np.inf)\nref = np.pi**2/6  # true value\nres.error  # estimated error",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "np.float64(7.448762306416137e-09)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "4791c037",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "(res.sum - ref)/ref  # true error",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "np.float64(-1.839871898894426e-13)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "e0b3ac69",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res.nfev  # number of points at which callable was evaluated",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "np.int32(8561)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "7b8ae562",
            "cell_type": "markdown",
            "source": "Compute the infinite sums of the reciprocals of integers raised to powers ``p``,\nwhere ``p`` is an array.\n",
            "metadata": {}
        },
        {
            "id": "31174330",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy import special\np = np.arange(3, 10)\nres = nsum(lambda k, p: 1/k**p, 1, np.inf, maxterms=1e3, args=(p,))\nref = special.zeta(p, 1)\nnp.allclose(res.sum, ref)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "c8a035b3",
            "cell_type": "markdown",
            "source": "Evaluate the alternating harmonic series.\n",
            "metadata": {}
        },
        {
            "id": "d9a9fc1b",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res = nsum(lambda x: 1/x - 1/(x+1), 1, np.inf, step=2)\nres.sum, res.sum - np.log(2)  # result, difference vs analytical sum",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(np.float64(0.6931471805598691), np.float64(-7.616129948928574e-14))"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}