{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "8dbb5f52",
            "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": "9af426ea",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.linalg import polar\na = np.array([[1, -1], [2, 4]])\nu, p = polar(a)\nu",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[ 0.85749293, -0.51449576],\n       [ 0.51449576,  0.85749293]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "a4266a88",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "p",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[ 1.88648444,  1.2004901 ],\n       [ 1.2004901 ,  3.94446746]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "0919e4ca",
            "cell_type": "markdown",
            "source": "A non-square example, with m < n:\n",
            "metadata": {}
        },
        {
            "id": "a2711f13",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "b = np.array([[0.5, 1, 2], [1.5, 3, 4]])\nu, p = polar(b)\nu",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[-0.21196618, -0.42393237,  0.88054056],\n       [ 0.39378971,  0.78757942,  0.4739708 ]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "35bbfa47",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "p",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[ 0.48470147,  0.96940295,  1.15122648],\n       [ 0.96940295,  1.9388059 ,  2.30245295],\n       [ 1.15122648,  2.30245295,  3.65696431]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "2761d60c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "u.dot(p)   # Verify the decomposition.",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[ 0.5,  1. ,  2. ],\n       [ 1.5,  3. ,  4. ]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "38d18387",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "u.dot(u.T)   # The rows of u are orthonormal.",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[  1.00000000e+00,  -2.07353665e-17],\n       [ -2.07353665e-17,   1.00000000e+00]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "cf157219",
            "cell_type": "markdown",
            "source": "Another non-square example, with m > n:\n",
            "metadata": {}
        },
        {
            "id": "c0e8b122",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "c = b.T\nu, p = polar(c)\nu",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[-0.21196618,  0.39378971],\n       [-0.42393237,  0.78757942],\n       [ 0.88054056,  0.4739708 ]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "8bad46e5",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "p",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[ 1.23116567,  1.93241587],\n       [ 1.93241587,  4.84930602]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "7fe1bb7c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "u.dot(p)   # Verify the decomposition.",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[ 0.5,  1.5],\n       [ 1. ,  3. ],\n       [ 2. ,  4. ]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "6e727795",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "u.T.dot(u)  # The columns of u are orthonormal.",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[  1.00000000e+00,  -1.26363763e-16],\n       [ -1.26363763e-16,   1.00000000e+00]])"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}