{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "7357147c",
            "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": "732e9d5b",
            "cell_type": "markdown",
            "source": "Calculate $\\int^4_0 x^2 dx$ and compare with an analytic result\n",
            "metadata": {}
        },
        {
            "id": "e241b1e4",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy import integrate\nimport numpy as np\nx2 = lambda x: x**2\nintegrate.quad(x2, 0, 4)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(21.333333333333332, 2.3684757858670003e-13)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "7656b1c1",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "print(4**3 / 3.)  # analytical result",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "21.3333333333"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "29239480",
            "cell_type": "markdown",
            "source": "Calculate $\\int^\\infty_0 e^{-x} dx$\n",
            "metadata": {}
        },
        {
            "id": "4ace6352",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "invexp = lambda x: np.exp(-x)\nintegrate.quad(invexp, 0, np.inf)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(1.0, 5.842605999138044e-11)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "1e3b280f",
            "cell_type": "markdown",
            "source": "Calculate $\\int^1_0 a x \\,dx$ for $a = 1, 3$\n",
            "metadata": {}
        },
        {
            "id": "89ec0c9d",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "f = lambda x, a: a*x\ny, err = integrate.quad(f, 0, 1, args=(1,))\ny",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.5"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "bf290eb8",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "y, err = integrate.quad(f, 0, 1, args=(3,))\ny",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "1.5"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "2907707c",
            "cell_type": "markdown",
            "source": "Calculate $\\int^1_0 x^2 + y^2 dx$ with ctypes, holding\ny parameter as 1\n```\n\ntestlib.c =>\ndouble func(int n, double args[n]){\nreturn args[0]*args[0] + args[1]*args[1];}\ncompile to library testlib.*\n\n```\n```\n\nfrom scipy import integrate\nimport ctypes\nlib = ctypes.CDLL('/home/.../testlib.*') #use absolute path\nlib.func.restype = ctypes.c_double\nlib.func.argtypes = (ctypes.c_int,ctypes.c_double)\nintegrate.quad(lib.func,0,1,(1))\n#(1.3333333333333333, 1.4802973661668752e-14)\nprint((1.0**3/3.0 + 1.0) - (0.0**3/3.0 + 0.0)) #Analytic result\n# 1.3333333333333333\n\n```\nBe aware that pulse shapes and other sharp features as compared to the\nsize of the integration interval may not be integrated correctly using\nthis method. A simplified example of this limitation is integrating a\ny-axis reflected step function with many zero values within the integrals\nbounds.\n",
            "metadata": {}
        },
        {
            "id": "02f45b29",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "y = lambda x: 1 if x<=0 else 0\nintegrate.quad(y, -1, 1)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(1.0, 1.1102230246251565e-14)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "76887a56",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "integrate.quad(y, -1, 100)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(1.0000000002199108, 1.0189464580163188e-08)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "a127dda9",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "integrate.quad(y, -1, 10000)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(0.0, 0.0)"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}