{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "c5217328",
            "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": "a2295183",
            "cell_type": "markdown",
            "source": "The Rosenbrock function maps from $\\mathbf{R}^m \\rightarrow \\mathbf{R}$;\nthe SciPy implementation `scipy.optimize.rosen` is vectorized to accept an\narray of shape ``(m, p)`` and return an array of shape ``p``. Suppose we wish\nto evaluate the Jacobian (AKA the gradient because the function returns a scalar)\nat ``[0.5, 0.5, 0.5]``.\n",
            "metadata": {}
        },
        {
            "id": "f78349c0",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.differentiate import jacobian\nfrom scipy.optimize import rosen, rosen_der\nm = 3\nx = np.full(m, 0.5)\nres = jacobian(rosen, x)\nref = rosen_der(x)  # reference value of the gradient\nres.df, ref",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(array([-51.,  -1.,  50.]), array([-51.,  -1.,  50.]))"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "aa90ba0d",
            "cell_type": "markdown",
            "source": "As an example of a function with multiple outputs, consider Example 4\nfrom [1].\n",
            "metadata": {}
        },
        {
            "id": "96cd27f1",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "def f(x):\n    x1, x2, x3 = x\n    return [x1, 5*x3, 4*x2**2 - 2*x3, x3*np.sin(x1)]",
            "outputs": []
        },
        {
            "id": "aba82a9d",
            "cell_type": "markdown",
            "source": "The true Jacobian is given by:\n",
            "metadata": {}
        },
        {
            "id": "f63c9ceb",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "def df(x):\n        x1, x2, x3 = x\n        one = np.ones_like(x1)\n        return [[one, 0*one, 0*one],\n                [0*one, 0*one, 5*one],\n                [0*one, 8*x2, -2*one],\n                [x3*np.cos(x1), 0*one, np.sin(x1)]]",
            "outputs": []
        },
        {
            "id": "edf3dde6",
            "cell_type": "markdown",
            "source": "Evaluate the Jacobian at an arbitrary point.\n",
            "metadata": {}
        },
        {
            "id": "334bd45c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "rng = np.random.default_rng()\nx = rng.random(size=3)\nres = jacobian(f, x)\nref = df(x)\nres.df.shape == (4, 3)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "78694beb",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "np.allclose(res.df, ref)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "d6c60362",
            "cell_type": "markdown",
            "source": "Evaluate the Jacobian at 10 arbitrary points in a single call.\n",
            "metadata": {}
        },
        {
            "id": "c0db1239",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "x = rng.random(size=(3, 10))\nres = jacobian(f, x)\nref = df(x)\nres.df.shape == (4, 3, 10)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "e2cd93ea",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "np.allclose(res.df, ref)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}