{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "1e54137d",
            "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": "80dc1c7e",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy import linalg\nrng = np.random.default_rng()\na = rng.standard_normal((9, 6))",
            "outputs": []
        },
        {
            "id": "c19bbda9",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "q, r = linalg.qr(a)\nnp.allclose(a, np.dot(q, r))",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "7bcdef82",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "q.shape, r.shape",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "((9, 9), (9, 6))"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "f288f7d9",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "r2 = linalg.qr(a, mode='r')\nnp.allclose(r, r2)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "d2aca632",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "q3, r3 = linalg.qr(a, mode='economic')\nq3.shape, r3.shape",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "((9, 6), (6, 6))"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "a4cdbba4",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "q4, r4, p4 = linalg.qr(a, pivoting=True)\nd = np.abs(np.diag(r4))\nnp.all(d[1:] <= d[:-1])",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "fb8e1400",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "np.allclose(a[:, p4], np.dot(q4, r4))",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "48b500a3",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "P = np.eye(p4.size)[p4]\nnp.allclose(a, np.dot(q4, r4) @ P)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "729c6c15",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "np.allclose(a @ P.T, np.dot(q4, r4))",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "09c04f54",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "q4.shape, r4.shape, p4.shape",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "((9, 9), (9, 6), (6,))"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "5da4a14d",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "q5, r5, p5 = linalg.qr(a, mode='economic', pivoting=True)\nq5.shape, r5.shape, p5.shape",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "((9, 6), (6, 6), (6,))"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "760a9803",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "P = np.eye(6)[:, p5]\nnp.allclose(a @ P, np.dot(q5, r5))",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}