{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "1c8510fc",
            "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": "b1a12b04",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.linalg import schur, eigvals\nA = np.array([[0, 2, 2], [0, 1, 2], [1, 0, 1]])\nT, Z = schur(A)\nT",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[ 2.65896708,  1.42440458, -1.92933439],\n       [ 0.        , -0.32948354, -0.49063704],\n       [ 0.        ,  1.31178921, -0.32948354]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "321f12eb",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "Z",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[0.72711591, -0.60156188, 0.33079564],\n       [0.52839428, 0.79801892, 0.28976765],\n       [0.43829436, 0.03590414, -0.89811411]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "e320fcf2",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "T2, Z2 = schur(A, output='complex')\nT2",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[ 2.65896708, -1.22839825+1.32378589j,  0.42590089+1.51937378j], # may vary\n       [ 0.        , -0.32948354+0.80225456j, -0.59877807+0.56192146j],\n       [ 0.        ,  0.                    , -0.32948354-0.80225456j]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "a7c6a7cc",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "eigvals(T2)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([2.65896708, -0.32948354+0.80225456j, -0.32948354-0.80225456j])   # may vary"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "1fd1e393",
            "cell_type": "markdown",
            "source": "A custom eigenvalue-sorting condition that sorts by positive imaginary part\nis satisfied by only one eigenvalue.\n",
            "metadata": {}
        },
        {
            "id": "d1702df8",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "_, _, sdim = schur(A, output='complex', sort=lambda x: x.imag > 1e-15)\nsdim",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "1"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "05dbeb51",
            "cell_type": "markdown",
            "source": "When ``output='real'`` and the array `a` is real, the `sort` callable must accept\nthe real and imaginary parts as separate arguments. Note that now the complex\neigenvalues ``-0.32948354+0.80225456j`` and ``-0.32948354-0.80225456j`` will be\ntreated as a complex conjugate pair, and according to the `sdim` documentation,\ncomplex conjugate pairs for which the condition is True for *either* eigenvalue\nincrease `sdim` by *two*.\n",
            "metadata": {}
        },
        {
            "id": "adcc6815",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "_, _, sdim = schur(A, output='real', sort=lambda x, y: y > 1e-15)\nsdim",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "2"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}