{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "1806b140",
            "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": "dcaf73d9",
            "cell_type": "markdown",
            "source": "You can compute a sparse distance matrix between two kd-trees:\n",
            "metadata": {}
        },
        {
            "id": "7ecabc68",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.spatial import KDTree\nrng = np.random.default_rng()\npoints1 = rng.random((5, 2))\npoints2 = rng.random((5, 2))\nkd_tree1 = KDTree(points1)\nkd_tree2 = KDTree(points2)\nsdm = kd_tree1.sparse_distance_matrix(kd_tree2, 0.3)\nsdm.toarray()",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[0.        , 0.        , 0.12295571, 0.        , 0.        ],\n   [0.        , 0.        , 0.        , 0.        , 0.        ],\n   [0.28942611, 0.        , 0.        , 0.2333084 , 0.        ],\n   [0.        , 0.        , 0.        , 0.        , 0.        ],\n   [0.24617575, 0.29571802, 0.26836782, 0.        , 0.        ]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "df41b238",
            "cell_type": "markdown",
            "source": "You can check distances above the `max_distance` are zeros:\n",
            "metadata": {}
        },
        {
            "id": "b5ac16c3",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.spatial import distance_matrix\ndistance_matrix(points1, points2)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[0.56906522, 0.39923701, 0.12295571, 0.8658745 , 0.79428925],\n   [0.37327919, 0.7225693 , 0.87665969, 0.32580855, 0.75679479],\n   [0.28942611, 0.30088013, 0.6395831 , 0.2333084 , 0.33630734],\n   [0.31994999, 0.72658602, 0.71124834, 0.55396483, 0.90785663],\n   [0.24617575, 0.29571802, 0.26836782, 0.57714465, 0.6473269 ]])"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}