{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "504af4a5",
            "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": "07ac3928",
            "cell_type": "markdown",
            "source": "An Hadamard matrix, which has orthogonal columns, so we expect that\nthe suspace angle to be $\\frac{\\pi}{2}$:\n",
            "metadata": {}
        },
        {
            "id": "746b6b95",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.linalg import hadamard, subspace_angles\nrng = np.random.default_rng()\nH = hadamard(4)\nprint(H)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "[[ 1  1  1  1]\n [ 1 -1  1 -1]\n [ 1  1 -1 -1]\n [ 1 -1 -1  1]]"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "6009d1bb",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "np.rad2deg(subspace_angles(H[:, :2], H[:, 2:]))",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([ 90.,  90.])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "a28ae598",
            "cell_type": "markdown",
            "source": "And the subspace angle of a matrix to itself should be zero:\n",
            "metadata": {}
        },
        {
            "id": "ac9048bb",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "subspace_angles(H[:, :2], H[:, :2]) <= 2 * np.finfo(float).eps",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([ True,  True], dtype=bool)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "8f037106",
            "cell_type": "markdown",
            "source": "The angles between non-orthogonal subspaces are in between these extremes:\n",
            "metadata": {}
        },
        {
            "id": "ed4ab267",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "x = rng.standard_normal((4, 3))\nnp.rad2deg(subspace_angles(x[:, :2], x[:, [2]]))",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([ 55.832])  # random"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}